MailUtil.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.szwl.model.utils;
  2. import javax.activation.DataHandler;
  3. import javax.activation.FileDataSource;
  4. import javax.mail.*;
  5. import javax.mail.internet.*;
  6. import java.io.UnsupportedEncodingException;
  7. import java.util.Properties;
  8. /**
  9. * 阿里云邮件发送
  10. */
  11. public class MailUtil {
  12. private static final String ALIDM_SMTP_HOST = "smtp.mxhichina.com";
  13. // private static final String ALIDM_SMTP_HOST = "smtp.qiye.aliyun.com";
  14. // private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com";
  15. private static final int ALIDM_SMTP_PORT = 25;// 或80
  16. // private static final String SENDER_NAME = "Magic Candy";
  17. // 发件人的账号 和 密码
  18. private String user;
  19. private String password;
  20. public MailUtil() {
  21. // this("AfterSalesInfo@sunzee.net", "Sz123456");
  22. // this("magic_candy_sunzee@sunzee.net", "Sz123456");
  23. this("magiccandy020@magiccandy.cn", "Sunzee020");
  24. }
  25. public MailUtil(String user, String password) {
  26. this.user = user;
  27. this.password = password;
  28. }
  29. // public static void main(String[] args) {
  30. // new MailUtil().send("520283710@qq.com", "测试1", "nihao显示");
  31. //new MailUtil().send("1161588342@qq.com", "测试1", "市劳动纠纷联赛积分了","C:/Users/guo/Desktop/Proguard.xml");
  32. // }
  33. /**
  34. * 发送邮件
  35. *
  36. * @param toEmail 收件人邮箱地址
  37. * @param subject 邮件标题
  38. * @param content 邮件内容 可以是html内容
  39. */
  40. public void send(String toEmail, String subject, String content) {
  41. Session session = loadMailSession();
  42. // session.setDebug(true);
  43. // 创建邮件消息
  44. MimeMessage message = new MimeMessage(session);
  45. try {
  46. // 设置发件人
  47. message.setFrom(new InternetAddress(user));
  48. // message.setFrom(new InternetAddress(user, SENDER_NAME));
  49. Address[] a = new Address[1];
  50. a[0] = new InternetAddress(user);
  51. message.setReplyTo(a);
  52. // 设置收件人
  53. InternetAddress to = new InternetAddress(toEmail);
  54. message.setRecipient(MimeMessage.RecipientType.TO, to);
  55. // 设置邮件标题
  56. message.setSubject(subject);
  57. // 设置邮件的内容体
  58. message.setContent(content, "text/html;charset=UTF-8");
  59. // 发送邮件
  60. Transport.send(message);
  61. } catch (MessagingException e) {
  62. String err = e.getMessage();
  63. // 在这里处理message内容, 格式是固定的
  64. System.out.println(err);
  65. }
  66. }
  67. /**
  68. * 发送邮件 带附件
  69. *
  70. * @param toEmail 收件人邮箱地址
  71. * @param subject 邮件标题
  72. * @param content 邮件内容 可以是html内容
  73. * @param attachPath 附件路径
  74. */
  75. public void send(String toEmail, String subject, String content, String attachPath) {
  76. Session session = loadMailSession();
  77. MimeMessage mm = new MimeMessage(session);
  78. try {
  79. //发件人
  80. mm.setFrom(new InternetAddress(user));
  81. // mm.setFrom(new InternetAddress(user, SENDER_NAME));
  82. //收件人
  83. mm.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail)); // 设置收件人
  84. // mm.setRecipient(Message.RecipientType.CC, new
  85. // InternetAddress("XXXX@qq.com")); //设置抄送人
  86. //标题
  87. mm.setSubject(subject);
  88. //内容
  89. Multipart multipart = new MimeMultipart();
  90. //body部分
  91. BodyPart contentPart = new MimeBodyPart();
  92. contentPart.setContent(content, "text/html;charset=utf-8");
  93. multipart.addBodyPart(contentPart);
  94. //附件部分
  95. BodyPart attachPart = new MimeBodyPart();
  96. FileDataSource fileDataSource = new FileDataSource(attachPath);
  97. attachPart.setDataHandler(new DataHandler(fileDataSource));
  98. attachPart.setFileName(MimeUtility.encodeText(fileDataSource.getName()));
  99. multipart.addBodyPart(attachPart);
  100. mm.setContent(multipart);
  101. Transport.send(mm);
  102. } catch (Exception e) {
  103. String err = e.getMessage();
  104. // 在这里处理message内容, 格式是固定的
  105. System.out.println(err);
  106. }
  107. }
  108. private Session loadMailSession() {
  109. try {
  110. // 配置发送邮件的环境属性
  111. final Properties props = new Properties();
  112. // 表示SMTP发送邮件,需要进行身份验证
  113. props.put("mail.smtp.auth", "true");
  114. props.put("mail.smtp.host", ALIDM_SMTP_HOST);
  115. // props.put("mail.smtp.port", ALIDM_SMTP_PORT);
  116. // 如果使用ssl,则去掉使用25端口的配置,进行如下配置,
  117. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  118. props.put("mail.smtp.socketFactory.port", "465");
  119. props.put("mail.smtp.port", "465");
  120. // 发件人的账号
  121. props.put("mail.user", user);
  122. // 访问SMTP服务时需要提供的密码
  123. props.put("mail.password", password);
  124. // 构建授权信息,用于进行SMTP进行身份验证
  125. Authenticator authenticator = new Authenticator() {
  126. @Override
  127. protected PasswordAuthentication getPasswordAuthentication() {
  128. // 用户名、密码
  129. String userName = props.getProperty("mail.user");
  130. String password = props.getProperty("mail.password");
  131. return new PasswordAuthentication(userName, password);
  132. }
  133. };
  134. // 使用环境属性和授权信息,创建邮件会话
  135. return Session.getInstance(props, authenticator);
  136. } catch (Exception e) {
  137. e.printStackTrace();
  138. System.out.println("mail session is null");
  139. }
  140. return null;
  141. }
  142. }