发送邮件时我收到此错误
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);
任何帮助都将不胜感激。
先谢谢。
答案 0 :(得分:14)
您需要告诉它您正在使用SSL:
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
万一你错过了什么,这里有工作代码:
String d_email = "address@gmail.com",
d_uname = "Name",
d_password = "urpassword",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "toAddress@gmail.com",
m_subject = "Indoors Readable File: " + params[0].getName(),
m_text = "This message is from Indoor Positioning App. Required file(s) are attached.";
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SMTPAuthenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
try {
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
Transport transport = session.getTransport("smtps");
transport.connect(d_host, Integer.valueOf(d_port), d_uname, d_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (AddressException e) {
e.printStackTrace();
return false;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
答案 1 :(得分:6)
在使用NetBeans进行调试时,我遇到过这个问题,甚至执行实际的jar文件。防病毒软件会阻止发送电子邮件。您应该在调试期间暂时禁用防病毒软件,或者排除NetBeans并扫描实际的jar文件。就我而言,我正在使用Avast。
请参阅此链接,了解如何排除:How to Add File/Website Exception into avast! Antivirus 2014
它对我有用。
答案 2 :(得分:1)
我所做的是我评论了
props.put("mail.smtp.starttls.enable","true");
因为显然对于G-mail你不需要它。然后,如果您还没有这样做,则需要在G-mail中为您的程序创建一个应用程序密码。我做到了,效果很好。此链接将向您展示如何:https://support.google.com/accounts/answer/185833。
答案 3 :(得分:0)
端口465用于“smtp over SSL”。
http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html
[...] For example, use
props.put("mail.smtp.port", "888");
to set the mail.smtp.port property, which is of type int.
Note that if you're using the "smtps" protocol to access SMTP over SSL,
all the properties would be named "mail.smtps.*"
答案 4 :(得分:0)
就我而言,这是Avast Antivirus干扰了连接。 禁用此功能的操作: Avast->设置->组件-> Mail Shield(自定义)-> SSL扫描->取消选中“扫描SSL连接”。