我想知道如何从java程序发送邮件。我写的java邮件程序没有错。但我得到了未知的SMTP端口465,即使我使用g邮件服务器端口465。 我想知道是否有任何选项需要在我的电脑中启用。 请一步一步告诉我需要什么 我正在使用Windows 7并希望使用gmail服务器..
你的努力将非常感谢。
请告诉我以下任何错误?
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
at mail1.SendMailSSL.main(SendMailSSL.java:44)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:310)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at mail1.SendMailSSL.main(SendMailSSL.java:39)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1611)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:187)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:181)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1035)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:124)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:516)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:454)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:884)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1112)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1139)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1123)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:503)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:234)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
答案 0 :(得分:4)
这个解决方案非常适合我。您需要将GMail smtp服务器证书导入您的信任库
使用openssl命令获取Gmail smtp服务器证书
“s_client -connect smtp.gmail.com:465”
将“----- BEGIN CERTIFICATE -----”和“----- END CERTIFICATE -----”(包括该前缀/后缀)之间的输出保存到文件“gmail”中。证书“
将此证书文件导入您的密钥库(对于Linux,它将需要sudo)
keytool -import -alias smtp.gmail.com -keystore“%JAVA_HOME%/ jre / lib / security / cacerts”-file gmail.cert
重新启动您的Java应用以传播更改
致:http://notepad2.blogspot.com/2012/04/import-gmail-certificate-into-java.html
OpenSSL可以在这里下载:http://gnuwin32.sourceforge.net/packages/openssl.htm
答案 1 :(得分:0)
尝试使用命令telnet smtp.gmail.com 465
查看您是否可以连接。如果你要连接,这应该给你一个指示。同时尝试端口587(Submission / StartTLS),它应该提供一个可见的标题。
Windows 7上默认不安装Telnet。您应该能够使用命令pkgmgr /iu:"TelnetClient"
从命令行安装它。
连接后,您需要使用gmail ID和密码进行身份验证才能发送邮件。看来你的代码还没有连接。
答案 2 :(得分:0)
通过将调试选项添加到获取会话时传递的任何道具来查找其他线索。我想你会看到你实际连接到gmail的后端,因为你的异常是在send()期间,而不是在connect()期间,这意味着你使用的主机/端口/ id / psw都可能正确(除非有某种排序)懒惰连接的发生。)
props.put("mail.debug", "" + true); // Enable debug mode
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpId, psw);
}
});
您的调试输出应如下所示:
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
9 220 mx.google.com ESMTP cpxgxgxgxgxgxgb.3
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
答案 3 :(得分:0)
您使用的是SSLSocket吗?
SSLSocket s = (SSLSocket) ((SSLSocketFactory)
SSLSocketFactory.getDefault()).
createSocket(InetAddress.getByName("smtp.gmail.com"), 465);