开启465端口 我采取的方法是配置文件读取这些信息
mailServerHost=smtp.xxx.com 发送邮件使用的邮箱类型
mailServerPort=465 端口号
MyAuthenticator authenticator = null;
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器 发送邮件使用的邮箱账号和密码
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
邮箱会话设置
Properties p = new Properties();
p.setProperty("mail.smtp.host",this.mailServerHost);p.setProperty("mail.smtp.port",this.mailServerPort);
p.put("mail.smtp.auth", "false");
//465端口开启SSL 最关键的一步
p.put("mail.smtp.ssl.enable", "true");
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(p, authenticator);
完成后本地测试没有问题,阿里云服务器测试也没有问题。
原帖http://blog.youkuaiyun.com/allen_zs/article/details/50753311