发送邮件时出现此错误
java.lang.RuntimeException: javax.mail.SendFailedException: 发送失败;嵌套异常是:类 javax.mail.MessagingException:无法连接到 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);
任何帮助,将不胜感激。
提前致谢。