1 packagecom.dzf.utils;2
3 importjava.io.File;4 importjava.io.IOException;5 importjava.io.InputStream;6 importjava.text.SimpleDateFormat;7 importjava.util.ArrayList;8 importjava.util.Date;9 importjava.util.List;10 importjava.util.Properties;11
12 importjavax.activation.DataHandler;13 importjavax.activation.FileDataSource;14 importjavax.mail.Message;15 importjavax.mail.Session;16 importjavax.mail.Transport;17 importjavax.mail.internet.InternetAddress;18 importjavax.mail.internet.MimeBodyPart;19 importjavax.mail.internet.MimeMessage;20 importjavax.mail.internet.MimeMultipart;21
22 importorg.slf4j.Logger;23 importorg.slf4j.LoggerFactory;24
25 /**
26 * 邮件发送工具类27 *@authorxxx28 * @date 2018年1月22日29 * @time 22:43:4030 */
31 public class SendEmailUtil implementsRunnable {32 private static Logger log = LoggerFactory.getLogger(SendEmailUtil.class);33 private static Properties prop = newProperties();34 private List toEmilList = new ArrayList();//用于指定收件人的邮箱
35 private List ccEmilList = new ArrayList();//用于抄送的收件人的邮箱
36 public ListgetCcEmilList() {37 returnccEmilList;38 }39 public void setCcEmilList(ListccEmilList) {40 this.ccEmilList =ccEmilList;41 }42 public SendEmailUtil(List toEmilList, ListccEmilList) {43 super();44 this.toEmilList =toEmilList;45 this.ccEmilList =ccEmilList;46 }47 public ListgetToEmilList() {48 returntoEmilList;49 }50 public void setToEmilList(ListemilList) {51 this.toEmilList =emilList;52 }53 public SendEmailUtil(ListemilList) {54 super();55 this.toEmilList =emilList;56 }57
58 publicSendEmailUtil() {59 super();60 }61 static{62 InputStream in = SendEmailUtil.class.getClassLoader().getResourceAsStream("emil_config.properties");63 try{64 prop.load(in);65 } catch(IOException e) {66 e.printStackTrace();67 log.error("读取配置失败,异常信息{}",e.toString());68 throw new RuntimeException("读取邮件配置文件失败");69 }70 }71 @Override72 public voidrun() {73 try{74 log.info("邮件创建开始了。。。。");75 //1.创建session
76 Session session =Session.getInstance(prop);77 //2.开启session的debug模式,可以查看出email发送的情况
78 session.setDebug(true);79 //3.连接发件服务器
80 Transport trans =session.getTransport();81 trans.connect(prop.getProperty("mail.host"),prop.getProperty("mail.name"), prop.getProperty("mail.password"));82 //4.创建邮件
83 Message message =createMessageMail(session);84 //发送邮件
85 trans.sendMessage(message, message.getAllRecipients());86 trans.close();87 } catch(Exception e) {88 log.error("创建邮件发生异常:异常为{}",e.toString());89 e.printStackTrace();90 }91 }92 /**
93 * 组装邮件94 *@paramsession95 */
96 private Message createMessageMail(Session session)throwsException {97 //创建邮件对象
98 MimeMessage mimeMessage = newMimeMessage(session);99 //设置邮件的基本信息100 //发件人
101 mimeMessage.setFrom(new InternetAddress(prop.getProperty("mail.from")));102 //收件人
103 InternetAddress[] addressTo = newInternetAddress[toEmilList.size()];104 for(int i =0 ;i
108 InternetAddress[] addressCc = newInternetAddress[ccEmilList.size()];109 for(int i =0 ;i0){114 mimeMessage.setRecipients(Message.RecipientType.CC, addressCc);115 }116 //邮件标题
117 mimeMessage.setSubject("私人定制-系统通知");118
119 //邮件正文
120 MimeBodyPart bodyPart = newMimeBodyPart();121 SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");122 Date date = newDate();123 String str =simple.format(date);124 bodyPart.setContent("亲爱得用户:
你好,你与"+str+"登录私人定制,如果不是本人登录,你的密码可能已经泄露!请立即前往官网修改密码。如果是你非别人登录,请你忽略此消息!谢谢!
","text/html;charset=utf-8");125 //TODO添加附件,图片之类126 MimeBodyPart attach = newMimeBodyPart();127 File file = new File("e:/generatorConfig.xml");128 DataHandler dh= new DataHandler(newFileDataSource(file));129 attach.setDataHandler(dh);130
131 //描述数据关系
132 MimeMultipart multipart = newMimeMultipart();133 multipart.addBodyPart(bodyPart);134 multipart.addBodyPart(attach);135 mimeMessage.setContent(multipart);136 mimeMessage.saveChanges();137 returnmimeMessage;138 }139 public static voidmain(String[] args) {140 List toEmilList = new ArrayList();141 toEmilList.add("xxxx23319@qq.com");142 toEmilList.add("xxxx39686@qq.com");143 List ccEmilList = new ArrayList();144 ccEmilList.add("xxxx79285@qq.com");145 SendEmailUtil email = newSendEmailUtil(toEmilList, ccEmilList);146 new Thread(email).start();//启动一个线程
147 }148 }