package common.mail;
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import common.mail.Mail.MailInfor.Accessory;
/**
* classname Mail
* action 用于发送mail,借住163的smtp服务器
* @version 2007/7/26
* */
public class Mail {
/**
* 内部类MailInor包函了发送邮件需要的基本信息
* 以属性方式需要对其进行配置
* */
class MailInfor {
/**
* constructor 无参
* action 对属性进行初始化
* */
MailInfor() {
setAccessory(false);
}
private String host = "smtp.163.com";
private String fromMail = "zhangliyang_good@163.com";
private List toMail;
private String userName = "zhangliyang_good";
private String password = "xtgmybjjmh";
private String subject;
private String content;
private boolean isDebug = false;
private boolean accessory;
private List accessorys;
public void setAccessorys(List accessList) {
this.accessorys = accessList;
}
public List getAccessorys() {
return this.accessorys;
}
public void setHost(String host) {
this.host = host;
}
public void setFromMail(String fromMail) {
this.fromMail = fromMail;
}
public void setToMail(List toMail) {
this.toMail = toMail;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassword(String password) {
this.password = password;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setContent(String content) {
this.content = content;
}
public String getHost() {
return host;
}
public String getFromMail() {
return fromMail;
}
public List getToMail() {
return toMail;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
public String getSubject() {
return subject;
}
public String getContent() {
return content;
}
public void setIsDebug(boolean isDebug) {
this.isDebug = isDebug;
}
public boolean isIsDebug() {
return isDebug;
}
public void setAccessory(boolean accessory) {
this.accessory = accessory;
}
public boolean isAccessory() {
return accessory;
}
/**
* 内部类Accessory用于发送附件,包函了发送附件需要的信息
* 以属性方式需要对其进行配置
* */
class Accessory {
private String fileUrl;
private String fileName;
public void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl;
}
public String getFileUrl() {
return this.fileUrl;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileName() {
return this.fileName;
}
}
}
public boolean sendMail(MailInfor infor) {
Properties props = new Properties();
props.put("mail.smtp.host", infor.getHost());
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
//是否在控制台输出日志信息
session.setDebug(infor.isIsDebug());
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(infor.getFromMail()));
List list = infor.getToMail();
if (list != null) {
Iterator iter = list.iterator();
while (iter.hasNext()) {
message.addRecipient(Message.RecipientType.TO,
new InternetAddress( (String) iter.next()));
}
}
else {
throw new NullPointerException("tomail is not null");
}
message.setSubject(infor.getSubject());
MimeMultipart mm = new MimeMultipart();
//普通文本
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(infor.getContent(), "text/html;charset=gb2312");
//附件
if (infor.isAccessory()) {
List accessorys = infor.getAccessorys();
Iterator iter = accessorys.iterator();
BodyPart streamContent = null;
DataSource dataSource = null;
Accessory accessory = null;
while (iter.hasNext()) {
streamContent = new MimeBodyPart();
accessory = (Accessory) iter.next();
dataSource = new FileDataSource(accessory.getFileUrl());
streamContent.setDataHandler(new DataHandler(dataSource));
streamContent.setFileName(accessory.getFileName());
mm.addBodyPart(streamContent);
}
}
//填加相应的属性
mm.addBodyPart(htmlPart);
message.setContent(mm);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(infor.getHost(), infor.getUserName(),
infor.getPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
//示例
public static void main(String[] args) {
Mail mail = new Mail();
MailInfor infor = mail.new MailInfor();
List list = new ArrayList();
list.add("aaaaa@163.com");
infor.setToMail(list);
infor.setSubject("你好");
infor.setContent("......................");
infor.setFromMail("aaaaa@163.com");
infor.setUserName("aaaaa");
infor.setPassword("123456");
infor.setAccessory(true);
//填加附件
list = new ArrayList();
for (int i = 0; i < 2; i++) {
Accessory accessory = infor.new Accessory();
accessory.setFileName("bb.jpg");
accessory.setFileUrl(
"c:/{D940ED42-3EDC-430B-ACE2-2764187EBFED}0.jpg");
list.add(accessory);
}
infor.setAccessorys(list);
if (mail.sendMail(infor)) {
System.out.println("成功");
}
}
}