packageemail;importjava.io.File;importjava.io.UnsupportedEncodingException;importjava.util.Date;importjava.util.List;importjavax.activation.DataHandler;importjavax.activation.FileDataSource;importjavax.mail.Message;importjavax.mail.Message.RecipientType;importjavax.mail.MessagingException;importjavax.mail.Multipart;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;importjavax.mail.internet.MimeUtility;importorg.apache.commons.lang3.StringUtils;/*** user:Rex
* date:2016年12月25日 上午3:56:49
* TODO 邮件工具类*/
public classEmailUtil {/*** user: Rex
* date: 2016年12月25日 上午4:29:18
*@paramsubject 邮件标题
*@paramcontent 邮件内容
*@paramto 收件人(多个收件人用英文逗号“,”隔开)
*@throwsException*/
public static void sendEmail(String subject, String content, String to) throwsException{
Message msg= createMessage(subject, content, to, null);//连接邮件服务器、发送邮件
Transport.send(msg);
}/*** user: Rex
* date: 2016年12月25日 上午4:17:11
*@paramsubject 邮件标题
*@paramcontent 邮件内容
*@paramto 收件人(多个收件人用英文逗号“,”隔开)
*@paramtype
*@paramotherRecipient 抄送人或暗送人(多个抄送人或暗送人用英文逗号“,”隔开)
*@return邮箱对象
*@throwsException*/
public static void sendEmail(String subject, String content, String to, RecipientType type, String otherRecipient) throwsException{
Message msg= createMessage(subject, content, to, type, otherRecipient, null);//连接邮件服务器、发送邮件
Transport.send(msg);
}/*** user: Rex
* date: 2016年12月25日 上午4:17:11
*@paramsubject 邮件标题
*@paramcontent 邮件内容
*@paramto 收件人(多个收件人用英文逗号“,”隔开)
*@paramcc 抄送人(多个抄送人用英文逗号“,”隔开)
*@parambcc 暗送人(多个暗送人用英文逗号“,”隔开)
*@return邮箱对象
*@throwsException*/
public static void sendEmail(String subject, String content, String to, String cc, String bcc) throwsException{
Message msg= createMessage(subject, content, to, cc, bcc, null);//连接邮件服务器、发送邮件
Transport.send(msg);
}/*** user: Rex
* date: 2016年12月25日 上午7:04:02
*@paramsubject 邮件标题
*@paramcontent 邮件内容
*@paramto 收件人(多个收件人用英文逗号“,”隔开)
*@paramfileList 附件
*@throwsException*/
public static void sendEmail(String subject, String content, String to, List fileList) throwsException{
Message msg=createMessage(subject, content, to, fileList);//连接邮件服务器、发送邮件
Transport.send(msg);
}/*** user: Rex
* date: 2016年12月25日 上午7:04:02
*@paramsubject 邮件标题
*@paramcontent 邮件内容
*@paramto 收件人(多个收件人用英文逗号“,”隔开)
*@paramtype
*@paramotherRecipient 抄送人或暗送人(多个抄送人或暗送人用英文逗号“,”隔开)
*@paramfileList 附件
*@throwsException*/
public static void sendEmail(String subject, String content, String to, RecipientType type, String otherRecipient, List fileList) throwsException{
Message msg=createMessage(subject, content, to, type, otherRecipient, fileList);//连接邮件服务器、发送邮件
Transport.send(msg);
}/*** user: Rex
* date: 2016年12月25日 上午7:04:02
*@paramsubject 邮件标题
*@paramcontent 邮件内容
*@paramto 收件人(多个收件人用英文逗号“,”隔开)
*@paramcc 抄送人(多个抄送人用英文逗号“,”隔开)
*@parambcc 暗送人(多个暗送人用英文逗号“,”隔开)
*@paramfileList 附件
*@throwsException*/
public static void sendEmail(String subject, String content, String to, String cc, String bcc, List fileList) throwsException{
Message msg=createMessage(subject, content, to, cc, bcc, fileList);//连接邮件服务器、发送邮件
Transport.send(msg);
}/*** user: Rex
* date: 2016年12月25日 上午7:02:07
*@paramsubject 邮件标题
*@paramcontent 邮件内容
*@paramto 收件人(多个收件人用英文逗号“,”隔开)
*@paramcc 抄送人(多个抄送人用英文逗号“,”隔开)
*@parambcc 暗送人(多个暗送人用英文逗号“,”隔开)
*@paramfileList 附件
*@return邮箱对象
*@throwsException*/
private static Message createMessage(String subject, String content, String to, String cc, String bcc, List fileList) throwsException{
Message msg=createMessage(subject, content, to, RecipientType.CC, cc, fileList);
msg.setRecipients(RecipientType.BCC, InternetAddress.parse(bcc));
msg.setSentDate(new Date()); //设置信件头的发送日期
returnmsg;
}/*** user: Rex
* date: 2016年12月25日 上午7:02:07
*@paramsubject 邮件标题
*@paramcontent 邮件内容
*@paramto 收件人(多个收件人用英文逗号“,”隔开)
*@paramotherRecipient 抄送人或暗送人(多个抄送人或暗送人用英文逗号“,”隔开)
*@paramfileList 附件
*@return邮箱对象
*@throwsException*/
private static Message createMessage(String subject, String content, String to, RecipientType type, String otherRecipient, List fileList) throwsException{
Message msg=createMessage(subject, content, to, fileList);
msg.setRecipients(type, InternetAddress.parse(otherRecipient));returnmsg;
}/*** user: Rex
* date: 2016年12月25日 上午7:02:07
*@paramsubject 邮件标题
*@paramcontent 邮件内容
*@paramto 收件人(多个收件人用英文逗号“,”隔开)
*@paramfileList 附件
*@return邮箱对象
*@throwsException*/
private static Message createMessage(String subject, String content, String to, List fileList) throwsException{
checkEmail(subject, content, fileList);//邮件内容
Multipart mp =createMultipart(content, fileList);
Message msg= newMimeMessage(createSession());
msg.setFrom(newInternetAddress(EmailConfig.getFrom()));
msg.setSubject(subject);
msg.setRecipients(RecipientType.TO, InternetAddress.parse(to));
msg.setContent(mp);//Multipart加入到信件
msg.setSentDate(new Date()); //设置信件头的发送日期
returnmsg;
}/*** user: Rex
* date: 2016年12月25日 上午9:01:12
*@paramcontent 邮件正文内容
*@paramfileList 附件
*@return邮件内容对象
*@throwsMessagingException
*@throwsUnsupportedEncodingException
* Multipart
* TODO 创建邮件正文*/
private static Multipart createMultipart(String content, List fileList) throwsMessagingException, UnsupportedEncodingException{//邮件内容
Multipart mp = newMimeMultipart();
MimeBodyPart mbp= newMimeBodyPart();
mbp.setContent(content,"text/html;charset=gb2312");
mp.addBodyPart(mbp);if(fileList!=null && fileList.size()>0){//附件
FileDataSource fds;for(File file : fileList){
mbp=newMimeBodyPart();
fds= new FileDataSource(file);//得到数据源
mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart
mbp.setFileName(MimeUtility.encodeText(file.getName())); //得到文件名同样至入BodyPart
mp.addBodyPart(mbp);
}
}returnmp;
}/*** user: Rex
* date: 2016年12月25日 上午9:48:18
*@paramtitle 邮件标题
*@paramcontent 邮件正文
*@paramfileList 邮件附件
* void
* TODO 校验邮件内容合法性
*@throwsException*/
private static void checkEmail(String subject, String content, List fileList) throwsException{if(StringUtils.isEmpty(subject)){throw new Exception("邮件标题不能为空");
}if(StringUtils.isEmpty(content) && (fileList==null || fileList.size()==0)){throw new Exception("邮件内容不能为空");
}
}/*** user: Rex
* date: 2016年12月25日 上午4:01:47
*@return* Session
* TODO 创建邮箱上下文*/
private staticSession createSession(){returnSession.getDefaultInstance(EmailConfig.getSessionProperties(), EmailAuthenticator.createEmailAuthenticator());
}
}