JavaMailUtils.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package com.zhongzheng.common.utils;
  2. import javax.mail.Authenticator;
  3. import javax.mail.PasswordAuthentication;
  4. import javax.mail.Session;
  5. import java.util.Properties;
  6. /**
  7. * @author yangdamao
  8. * @date 2023年06月19日 10:11
  9. */
  10. public class JavaMailUtils {
  11. public static Session createsession(String postAccount,String postPassword,String STMPserver,String post) {
  12. // SMTP服务器地址
  13. String smtp = STMPserver;
  14. // 邮箱账号和密码(授权密码)
  15. String userName = postAccount;
  16. String password = postPassword;
  17. // SMTP服务器的连接信息
  18. Properties props = new Properties();
  19. props.put("mail.smtp.host", smtp); // SMTP主机号
  20. props.put("mail.smtp.port", post); // 主机端口号
  21. props.put("mail.smtp.auth", "true"); // 是否需要认证
  22. props.put("mail.smtp.starttls.enable", "true"); // 启用TLS加密
  23. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  24. // 创建Session
  25. // 参数1:SMTP服务器的连接信息
  26. // 参数2:用户认证对象(Authenticator接口的匿名实现类)
  27. Session session = Session.getInstance(props, new Authenticator() {
  28. @Override
  29. protected PasswordAuthentication getPasswordAuthentication() {
  30. return new PasswordAuthentication(userName, password);
  31. }
  32. });
  33. // 开启调试模式
  34. session.setDebug(true);
  35. return session;
  36. }
  37. }