| | |
| | | import javax.mail.internet.InternetAddress; |
| | | import javax.mail.internet.MimeMessage; |
| | | import java.text.MessageFormat; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Properties; |
| | | import com.sun.mail.util.MailSSLSocketFactory; |
| | | |
| | | public class EmailSend { |
| | | |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | public static void sendArrivalEmail(List<String> revicerList, String msg, String title){ |
| | | EmailConfigService cs = SpringUtil.getBean("emailConfigService", EmailConfigService.class); |
| | | String sender = cs.getZkySmtpUser();/*发送人*/ |
| | | String password = cs.getZkySmtpPwd(); |
| | | |
| | | String ZkySmtp = "smtp.cstnet.cn"; /*中科院邮件服务器*/ |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | Properties prop = new Properties(); |
| | | prop.setProperty("mail.host", ZkySmtp); |
| | | prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议 |
| | | prop.setProperty("mail.smtp.auth", "true"); // 需要验证用户名密码 |
| | | prop.setProperty("mail.smtp.port", "25"); |
| | | |
| | | MailSSLSocketFactory mailSSLSocketFactory = null; |
| | | try{ |
| | | mailSSLSocketFactory = new MailSSLSocketFactory(); |
| | | mailSSLSocketFactory.setTrustAllHosts(true); |
| | | }catch (Exception e){ |
| | | logger.error("邮件开启 MailSSLSocketFactory 失败:"+e.getMessage()); |
| | | } |
| | | |
| | | if (mailSSLSocketFactory != null) { |
| | | |
| | | prop.put("mail.smtp.ssl.enable", false); |
| | | prop.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory); |
| | | //创建定义整个应用程序所需的环境信息的 Session 对象 |
| | | Session session = Session.getDefaultInstance(prop, new Authenticator() { |
| | | public PasswordAuthentication getPasswordAuthentication() { |
| | | //发件人邮件用户名、授权码 |
| | | return new PasswordAuthentication(sender, password); |
| | | } |
| | | }); |
| | | |
| | | for (String revicer : revicerList) { |
| | | try { |
| | | //2、通过session得到transport对象 |
| | | Transport ts = session.getTransport(); |
| | | //3、使用邮箱的用户名和授权码连上邮件服务器 |
| | | ts.connect(sender, password); |
| | | try { |
| | | //4、创建邮件 |
| | | //创建邮件对象 |
| | | MimeMessage message = new MimeMessage(session); |
| | | //指明邮件的发件人 |
| | | message.setFrom(new InternetAddress(sender)); |
| | | //指明邮件的收件人 |
| | | message.setRecipient(Message.RecipientType.TO, new InternetAddress(revicer)); |
| | | //邮件的标题 |
| | | message.setSubject(sdf.format(new Date())+title); |
| | | //邮件的文本内容 |
| | | message.setContent(msg, "text/html;charset=UTF-8"); |
| | | //5、发送邮件 |
| | | ts.sendMessage(message, message.getAllRecipients()); |
| | | } catch (Exception e) { |
| | | logger.error("邮件发送" + "失败:" + e.getMessage()); |
| | | } |
| | | ts.close(); |
| | | } catch (Exception e) { |
| | | logger.error("邮件发送失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | } |
| | | logger.error("邮件发送完成" + "时间:" + sdf.format(new Date())); |
| | | } |
| | | } |
| | | |