1.jar包依赖

  1. <dependency>
  2. <groupId>javax.mail</groupId>
  3. <artifactId>javax.mail-api</artifactId>
  4. <version>1.5.6</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.commons</groupId>
  8. <artifactId>commons-email</artifactId>
  9. <version>1.4</version>
  10. </dependency>

2.sendMailUtil类

  1. public class SendMailUtil {
  2. private static String mailFrom = "";
  3. private static String mailPort = "465";
  4. private static String mailHost = "smtp.263.net";
  5. private static String mailUsername = "";//发件人的邮箱
  6. private static String mailPassword = "";//发件人密码认证(qq邮箱、163等需要授权码)
  7. public Authenticator createAuthenticator() {
  8. Authenticator authenticator = new Authenticator() {
  9. @Override
  10. protected PasswordAuthentication getPasswordAuthentication() {
  11. //发件人邮箱账号
  12. //String userId = props.getProperty("userId", "jiangmo01@outlook.com");
  13. //发件人邮箱密码(qq、163等邮箱用的是授权码,outlook是密码)
  14. //String password = props.getProperty("password", "Shiyueshiriqing");
  15. return new PasswordAuthentication(mailUsername, mailPassword);
  16. }
  17. };
  18. return authenticator;
  19. }
  20. public Properties createProperties() {
  21. Properties props = new Properties();
  22. //发件人
  23. props.getProperty("fromEmail", mailFrom);
  24. props.put("mail.smtp.port", mailPort);
  25. props.put("mail.smtp.host", mailHost);
  26. //当前smtp host设为可信任 否则抛出javax.mail.MessagingException: Could not convert socket to TLS
  27. props.put("mail.smtp.ssl.trust", mailHost);
  28. props.put("mail.transport.protocol", "smtp");
  29. props.put("mail.smtp.auth", "true");
  30. props.put("mail.smtp.starttls.enable", "true");
  31. props.put("mail.smtp.ssl", "true");
  32. //开启debug调试,控制台会打印相关信息
  33. props.put("mail.debug", "true");
  34. props.put("mail.smtp.socketFactory.fallback", "true");
  35. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  36. props.put("mail.smtp.ssl.protocols", "TLSv1.2");
  37. return props;
  38. }
  39. public boolean sendOutlookMail(String receiver, String subject, String content) {
  40. Properties props = createProperties();
  41. //收件人
  42. props.getProperty("toEmail", receiver);//对收件人重新赋值了
  43. Session session = Session.getInstance(props, createAuthenticator());
  44. try {
  45. MimeMessage message = new MimeMessage(session);
  46. message.setFrom(new InternetAddress(mailUsername));
  47. List list = new ArrayList();//不能使⽤string类型的类型,这样只能发送⼀个收件⼈
  48. String []median=receiver.split(",");//对输⼊的多个邮件进⾏逗号分割
  49. for(int i=0;i<median.length;i++){
  50. list.add(new InternetAddress(median[i]));
  51. }
  52. // InternetAddress[] address =(InternetAddress[])list.toArray(new InternetAddress[list.size()]);
  53. // InternetAddress[] address = InternetAddress.parse(list.toString());
  54. InternetAddress[] address = InternetAddress.parse(receiver);
  55. message.setRecipients(Message.RecipientType.TO, address);
  56. //标题
  57. message.setSubject(subject);
  58. MimeMultipart multipart = new MimeMultipart();
  59. MimeBodyPart mimeBodyPart = new MimeBodyPart();
  60. //String emailText = getHtmlContent(sendEmailApi.getTemplateContent(tempValue),tempMap);
  61. //文本类型正文
  62. //mimeBodyPart.setText("尊敬的张三:\r您好!\r特邀您...");
  63. /*String emailText = "<h3>这是一个大标题</h3>\n" +
  64. "<br/>\n" +
  65. "<p>写点什么好呢</p>";*/
  66. //html类型正文
  67. mimeBodyPart.setContent(content, "text/html;charset=UTF-8");
  68. //添加正文
  69. multipart.addBodyPart(mimeBodyPart);
  70. message.setContent(multipart);
  71. message.setSentDate(new Date());
  72. message.saveChanges();
  73. Transport.send(message);
  74. System.out.println("邮件发送结束");
  75. return true;
  76. } catch (MessagingException me) {
  77. System.out.println("错误信息:" + me.getMessage());
  78. return false;
  79. } catch (Exception ex) {
  80. System.out.println("错误信息:" + ex.getMessage());
  81. return false;
  82. }
  83. }
  84. }

3.测试邮件发送

  1. public static void main(String[] args) {
  2. try {
  3. SendMailUtil mail = new SendMailUtil();
  4. boolean bool = mail.sendOutlookMail("**@126.com", "测试邮件发送"
  5. , "您好,这是一封测试邮件,请忽略");
  6. } catch (Exception e) {
  7. System.out.println("发送邮件失败!");
  8. }
  9. }