无法连接到 SMTP 主机: smtp.gmail.com, 端口: 465, 响应: -1
发送邮件时,我收到此错误
java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed;嵌套的异常是: 类 javax.mail.Messaging异常: 无法连接到 SMTP 主机: smtp.gmail.com, 端口: 465, 响应: -1
我的代码是:
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("email","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("email"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(this.to));
message.setSubject("Testing");
message.setText("Hey, this is the testing email.");
Transport.send(message);
任何帮助将不胜感激。
提前致谢。