当前位置:   article > 正文

java发送邮件

java发送邮件

  1. <dependency>
  2. <groupId>javax.mail</groupId>
  3. <artifactId>mail</artifactId>
  4. <version>1.4.7</version>
  5. </dependency>
  1. public static void main(String[] args) {
  2. try {
  3. Properties prop = new Properties();
  4. // 设置QQ邮件服务器
  5. prop.setProperty("mail.host", "mail.xxxxx.com");
  6. // 当使用公司内部邮箱,可以设置端口
  7. //prop.setProperty("mail.smtp.port", "25");
  8. // 当使用公司内部邮箱,可以设置服务器
  9. //prop.setProperty("mail.smtp.host", "smtp.qq.com");
  10. // 邮件发送协议
  11. prop.setProperty("mail.transport.protocol", "smtp");
  12. // 需要验证用户名密码
  13. prop.setProperty("mail.smtp.auth", "true");
  14. // QQ邮箱设置SSL加密(可以不改)
  15. MailSSLSocketFactory sf = new MailSSLSocketFactory();
  16. sf.setTrustAllHosts(true);
  17. prop.put("mail.smtp.ssl.enable", "true");
  18. prop.put("mail.smtp.ssl.socketFactory", sf);
  19. //1、创建定义整个应用程序所需的环境信息的 Session 对象
  20. Session session = Session.getDefaultInstance(prop, new Authenticator() {
  21. @Override
  22. protected PasswordAuthentication getPasswordAuthentication() {
  23. //传入发件人的姓名和授权码
  24. return new PasswordAuthentication("xxxxx@xxxxx.com", "xxxxxx");
  25. }
  26. });
  27. //2、通过session获取transport对象
  28. Transport transport = session.getTransport();
  29. //3、通过transport对象邮箱用户名和授权码连接邮箱服务器
  30. transport.connect("mail.xxxx.com", "xxx@xxx.com", "xxxx");
  31. //4、创建邮件,传入session对象
  32. MimeMessage mimeMessage = complexEmail(session);
  33. //邮件发送太慢,有错误地址停止发送
  34. transport.addTransportListener(new CustomTransportListener(transport));
  35. //5、发送邮件
  36. transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
  37. //6、关闭连接
  38. transport.close();
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. public static MimeMessage complexEmail(Session session) throws MessagingException {
  44. //消息的固定信息
  45. MimeMessage mimeMessage = new MimeMessage(session);
  46. //发件人
  47. mimeMessage.setFrom(new InternetAddress(username));
  48. List<ChatGPTMailAddress> mailList = statisticsDao.selectAllMail();
  49. List<String> toList = new ArrayList<>();
  50. List<String> ccList = new ArrayList<>();
  51. if (CollectionUtils.isNotEmpty(mailList)) {
  52. mailList.forEach(o -> {
  53. if ("1".equals(o.getMailType())) {
  54. toList.add(o.getMailAddress());
  55. } else if ("2".equals(o.getMailType())) {
  56. ccList.add(o.getMailAddress());
  57. }
  58. });
  59. }
  60. if (CollectionUtils.isNotEmpty(toList)) {
  61. Address[] tos = new InternetAddress[toList.size()];
  62. for (int i = 0; i < toList.size(); i++) {
  63. tos[i] = new InternetAddress(toList.get(i));
  64. }
  65. //收件人
  66. mimeMessage.setRecipients(Message.RecipientType.TO, tos);
  67. }
  68. if (CollectionUtils.isNotEmpty(ccList)) {
  69. Address[] ccs = new InternetAddress[ccList.size()];
  70. for (int i = 0; i < ccList.size(); i++) {
  71. ccs[i] = new InternetAddress(ccList.get(i));
  72. }
  73. //收件人
  74. mimeMessage.setRecipients(Message.RecipientType.CC, ccs);
  75. }
  76. //邮件标题
  77. mimeMessage.setSubject("儿童对话体验数据分析同步");
  78. //邮件内容
  79. //准备图片数据
  80. // MimeBodyPart image = new MimeBodyPart();
  81. // DataHandler handler = new DataHandler(new
  82. // FileDataSource("D:\\picture\\test1.jpg"));
  83. // image.setDataHandler(handler);
  84. // image.setContentID("test1.jpg"); //设置图片id
  85. String html = HtmlTableTemplate.generateHtml(list, charLength.toString(),questionRate.toString());
  86. //准备文本
  87. MimeBodyPart text = new MimeBodyPart();
  88. text.setContent(html, "text/html;charset=utf-8");
  89. //拼装邮件正文
  90. MimeMultipart mimeMultipart = new MimeMultipart();
  91. // mimeMultipart.addBodyPart(image);
  92. mimeMultipart.addBodyPart(text);
  93. mimeMultipart.setSubType("related");
  94. //将拼装好的正文内容设置为主体
  95. MimeBodyPart contentText = new MimeBodyPart();
  96. contentText.setContent(mimeMultipart);
  97. //拼接附件
  98. MimeMultipart allFile = new MimeMultipart();
  99. //正文
  100. allFile.addBodyPart(contentText);
  101. if (StringUtils.isNotBlank(txtPath)) {
  102. //附件
  103. MimeBodyPart appendix = new MimeBodyPart();
  104. appendix.setDataHandler(new DataHandler(new FileDataSource(txtPath)));
  105. appendix.setFileName("chatGPT统计.txt");
  106. //附件
  107. allFile.addBodyPart(appendix);
  108. }
  109. if (StringUtils.isNotBlank(path)) {
  110. //附件
  111. MimeBodyPart appendix = new MimeBodyPart();
  112. appendix.setDataHandler(new DataHandler(new FileDataSource(path)));
  113. appendix.setFileName("用户统计.xlsx");
  114. //附件
  115. allFile.addBodyPart(appendix);
  116. }
  117. //正文和附件都存在邮件中,所有类型设置为mixed
  118. allFile.setSubType("mixed");
  119. //放到Message消息中
  120. mimeMessage.setContent(allFile);
  121. mimeMessage.saveChanges();//保存修改
  122. return mimeMessage;
  123. }
  1. class CustomTransportListener extends TransportAdapter {
  2. Transport transport;
  3. CustomTransportListener(Transport transport) {
  4. this.transport = transport;
  5. }
  6. @Override
  7. public void messageNotDelivered(TransportEvent transportEvent) {
  8. Address[] addresses = transportEvent.getValidUnsentAddresses();
  9. Message message = transportEvent.getMessage();
  10. try {
  11. transport.sendMessage(message, addresses);
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/728309
推荐阅读
相关标签
  

闽ICP备14008679号