Java 无法连接到SMTP主机:SMTP.gmail.com,端口:465,响应:-1
发送邮件时,我收到此错误 java.lang.RuntimeException:javax.mail.SendFailedException:Sending 失败;嵌套异常为:类javax.mail.MessaginException: 无法连接到SMTP主机:SMTP.gmail.com,端口:465,响应: -一, 我的代码是:Java 无法连接到SMTP主机:SMTP.gmail.com,端口:465,响应:-1,java,smtp,gmail,Java,Smtp,Gmail,发送邮件时,我收到此错误 java.lang.RuntimeException:javax.mail.SendFailedException:Sending 失败;嵌套异常为:类javax.mail.MessaginException: 无法连接到SMTP主机:SMTP.gmail.com,端口:465,响应: -一, 我的代码是: Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com")
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);
任何帮助都将不胜感激
提前感谢。端口465用于“SSL上的smtp”
您需要告诉它您正在使用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;
}
在使用NetBeans进行调试时,甚至在执行实际的jar文件时,我都遇到过这个问题。防病毒软件会阻止发送电子邮件。您应该在调试期间暂时禁用防病毒软件,或者从扫描中排除NetBeans和实际的jar文件。就我而言,我使用的是Avast 请参阅此链接,了解如何排除:
这对我来说很有用。我所做的就是把
props.put("mail.smtp.starttls.enable","true");
因为显然对于G-mail来说你并不需要它。然后,如果您还没有这样做,您需要在G-mail中为您的程序创建一个应用程序密码。我这样做了,效果非常好。在此,此链接将向您展示如何: 在我的例子中,是Avast Antivirus干扰了连接。 禁用此功能的操作:
Avast->设置->组件->邮件屏蔽(自定义)->SSL扫描->取消选中“扫描SSL连接” 检查我尝试了相同的代码,但仍然得到错误:javax.mail.MessaginException:530 5.7.0必须首先发出STARTTLS命令。4sm28216799pbn.23-gsmtp在com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020)在com.sun.mail.SMTPTransport.mailFrom(SMTPTransport.java:716)在com.sun.mail.SMTPTransport.sendMessage(SMTPTransport.java:388)在rapid.mail.mail(mail.java:60)上starttls的这一行就是为什么我会遇到同样的错误:properties.put(“mail.smtp.starttls.enable”,“true”);尝试使用最新的jar版本JavaMail 1.4.7 No progress same Error将端口号更改为587。能否更详细地描述SMTP响应与防病毒软件之间的连接?@carlodurso:antivirus(在我的例子中是AVAST)具有阻止因怀疑安全威胁而使用端口进出的程序的功能。所以最好不要阻止你的jar文件。下面是一个例外。你能告诉我哪里失败了吗javax.mail.SendFailedException:发送失败;嵌套异常为:类javax.mail.MessaginException:无法连接到SMTP主机:SMTP.gmail.com,端口:587;嵌套的异常是:java.net.ConnectException:连接超时:在javax.mail.Transport.send0(Transport.java:218)在javax.mail.Transport.send(Transport.java:80)在sendMail.SendMail4.execute(SendMail4.java:70)在sendMail.sel.main(sel.java:10)连接。谢谢。我不知道什么是
d_uname
字段实用程序。另一方面,在连接指令中,我们应该传递电子邮件而不是用户名:transport.connect(d_host,Integer.valueOf(d_port),d_email,d_password)代码>。此外,您最好在代码中显示类SMTPAuthenticator
来自何处的包,或者在答案上指定所需的API。
props.put("mail.smtp.starttls.enable","true");