2014-06-28 112 views
1

所以我剛開始使用JavaMail API。我在我的Web應用程序的幾個地方使用過它(我使用Struts2 & Hibernate),發送電子郵件的代碼在某些地方有效,並且在某些地方不適用。它沒有的地方,我得到一個MessagingException : Could not convert socket to TLSJava郵件消息異常

這是我的代碼。在類中,我定義了這個靜態塊:

靜態屬性properties = new Properties(); 靜態{

properties.put("mail.smtp.starttls.enable", "true"); 
    properties.put("mail.smtp.host", "smtp.gmail.com"); 
    properties.put("mail.smtp.user", "[email protected]"); // User name 
    properties.put("mail.smtp.password", "<the_correct_password>"); // password 
    properties.put("mail.smtp.port", "587"); 
    properties.put("mail.smtp.auth", "true"); 
} 

而這裏的發送是一個函數內的電子郵件代碼:

javax.mail.Session emailSession = javax.mail.Session.getDefaultInstance(properties, 
            new javax.mail.Authenticator() { 
           protected PasswordAuthentication 
           getPasswordAuthentication() { 
           return new 
           PasswordAuthentication("[email protected]", "<the_correct_password>"); 
           }}); 

    Message message = new MimeMessage(emailSession); 
    message.setFrom(new InternetAddress("[email protected]")); 
    message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(getModel().getEmail())); 
message.setSubject("Password change!"); 
message.setText("Hi "+getModel().getName()+"\n"+ "Security code - "+sb.toString()); 
Transport.send(message); 

這裏是異常堆棧跟蹤:

javax.mail.MessagingException: Could not convert socket to TLS; 
    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.startTLS(SMTPTransport.java:1907) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666) 
    at javax.mail.Service.connect(Service.java:317) 
    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 com.proconsulto.action.LoginAction.checkEmail(LoginAction.java:295) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252) 
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) 
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) 
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) 
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:546) 
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) 
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:304) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
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(Unknown Source) 
    at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source) 
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source) 
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source) 
    at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source) 
    at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source) 
    at sun.security.ssl.Handshaker.processLoop(Unknown Source) 
    at sun.security.ssl.Handshaker.process_record(Unknown Source) 
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) 
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) 
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) 
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) 
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549) 
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:486) 
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902) 
    ... 76 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(Unknown Source) 
    at sun.security.validator.PKIXValidator.engineValidate(Unknown Source) 
    at sun.security.validator.Validator.validate(Unknown Source) 
    at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source) 
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source) 
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source) 
    ... 87 more 
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source) 
    at java.security.cert.CertPathBuilder.build(Unknown Source) 
    ... 93 more 

295線多數民衆贊成提到的是這line:Transport.send(message)

令人驚訝的是,這個代碼在幾個功能&的作品中失敗了。我用幾十次代替了確切的代碼,但我仍然無法弄清楚什麼是錯的。我想也許它沒有收到收件人地址,或者properties不正確,但我檢查了一倍,並且我無法理解導致此行爲的原因。請幫幫我!

回答

2

請參考http://www.oracle.com/technetwork/java/javamail/faq/index.html#installcert其中規定:

問:當連接到我的郵件服務器通過SSL我得到「無法找到有效的認證路徑要求的目標」異常。

答:您的服務器則可能使用測試證書或自簽名證書,而不是由商業 證書頒發機構簽署的證書。您需要將服務器的證書 安裝到您的信任存儲中。 InstallCert程序將有所幫助。

或者,您可以將「mail.protocol.ssl.trust」屬性設置爲 郵件服務器的主機名。有關詳細信息,請參閱協議 提供程序包的javadoc。這個問題的

其他常見原因有:

  • 有一個防火牆或防病毒程序截獲您的請求。
  • 您的JDK安裝出現問題,導致它無法爲受信任的證書頒發機構查找證書。
  • 您正在運行在覆蓋了JDK的可信證書頒發機構列表的應用程序服務器中。

當你連接到Gmail應該都存在於默認信任的證書需要,原因有可能成爲反病毒阻止您的SMTP調用。

+0

...但我不使用防病毒。爲什麼它在某些代碼段中工作,而不是在其他代碼段中工作? – Saturnian

+0

您可以嘗試使用「-Djava.security.debug = all」運行應用程序併發布結果,可能會有更多相關的詳細信息。您也可以嘗試使用另一個JDK安裝來運行該應用程序,並查看它是否消失。 –

+0

我該怎麼做?我在Windows上使用Eclipse。如何使用-Djava.security.debug = all運行應用程序? – Saturnian

0

我必須在我的靜態塊添加其他財產,爲我工作:

props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

+1

如果這樣可以正常工作,那意味着您的JDK安裝程序出現問題,導致它無法找到允許其正常信任Gmail的證書頒發機構證書。如果您只在Eclipse中運行程序,請嘗試在Eclipse之外運行它。正如其他人所提到的,您可能擁有防火牆或防病毒產品,它們攔截與Gmail的通信並呈現不同的證書。您可能需要使用InstallCert程序,該程序將打印出證書的詳細信息,以查看您真正獲得的證書。 –

+0

嗨。我現在只添加了這行代碼。此前,令人驚訝的是,它曾經工作。我能夠毫無問題地發送電子郵件。我運行該項目的一個很好的一天,它停止工作。所以我必須添加該行。我檢查了我的防火牆設置,我目前正在安裝反病毒軟件。我非常驚訝它在兩天前突然拋出一個'MessagingException',它工作正常。 另外,我在運行InstallCert時遇到了一些問題。我希望我能很快解決這個問題! – Saturnian