从tomcat发送电子邮件的异常

时间:2011-05-23 13:12:32

标签: tomcat javamail

尝试从tomcat发送来自javamail的电子邮件时,我会遇到奇怪的行为。 我第一次尝试发送它的电子邮件。在第二次尝试时,我得到一个异常,它继续抛出此异常,直到我重新启动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配置:

    <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

0 个答案:

没有答案