当前位置: 技术问答>java相关
有这样的mail组件吗?
来源: 互联网 发布时间:2015-01-01
本文导语: 我要做一个电子报,要求可以发给很多用户,而且可以发html格式的,有这样的东东吗? | java.sun.com/products/javamail | //---------------Ping.java // Copyright (C) 1998 Yves Soun. // This ...
我要做一个电子报,要求可以发给很多用户,而且可以发html格式的,有这样的东东吗?
|
java.sun.com/products/javamail
|
//---------------Ping.java
// Copyright (C) 1998 Yves Soun.
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details (cf. file COPYING).
import java.net.*;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class sendmail extends Thread {
String to = null,subject = null, from = null,
cc = null, bcc = null, url = null;
String mailhost = null;
String content = null;
String mailer = "msgsend";
String protocol = null, host = null, user = null, password = null;
String record = null; // name of folder in which to record mail
boolean debug = false;
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
int optind;
JdbcBean jdbcBean ,jdbcBean2;
public static void main(String argv[]){
try{
new sendmail().start();
}
catch(Exception ex){
System.out.println(ex.toString());
}
}
public void run()
{
try{
for(;;) {
System.out.println("asdfasdf");
try{
sendmail("asdf");
}
catch(Exception ex){
}
sleep(5000000);
}
catch(Exception ex){
System.out.println(ex.toString());
}
}
public sendmail()
{
}
public void sendmail(String abc)
throws Exception
{
java.util.Date Today = new java.util.Date();
int Years = 1900 + Today.getYear();
int Months = 1 + Today.getMonth();
String now_today = Years + "/" + Months + "/" + Today.getDate()
+ " " + Today.getHours() + ":" + Today.getMinutes() + ":" + Today.getSeconds();
try {
smail("hello myfriends!");
}
catch ( Exception e) {
System.out.println("Error in : " + e.toString());
}
}
public void smail(String errmsg)
throws Exception
{
/* fetch this part of info from database */
System.out.println("send mail begin." );
try{
Properties props = System.getProperties();
// XXX - could use Session.getTransport() and Transport.connect()
// XXX - assume we're using SMTP
mailhost="192.168.2.1";
from ="heming96@sina.com.cn";
//to = "hem@denet.net.cn";
to = from ;
cc = "hem@denet.com.cn";
subject= "我是何明";
if (mailhost != null)
props.put("mail.smtp.host", mailhost);
// Get a Session object
Session session = Session.getDefaultInstance(props, null);
if (debug)
session.setDebug(true);
// construct the javax.mail.Message
javax.mail.Message msg = new MimeMessage(session);
if (from != null)
msg.setFrom(new InternetAddress(from));
else
msg.setFrom();
msg.setRecipients(javax.mail.Message.RecipientType.TO,
InternetAddress.parse(to, false));
if (cc != null)
msg.setRecipients(javax.mail.Message.RecipientType.CC,
InternetAddress.parse(cc, false));
if (bcc != null)
msg.setRecipients(javax.mail.Message.RecipientType.BCC,
InternetAddress.parse(bcc, false));
msg.setSubject(subject);
//collect(in, msg);
content=errmsg;
msg.setText(content);
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new java.util.Date());
// send the thing off
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
public void collect(BufferedReader in, javax.mail.Message msg)
throws MessagingException, IOException {
String line;
StringBuffer sb = new StringBuffer();
while (((line = in.readLine()) != null )) {
System.out.println(line);
if(line.equals("EOF"))
break;
sb.append(line);
sb.append("n");
}
// If the desired charset is known, you can use
// setText(text, charset)
msg.setText(sb.toString());
}
}
// Copyright (C) 1998 Yves Soun.
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details (cf. file COPYING).
import java.net.*;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class sendmail extends Thread {
String to = null,subject = null, from = null,
cc = null, bcc = null, url = null;
String mailhost = null;
String content = null;
String mailer = "msgsend";
String protocol = null, host = null, user = null, password = null;
String record = null; // name of folder in which to record mail
boolean debug = false;
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
int optind;
JdbcBean jdbcBean ,jdbcBean2;
public static void main(String argv[]){
try{
new sendmail().start();
}
catch(Exception ex){
System.out.println(ex.toString());
}
}
public void run()
{
try{
for(;;) {
System.out.println("asdfasdf");
try{
sendmail("asdf");
}
catch(Exception ex){
}
sleep(5000000);
}
catch(Exception ex){
System.out.println(ex.toString());
}
}
public sendmail()
{
}
public void sendmail(String abc)
throws Exception
{
java.util.Date Today = new java.util.Date();
int Years = 1900 + Today.getYear();
int Months = 1 + Today.getMonth();
String now_today = Years + "/" + Months + "/" + Today.getDate()
+ " " + Today.getHours() + ":" + Today.getMinutes() + ":" + Today.getSeconds();
try {
smail("hello myfriends!");
}
catch ( Exception e) {
System.out.println("Error in : " + e.toString());
}
}
public void smail(String errmsg)
throws Exception
{
/* fetch this part of info from database */
System.out.println("send mail begin." );
try{
Properties props = System.getProperties();
// XXX - could use Session.getTransport() and Transport.connect()
// XXX - assume we're using SMTP
mailhost="192.168.2.1";
from ="heming96@sina.com.cn";
//to = "hem@denet.net.cn";
to = from ;
cc = "hem@denet.com.cn";
subject= "我是何明";
if (mailhost != null)
props.put("mail.smtp.host", mailhost);
// Get a Session object
Session session = Session.getDefaultInstance(props, null);
if (debug)
session.setDebug(true);
// construct the javax.mail.Message
javax.mail.Message msg = new MimeMessage(session);
if (from != null)
msg.setFrom(new InternetAddress(from));
else
msg.setFrom();
msg.setRecipients(javax.mail.Message.RecipientType.TO,
InternetAddress.parse(to, false));
if (cc != null)
msg.setRecipients(javax.mail.Message.RecipientType.CC,
InternetAddress.parse(cc, false));
if (bcc != null)
msg.setRecipients(javax.mail.Message.RecipientType.BCC,
InternetAddress.parse(bcc, false));
msg.setSubject(subject);
//collect(in, msg);
content=errmsg;
msg.setText(content);
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new java.util.Date());
// send the thing off
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
public void collect(BufferedReader in, javax.mail.Message msg)
throws MessagingException, IOException {
String line;
StringBuffer sb = new StringBuffer();
while (((line = in.readLine()) != null )) {
System.out.println(line);
if(line.equals("EOF"))
break;
sb.append(line);
sb.append("n");
}
// If the desired charset is known, you can use
// setText(text, charset)
msg.setText(sb.toString());
}
}
|
用JavaMail吧!