Исключение при отправке электронной почты от Tomcat - PullRequest
2 голосов
/ 23 мая 2011

Я получаю странное поведение при попытке отправить электронное письмо с javamail с tomcat. В первый раз, когда я пытаюсь отправить письмо, это работает. Со второй попытки я получаю исключение, и оно продолжает выдавать это исключение, пока я не перезапущу tomcat, и цикл продолжается.

Я использую Tomcat 7, я поместил mail.jar в TOMCAT_HOME / lib

Конфигурация Tomcat: TOMCAT_HOME / CONF / context.xml

  <Resource name="mail/Session" auth="Container"
            type="javax.mail.Session" 
         mail.smtp.host="smtp.gmail.com"
        username="*****@gmail.com"
        password="*******"
        mail.debug="true"
mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory"
        mail.user="*****@gmail.com"
        mail.transport.protocol="smtp" 
        mail.smtp.auth= "true"
        mail.smtp.port= "465"
        description="Global E-Mail Resource"/>

web.xml configurion:

    <resource-ref>
            <description>mail </description>
        <res-ref-name>mail/Session</res-ref-name>
        <res-type>javax.mail.Session</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>

код Java:

        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");
        Session session = (Session) envCtx.lookup("mail/Session");

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromParam));
        message.setRecipients(Message.RecipientType.TO, addressTo);
        message.setSubject(subjectParam);
        message.setContent(mimeMultipart);
        Transport.send(message);
        System.out.println("Done");

Исключение:

Could not connect to SMTP host: smtp.gmail.com, port: 465. Cause [Could not connect to SMTP host: smtp.gmail.com, port: 465].
        at web.scrappy.deamon.util.SendMailResource.postMail(SendMailResource.java:111)
        at web.scrappy.deamon.task.ScrappyOutgoingCommsTaskWorker.doWork(ScrappyOutgoingCommsTaskWorker.java:92)
        at web.scrappy.deamon.timertask.ScrappyOutgoingCommsTimerTask.run(ScrappyOutgoingCommsTimerTask.java:47)
        at java.util.TimerThread.mainLoop(Timer.java:512)
        at java.util.TimerThread.run(Timer.java:462)
    Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
      nested exception is:
        javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
        at javax.mail.Service.connect(Service.java:295)
        at javax.mail.Service.connect(Service.java:176)
        at javax.mail.Service.connect(Service.java:125)
        at javax.mail.Transport.send0(Transport.java:194)
        at javax.mail.Transport.send(Transport.java:124)
        at web.scrappy.deamon.util.SendMailResource.postMail(SendMailResource.java:106)
        ... 4 more
    Caused by: javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
        at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:190)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1623)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1586)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1569)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1154)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1131)
        at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:507)
        at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
        ... 11 more
    Caused by: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
        at sun.security.validator.PKIXValidator.<init>(PKIXValidator.java:59)
        at sun.security.validator.Validator.getInstance(Validator.java:161)
        at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.getValidator(X509TrustManagerImpl.java:108)
        at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:204)
        at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:249)
        at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1053)
        at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:128)
        at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:529)
        at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:465)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:884)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1120)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1147)
        ... 15 more
    Caused by: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
        at java.security.cert.PKIXParameters.setTrustAnchors(PKIXParameters.java:183)
        at java.security.cert.PKIXParameters.<init>(PKIXParameters.java:103)
        at java.security.cert.PKIXBuilderParameters.<init>(PKIXBuilderParameters.java:87)
        at sun.security.validator.PKIXValidator.<init>(PKIXValidator.java:57)
        ... 26 more
...