MailUtil.java 6.1 KB

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