SendMailUtils.java 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.fdkankan.ucenter.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.File;
  10. import java.util.Properties;
  11. /**
  12. * Created by Hb_zzZ on 2020/3/16.
  13. */
  14. public class SendMailUtils {
  15. /**
  16. * 发送带附件的邮件
  17. * @param from 发件人
  18. * @param pass 发件人密码
  19. * @param host 发件人主机
  20. * @param receive 收件人
  21. * @param subject 邮件主题
  22. * @param msg 邮件内容
  23. * @param filename 附件地址
  24. *
  25. */
  26. public static boolean sendMail(String from,String pass,String host,
  27. String receive, String subject, String msg, String filename) {
  28. if (StringUtils.isEmpty(receive)) {
  29. return false;
  30. }
  31. try {
  32. // 获取系统属性
  33. Properties properties = System.getProperties();
  34. // 设置邮件服务器
  35. properties.setProperty("mail.smtp.host", host);
  36. properties.setProperty("mail.debug", "true");
  37. properties.put("mail.smtp.auth", "true");
  38. MailSSLSocketFactory sf = new MailSSLSocketFactory();
  39. sf.setTrustAllHosts(true);
  40. properties.put("mail.smtp.ssl.enable", "true");
  41. properties.put("mail.smtp.ssl.socketFactory", sf);
  42. // 获取默认session对象
  43. Session session = Session.getDefaultInstance(properties, new Authenticator() {
  44. public PasswordAuthentication getPasswordAuthentication() { // qq邮箱服务器账户、第三方登录授权码
  45. return new PasswordAuthentication(from, pass); // 发件人邮件用户名、密码
  46. }
  47. });
  48. // 创建默认的 MimeMessage 对象
  49. MimeMessage message = new MimeMessage(session);
  50. // Set From: 头部头字段
  51. message.setFrom(new InternetAddress(from));
  52. // Set To: 头部头字段
  53. message.addRecipient(Message.RecipientType.TO, new InternetAddress(receive));
  54. // Set Subject: 主题文字
  55. message.setSubject(subject);
  56. MimeMultipart multipart = new MimeMultipart("mixed");
  57. MimeBodyPart htmlPart = new MimeBodyPart();
  58. htmlPart.setContent(msg, "text/html;charset=utf-8");
  59. multipart.addBodyPart(htmlPart);
  60. if (StringUtils.isNotBlank(filename)) {
  61. File file = new File(filename);
  62. if (file.exists()) {
  63. MimeBodyPart attachmentPart = new MimeBodyPart();
  64. DataSource source = new FileDataSource(file);
  65. attachmentPart.setDataHandler(new DataHandler(source));
  66. attachmentPart.setFileName(
  67. MimeUtility.encodeText(file.getName(), "UTF-8", "B")
  68. );
  69. multipart.addBodyPart(attachmentPart);
  70. }
  71. }
  72. message.setContent(multipart);
  73. Transport.send(message);
  74. return true;
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. }
  78. return false;
  79. }
  80. }