发送邮件时,我收到此错误
java.lang.RuntimeException:javax.mail.SendFailedException:发送失败; 嵌套异常是:class javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,port: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);
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.
提前致谢.