package com.zhongzheng.common.utils; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; import javax.mail.Session; import java.util.Properties; /** * @author yangdamao * @date 2023年06月19日 10:11 */ public class JavaMailUtils { public static Session createsession(String postAccount,String postPassword,String STMPserver,String post) { // SMTP服务器地址 String smtp = STMPserver; // 邮箱账号和密码(授权密码) String userName = postAccount; String password = postPassword; // SMTP服务器的连接信息 Properties props = new Properties(); props.put("mail.smtp.host", smtp); // SMTP主机号 props.put("mail.smtp.port", post); // 主机端口号 props.put("mail.smtp.auth", "true"); // 是否需要认证 props.put("mail.smtp.starttls.enable", "true"); // 启用TLS加密 props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // 创建Session // 参数1:SMTP服务器的连接信息 // 参数2:用户认证对象(Authenticator接口的匿名实现类) Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }); // 开启调试模式 session.setDebug(true); return session; } }