SendMailUtils.java 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.fdkankan.manage.util;
  2. import com.sun.mail.util.MailSSLSocketFactory;
  3. import org.apache.commons.lang3.StringUtils;
  4. import javax.activation.DataHandler;
  5. import javax.activation.DataSource;
  6. import javax.activation.FileDataSource;
  7. import javax.mail.*;
  8. import javax.mail.internet.*;
  9. import java.io.UnsupportedEncodingException;
  10. import java.security.GeneralSecurityException;
  11. import java.util.Properties;
  12. /**
  13. * Created by Hb_zzZ on 2020/3/16.
  14. */
  15. public class SendMailUtils {
  16. /**
  17. * 发送带附件的邮件
  18. * @param from 发件人
  19. * @param pass 发件人密码
  20. * @param host 发件人主机
  21. * @param receive 收件人
  22. * @param subject 邮件主题
  23. * @param msg 邮件内容
  24. * @param filename 附件地址
  25. *
  26. */
  27. public static boolean sendMail(String from,String pass,String host,
  28. String receive, String subject, String msg, String filename) {
  29. if (StringUtils.isEmpty(receive)) {
  30. return false;
  31. }
  32. try {
  33. // 获取系统属性
  34. Properties properties = System.getProperties();
  35. // 设置邮件服务器
  36. properties.setProperty("mail.smtp.host", host);
  37. properties.setProperty("mail.debug", "true");
  38. properties.put("mail.smtp.auth", "true");
  39. MailSSLSocketFactory sf = new MailSSLSocketFactory();
  40. sf.setTrustAllHosts(true);
  41. properties.put("mail.smtp.ssl.enable", "true");
  42. properties.put("mail.smtp.ssl.socketFactory", sf);
  43. // 获取默认session对象
  44. Session session = Session.getDefaultInstance(properties, new Authenticator() {
  45. public PasswordAuthentication getPasswordAuthentication() { // qq邮箱服务器账户、第三方登录授权码
  46. return new PasswordAuthentication(from, pass); // 发件人邮件用户名、密码
  47. }
  48. });
  49. // 创建默认的 MimeMessage 对象
  50. MimeMessage message = new MimeMessage(session);
  51. // Set From: 头部头字段
  52. message.setFrom(new InternetAddress(from));
  53. // Set To: 头部头字段
  54. message.addRecipient(Message.RecipientType.TO, new InternetAddress(receive));
  55. // Set Subject: 主题文字
  56. message.setSubject(subject);
  57. // 创建消息部分
  58. BodyPart messageBodyPart = new MimeBodyPart();
  59. // 消息
  60. messageBodyPart.setText(msg);
  61. // 创建多重消息
  62. MimeMultipart multipart = new MimeMultipart("mixed");
  63. if(StringUtils.isNotEmpty(filename)){
  64. // 附件部分
  65. messageBodyPart = new MimeBodyPart();
  66. // 设置要发送附件的文件路径
  67. DataSource source = new FileDataSource(filename);
  68. messageBodyPart.setDataHandler(new DataHandler(source));
  69. // messageBodyPart.setFileName(filename);
  70. // 处理附件名称中文(附带文件路径)乱码问题
  71. messageBodyPart.setFileName(MimeUtility.encodeText(filename));
  72. multipart.addBodyPart(messageBodyPart);
  73. }
  74. //html代码部分
  75. MimeBodyPart htmlPart = new MimeBodyPart();
  76. multipart.addBodyPart(htmlPart);
  77. //html代码
  78. htmlPart.setContent(msg, "text/html;charset=utf-8");
  79. // 发送完整消息
  80. message.setContent(multipart);
  81. // 发送消息
  82. Transport.send(message, new Address[]{new InternetAddress(receive)});
  83. // System.out.println("Sent message successfully....");
  84. return true;
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. return false;
  89. }
  90. }