package com.assess.util;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;public classSendmailUtil {/** 参考此文章
*http://www.cnblogs.com/xdp-gacl/p/4216311.html*
*
* smtp.sohu.com 搜狐邮箱主机
* smtp.163.com 163邮箱主机,默认端口25
* smtp.qq.com qq邮箱主机
**/
/**
* from 发件人邮箱
* passwd 发件人邮箱密码
* to 收件人邮箱
* subject 主题
* txt 内容
* files 附件
* host 服务主机
* protocol 协议 (smtp)
* auth 身份验证(true)
**/
public static boolean send(String from,String passwd,String to,
String subject, String txt,Listfiles,
String host,String protocol,String auth) {try{
Properties prop= newProperties();
prop.setProperty("mail.host", host);
prop.setProperty("mail.transport.protocol", protocol);
prop.setProperty("mail.smtp.auth", auth);//使用JavaMail发送邮件的5个步骤//1、创建session
Session session =Session.getInstance(prop);//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);//2、通过session得到transport对象
Transport ts =session.getTransport();//3、使用邮箱的用户名和密码连上邮件服务器,//发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,//用户名和密码都通过验证之后才能够正常发送邮件给收件人。
ts.connect(host, from, passwd);//4、创建邮件
Message message = createMixedMail(session, from, to, subject, txt, files);//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}catch(Exception e) {return false;
}return true;
}/**
* 发送文字、附件的邮件
**/
public static MimeMessage createMixedMail(Session session,String from,String to,String subject,String txt,Listfiles) throws Exception {//创建邮件
MimeMessage message = newMimeMessage(session);//设置邮件的基本信息
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO,newInternetAddress(to));
message.setSubject(subject);//正文
MimeBodyPart text = newMimeBodyPart();
text.setContent(txt,"text/html;charset=UTF-8");//图片//MimeBodyPart image = new MimeBodyPart();//image.setDataHandler(new DataHandler(new FileDataSource("src\\check.png")));//image.setContentID("aaa.jpg");//附件
List mimeBodyParts = new ArrayList();for(int i=0;i
MimeBodyPart attach= newMimeBodyPart();
DataHandler dh= new DataHandler(new FileDataSource(files.get(i)));
attach.setDataHandler(dh);
attach.setFileName(MimeUtility.encodeWord(dh.getName()));
mimeBodyParts.add(attach);
}//描述关系:正文和图片
MimeMultipart mp1 = newMimeMultipart();
mp1.addBodyPart(text);//mp1.addBodyPart(image);
mp1.setSubType("related");//描述关系:正文和附件
MimeMultipart mp2 = newMimeMultipart();for(int i=0;i
mp2.addBodyPart(mimeBodyParts.get(i));
}//mp2.addBodyPart(attach2);//代表正文的bodypart
MimeBodyPart content = newMimeBodyPart();
content.setContent(mp1);
mp2.addBodyPart(content);
mp2.setSubType("mixed");
message.setContent(mp2);
message.saveChanges();//message.writeTo(new FileOutputStream("D:\\MixedMail.eml"));//返回创建好的的邮件
returnmessage;
}
}