【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“EmailTeller.java”一文,供大家参考学习!
package com.ideas.communicate;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import com.ideas.util.Configuration;
public class EmailTeller
extends Thread {
private String title;
private String content;
private String[] address;
public EmailTeller(String title, String content, String[] address) {
this.title = title;
this.content = content;
this.address = address;
}
public void run() {
try {
Properties props = new Properties();
props.put("mail.smtp.host", Configuration.MAILServer);
props.put("mail.smtp.auth", Configuration.MAILAuth);
Session s = Session.getInstance(props);
s.setDebug(false);
MimeMessage message = new MimeMessage(s);
/**给消息对象设置发件人/收件人/主题/发信时间*/
InternetAddress from = new InternetAddress(Configuration.MAILAddr);
message.setFrom(from);
InternetAddress[] to = new InternetAddress[address.length];
for (int i = 0; i < to.length; i++) {
to[i] = new InternetAddress(address[i]);
}
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject(title);
message.setSentDate(new Date());
/**给消息对象设置内容*/
BodyPart mdp = new MimeBodyPart(); //新建一个存放信件内容的BodyPart对象
mdp.setContent(content, "text/html;charset=gb2312"); //给BodyPart对象设置内容和格式/编码方式
Multipart mm = new MimeMultipart();
/*新建一个MimeMultipart对象用来存放BodyPart对
象(事实上可以存放多个)*/
mm.addBodyPart(mdp); //将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
message.setContent(mm); //把mm作为消息对象的内容
message.saveChanges();
Transport transport = s.getTransport("smtp");
transport.connect(Configuration.MAILServer, Configuration.MAILUser,
Configuration.MAILPasswd);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (javax.mail.MessagingException me){}
catch (Exception e) {
System.err.println("发送电子邮件警报错误!!");
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
String [] address={"3710167@qq.com","3710167@qq.com"};
for (int i = 0; i < 20; i++) {
new EmailTeller("你好", "今天吃饭了吗?\n没有",
address).start();
Thread.sleep(4000);
}
}
}
上一篇:
EmailSystemBean.java
下一篇:
手机媒体营销分析