SendEmailUtil.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package com.goafanti.common.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.GeneralSecurityException;
  4. import java.util.Date;
  5. import java.util.Properties;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. import javax.mail.Authenticator;
  9. import javax.mail.Message;
  10. import javax.mail.MessagingException;
  11. import javax.mail.PasswordAuthentication;
  12. import javax.mail.Session;
  13. import javax.mail.Transport;
  14. import javax.mail.internet.InternetAddress;
  15. import javax.mail.internet.MimeMessage;
  16. import javax.mail.internet.MimeUtility;
  17. import com.goafanti.common.bo.EmailBo;
  18. import com.sun.mail.util.MailSSLSocketFactory;
  19. public class SendEmailUtil {
  20. private static Session session;
  21. private static volatile SendEmailUtil singleton;
  22. private SendEmailUtil() {}
  23. public static SendEmailUtil getInstance() {
  24. if (singleton == null) {
  25. synchronized (SendEmailUtil.class) {
  26. if (singleton == null) {
  27. singleton = new SendEmailUtil();
  28. session = getSession();
  29. }
  30. }
  31. }
  32. return singleton;
  33. }
  34. public static final String HOST = "smtp.163.com";
  35. public static final String PORT = "465";
  36. public static final String AUTH_USER_NICKNAME = "湖南科德信息咨询集团有限公司";
  37. public static final String FROM = "13875952633@163.com";// 发件人的email
  38. public static final String PWD = "kede2018";// 发件人密码
  39. public static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  40. private static Session getSession() {
  41. Properties props = new Properties();
  42. //开启安全协议
  43. MailSSLSocketFactory sf = null;
  44. try {
  45. sf = new MailSSLSocketFactory();
  46. sf.setTrustAllHosts(true);
  47. } catch (GeneralSecurityException e) {
  48. e.printStackTrace();
  49. }
  50. props.setProperty("mail.smtp.ssl.enable", "true");
  51. props.put("mail.smtp.ssl.socketFactory", sf);
  52. props.setProperty("mail.smtp.socketFactory.port", PORT);
  53. props.setProperty("mail.smtp.socketFactory.fallback", "false");// 注意是字符串的true,不是boolean类型的true
  54. // 这一套是465端口
  55. props.setProperty("mail.smtp.host", HOST);// 设置服务器地址
  56. props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);// 设置协议
  57. props.setProperty("mail.smtp.port", PORT);// 设置端口
  58. props.put("mail.smtp.auth", "true");// 注意是字符串的true,不是boolean类型的true
  59. Authenticator authenticator = new Authenticator() {
  60. @Override
  61. protected PasswordAuthentication getPasswordAuthentication() {
  62. return new PasswordAuthentication(FROM, PWD);
  63. }
  64. };
  65. Session session = Session.getDefaultInstance(props, authenticator);
  66. System.out.println("session hash code" + session.hashCode());
  67. return session;
  68. }
  69. /**
  70. * 如果要给多个人发,请EmailBo 的 address , addressee用逗号隔开 注意要一一对应且个数相等
  71. * @param emailBo
  72. * @throws MessagingException
  73. * @throws UnsupportedEncodingException
  74. */
  75. public void send(EmailBo emailBo)
  76. throws MessagingException, UnsupportedEncodingException {
  77. // Session session = getSession();
  78. // Instantiate a message
  79. Message msg = new MimeMessage(session);
  80. // Set message attributes
  81. msg.setFrom(new InternetAddress(MimeUtility.encodeText(AUTH_USER_NICKNAME) +"<" + FROM + ">"));
  82. // msg.setFrom(new InternetAddress(FROM));
  83. String[] adds = emailBo.getAddress().split(",");
  84. String[] ees = emailBo.getAddressee().split(",");
  85. for (int i = 0; i < adds.length; i++) {
  86. InternetAddress internetAddress = new InternetAddress(adds[i]);
  87. msg.setRecipient(Message.RecipientType.TO, internetAddress);
  88. msg.setSubject(emailBo.getSubject());
  89. msg.setSentDate(new Date());
  90. msg.setContent(emailBo.format(ees[i]), "text/html;charset=utf-8");
  91. // Send the message
  92. Transport.send(msg);
  93. }
  94. }
  95. /**
  96. * 如果要给多个人发,请EmailBo 的 address , addressee用逗号隔开 注意要一一对应且个数相等
  97. * @param emailBo
  98. * @throws MessagingException
  99. * @throws UnsupportedEncodingException
  100. */
  101. public void patentSend(EmailBo emailBo)
  102. throws MessagingException, UnsupportedEncodingException {
  103. // Session session = getSession();
  104. Message msg = new MimeMessage(session);
  105. // Set message attributes
  106. msg.setFrom(new InternetAddress(MimeUtility.encodeText(AUTH_USER_NICKNAME) +"<" + FROM + ">"));
  107. emailBo.SetEnd("");
  108. // msg.setFrom(new InternetAddress(FROM));
  109. String[] adds = emailBo.getAddress().split(",");
  110. // String[] ees = emailBo.getAddressee().split(",");
  111. for (int i = 0; i < adds.length; i++) {
  112. InternetAddress internetAddress = new InternetAddress(adds[i]);
  113. msg.setRecipient(Message.RecipientType.TO, internetAddress);
  114. msg.setSubject(emailBo.getSubject());
  115. msg.setSentDate(new Date());
  116. msg.setContent(emailBo.getContent(), "text/html;charset=utf-8");
  117. // Send the message
  118. Transport.send(msg);
  119. }
  120. }
  121. public static boolean isEmail(String email){
  122. if (StringUtils.isEmpty(email)) {
  123. return false;
  124. }
  125. //邮箱正则表达式
  126. String pattern = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
  127. Pattern p = Pattern.compile(pattern);
  128. Matcher m = p.matcher(email);
  129. return m.matches();
  130. }
  131. public static void main(String[] args) {
  132. EmailBo emailBo = new EmailBo( "专利提醒失败", "312518615@qq.com", "超管", "平台", "系统", "专利提醒失败");
  133. try {
  134. SendEmailUtil.getInstance().send(emailBo);
  135. } catch (UnsupportedEncodingException | MessagingException e) {
  136. // TODO Auto-generated catch block
  137. e.printStackTrace();
  138. }
  139. }
  140. }