Javamail - 无法连接到SMTP主机 [英] Javamail - Could not connect to SMTP host

查看:477
本文介绍了Javamail - 无法连接到SMTP主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的java发送电子邮件,我无法连接到我的主机。
这里我的代码:

I'm trying to send email from my java and I couldn't connect to my host. Here my codes:

public class sendEmail implements Runnable{
     @Override
     public void run(){
        try{
            final String username = "user", password = "pass", from = "from@mail.com", to = "to@mail.com";

            Properties props = new Properties();
            props.setProperty("mail.smtp.user", username);
            props.setProperty("mail.smtp.host", "mail.host.com");
            props.setProperty("mail.smtp.ssl.enable", "true"); 
            props.setProperty("mail.smtp.port", "465");
            props.setProperty("mail.smtp.starttls.enable", "true");
            props.setProperty("mail.smtp.debug", "true");
            props.setProperty("mail.smtp.auth", "true");
            props.setProperty("mail.smtp.socketFactory.port", "465");
            props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, auth);
            session.setDebug(true);

            Message msg = new MimeMessage(session);
            try{
                msg.setSubject("Test SMTP");
                msg.setFrom(new InternetAddress(from));
                msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
                Transport transport = session.getTransport("smtp");
                transport.connect("mail.itbuntuksemua.com", username, password);
                transport.sendMessage(msg, msg.getAllRecipients());
                transport.close();
                System.out.println("Done");
            }catch(MessagingException | NumberFormatException | HeadlessException ex){
                ex.printStackTrace();
            }
        }catch(UnknownHostException | NumberFormatException ex){
            ex.printStackTrace();
        }
     }
 }

这里我的 SMTPAuthenticator()代码:

private class SMTPAuthenticator extends javax.mail.Authenticator {
    @Override
    public PasswordAuthentication getPasswordAuthentication() {
        String username = "user"; 
        String password = "pass";
        return new PasswordAuthentication(username, password);
    } 
}

再次,我的报告错误:


DEBUG:setDebug:JavaMail版本1.4.7
DEBUG:getProvider()返回javax.mail.Provider [TRANSPORT,smtp,com。 sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP:useEhlo true,useAuth true
DEBUG SMTP:尝试连接到主机mail.host.com,端口465,isSSL true
javax.mail.MessagingException:无法连接到SMTP主机:mail.host.com,port:465;
嵌套异常是:
javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到有效的认证路径请求目标
在com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:295)
javax.mail.Service.connect(Service.java:176)
at scam.ccChecker $ sendEmail.run(ccChecker .java:186)
在java.lang.Thread.run(Thread.java:745)
导致:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到有效的认证路径到请求的目标
在sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at s un.security.ssl.SSocket socket.pla fatalSE(Handshaker.java:296)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1478)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:957)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:892)
at sun.security .ssl.SSocketSocketRepord(SSLSocketImpl.java:1050)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl .java:1391)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375)
在com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
在com.sun.mail .util.SocketFetcher.createSocket(SocketFetcher.java:354)
在com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:211)
在com.sun.mail.smtp.SMTPTransport .openServer(SMTPTransport.java:1927)
... 5更多
导致:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)找到请求的目标
的有效证书路径
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator.validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
at sun.security。 ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
a t sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1460)
... 16更多
导致:sun.security.provider.certpath.SunCertPathBuilderException:无法找到有效的认证路径在sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)$ b $ sun $ $ b at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
... 22更多

DEBUG: setDebug: JavaMail version 1.4.7 DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "mail.host.com", port 465, isSSL true javax.mail.MessagingException: Could not connect to SMTP host: mail.host.com, port: 465; nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) at javax.mail.Service.connect(Service.java:295) at javax.mail.Service.connect(Service.java:176) at scam.ccChecker$sendEmail.run(ccChecker.java:186) at java.lang.Thread.run(Thread.java:745) Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1478) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212) at sun.security.ssl.Handshaker.processLoop(Handshaker.java:957) at sun.security.ssl.Handshaker.process_record(Handshaker.java:892) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375) at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:354) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:211) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927) ... 5 more Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387) at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292) at sun.security.validator.Validator.validate(Validator.java:260) at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324) at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229) at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1460) ... 16 more Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145) at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131) at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280) at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382) ... 22 more

有人能帮我吗? :(

推荐答案

您可能想尝试下载证书,使用keytool进行安装,然后将其设置为系统变量:

You might want to try downloading the certificate, install it using keytool, then set them as system variables. Here's how:


  1. 转到Firefox浏览器中的URL,点击HTTPS证书链(URL地址旁边),点击 更多信息>security>show certificate>details>export ..。选择名称并选择文件类型示例。
  2. 找到文件 $ JAVA_HOME / jre / lib,然后将其添加到您的JVM中/ security / cacerts

  3. example.der 文件导入 cacerts 文件使用以下内容:

  1. Go to URL in your firefox browser, click on HTTPS certificate chain (next to URL address). Click "more info" > "security" > "show certificate" > "details" > "export..". Pickup the name and choose file type example.der. Now you have file with keystore and you have to add it to your JVM
  2. Locate the file $JAVA_HOME/jre/lib/security/cacerts
  3. Import the example.der file into the cacerts file using the following:

sudo keytool -import -alias example -keystore  $JAVA_HOME/jre/lib/security/cacerts -file example.der


  • 默认的密钥库密码是' changeit '

    您可以查看更改th在您使用此命令显示证书指纹时。

    You can view the change that you made with this command that shows the Certificate fingerprint.

    keytool -list -keystore cacerts
    


  • 如果这不能解决问题,请尝试添加这些java选项作为参数:

  • If this doesn't solve the problem, try adding these java options as arguments:

    -Djavax.net.ssl.trustStore="$JAVA_HOME/jre/lib/security/cacerts"
    -Djavax.net.ssl.trustStorePassword="changeit"
    


  • p>

  • Or you can put them in as system variables in your java code:

    System.setProperty("javax.net.ssl.trustStore","/usr/lib/jvm/java-1.7.0-openjdk-amd64/jre/lib/security/cacerts");
    System.setProperty("javax.net.ssl.trustStorePassword","changeit");
    


  • 可以在这里找到更多的说明: https://stackoverflow.com/a/36427118/1696153

    More instructions can be found here: https://stackoverflow.com/a/36427118/1696153

    这篇关于Javamail - 无法连接到SMTP主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆