电子邮件偶尔发送失败(身份验证javax.mail.MessagingException)

时间:2020-10-08 06:56:05

标签: java email java-8 debian

我有一个使用javax.mail发送电子邮件的应用程序。我正确地进行了配置,并且能够正常发送电子邮件。这里的问题是,在Debian实例下,信任库验证有时会失败并导致失败,最终甚至根本不会发送电子邮件。我使用亚马逊ses电子邮件发送服务。既然代码可以正常工作,我有什么办法可以阻止这种偶然的异常?

异常消息为:

javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors paramete
r must be non-empty
2020-10-07 10:01:47,739         at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2046)
2020-10-07 10:01:47,739         at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:711)
2020-10-07 10:01:47,739         at javax.mail.Service.connect(Service.java:388)
2020-10-07 10:01:47,739         at javax.mail.Service.connect(Service.java:246)
2020-10-07 10:01:47,739         at javax.mail.Service.connect(Service.java:195)
2020-10-07 10:01:47,739         at javax.mail.Transport.send0(Transport.java:254)
2020-10-07 10:01:47,739         at javax.mail.Transport.send(Transport.java:124)
2020-10-07 10:01:47,739         at com.mapfintech.systemlink.Connectivity.submitEmail(Connectivity.java:2089)
2020-10-07 10:01:47,739         at com.mapfintech.systemlink.Connectivity.sendAssertion(Connectivity.java:2025)
2020-10-07 10:01:47,739         at com.mapfintech.systemlink.datafetch.ClientDataFetcher.getFormJoins(ClientDataFetcher.java:880)
2020-10-07 10:01:47,739         at com.mapfintech.systemlink.datafetch.ClientDataFetcher.getProductFetch(ClientDataFetcher.java:408)
2020-10-07 10:01:47,739         at com.mapfintech.systemlink.MainClass.jobExecution(MainClass.java:3363)
2020-10-07 10:01:47,739         at com.mapfintech.systemlink.MainClass.<init>(MainClass.java:1438)
2020-10-07 10:01:47,739         at com.mapfintech.systemlink.MainClass.main(MainClass.java:349)
2020-10-07 10:01:47,740         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2020-10-07 10:01:47,740         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2020-10-07 10:01:47,740         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2020-10-07 10:01:47,740         at java.lang.reflect.Method.invoke(Method.java:498)
2020-10-07 10:01:47,740         at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282)
2020-10-07 10:01:47,740         at java.lang.Thread.run(Thread.java:748)
2020-10-07 10:01:47,740 Caused by: javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
2020-10-07 10:01:47,740         at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
2020-10-07 10:01:47,740         at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1959)
2020-10-07 10:01:47,740         at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1916)
2020-10-07 10:01:47,740         at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1899)
2020-10-07 10:01:47,740         at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1420)
2020-10-07 10:01:47,740         at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
2020-10-07 10:01:47,740         at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:598)
2020-10-07 10:01:47,740         at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:525)
2020-10-07 10:01:47,740         at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2041)
2020-10-07 10:01:47,740         ... 19 more
2020-10-07 10:01:47,740 Caused by: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
2020-10-07 10:01:47,740         at sun.security.validator.PKIXValidator.<init>(PKIXValidator.java:91)
2020-10-07 10:01:47,740         at sun.security.validator.Validator.getInstance(Validator.java:179)
2020-10-07 10:01:47,740         at sun.security.ssl.X509TrustManagerImpl.getValidator(X509TrustManagerImpl.java:312)
2020-10-07 10:01:47,740         at sun.security.ssl.X509TrustManagerImpl.checkTrustedInit(X509TrustManagerImpl.java:171)
2020-10-07 10:01:47,740         at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:184)
2020-10-07 10:01:47,741         at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
2020-10-07 10:01:47,741         at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1596)
2020-10-07 10:01:47,741         at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
2020-10-07 10:01:47,741         at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1052)
2020-10-07 10:01:47,741         at sun.security.ssl.Handshaker.process_record(Handshaker.java:987)
2020-10-07 10:01:47,741         at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072)
2020-10-07 10:01:47,741         at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
2020-10-07 10:01:47,741         at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
2020-10-07 10:01:47,741         ... 23 more
2020-10-07 10:01:47,741 Caused by: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
2020-10-07 10:01:47,741         at java.security.cert.PKIXParameters.setTrustAnchors(PKIXParameters.java:200)
2020-10-07 10:01:47,741         at java.security.cert.PKIXParameters.<init>(PKIXParameters.java:120)
2020-10-07 10:01:47,741         at java.security.cert.PKIXBuilderParameters.<init>(PKIXBuilderParameters.java:104)
2020-10-07 10:01:47,741         at sun.security.validator.PKIXValidator.<init>(PKIXValidator.java:89)
2020-10-07 10:01:47,741         ... 35 more

我的代码是:

static boolean email(String subject, String text, String attachmnt, Boolean switchReceiver) {
    boolean res = false;
    Properties propertiess = new Properties();
    propertiess.put("mail.smtp.auth", "true");
    propertiess.put("mail.smtp.host", mailSmtpHost);
    propertiess.put("mail.smtp.port", "587");
    // propertiess.put("mail.smtp.starttls.enable", "false");

    // Get the Session object.1
    Session sess = Session.getInstance(propertiess, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(mailUsername, mailPassword);
        }
    });

    try {
        // Header part
        Message messg = new MimeMessage(sess);
        String hostname = null;
        try {
            hostname = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        messg.setFrom(new InternetAddress(mailFrom, hostname + " Notifications"));

        if (switchReceiver) {
            messg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailToAssertion));
        } else {
            messg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailTo));
        }
        messg.setSubject(subject);

        // Body part
        BodyPart messgBodyPart = new MimeBodyPart();
        messgBodyPart.setText(text);
        Multipart mltp = new MimeMultipart();
        mltp.addBodyPart(messgBodyPart);

        if (attachmnt != null) {
            // Attachment
            messgBodyPart = new MimeBodyPart();
            DataSource srcc = new FileDataSource(attachmnt);
            messgBodyPart.setDataHandler(new DataHandler(srcc));
            messgBodyPart.setFileName(attachmnt);
            mltp.addBodyPart(messgBodyPart);
        }

        // Add content
        messg.setContent(mltp);

        // Send messg
        Transport.send(messg);

        System.out.println("Sent messg succeed!");
        res = true;
    } catch (Exception exp) {
        exp.printStackTrace();
        return false;
    }

    return res;
}

0 个答案:

没有答案