1.用以下代码,邮件发送测试失败
private static Session getSession() {
Properties props = new Properties();
props.put("mail.smtp.host", HOST);//设置服务器地址
props.put("mail.store.protocol" , PROTOCOL);//设置协议
props.put("mail.smtp.port", PORT);//设置端口以下代码加在这个附近就行
props.put("mail.smtp.ssl.enable", true);
props.put("mail.smtp.auth" , true);
/*
Authentication authentication = new Authentication(userName, userPwd);
Session session = Session.getDefaultInstance(props,authentication);*/
Authenticator authenticator = new Authenticator() {@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(FROM, PWD);
}};
//Authenticator authentication = new Authenticator("13631785646@136.com", "admin671435...");
Session session = Session.getDefaultInstance(props , authenticator);return session;
}
2.解决办法
方法一:加上以下这局代码【亲测有效】
props.put("mail.smtp.ssl.enable", true);
方法二:修改发送端口
将465端口改为25端口