今天出现一个奇怪的问题, 本地的邮件能正常发送. 代码上传到阿里云服务器上就是不能成功.
在阿里云社区上看到这么一篇帖子, https://yq.aliyun.com/ask/47591
后台发现写的代码没有使用ssl的连接方式.
Properties props = new Properties();
// 开启debug调试
// props.setProperty("mail.debug", "true");
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件服务器主机名
props.setProperty("mail.host", "smtp.9meth.com");
// 发送邮件协议名称
//props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
// 设置环境信息
// Session session = Session.getInstance(props);
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("mail", "password");
}
});
// 创建邮件对象
Message msg = new MimeMessage(session);
try {
msg.setSubject(title);
// 设置邮件内容
msg.setText(content);
// 设置发件人
msg.setFrom(new InternetAddress("发的邮件"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(收的邮件, false));
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
}