2015-02-05 204 views
1

我想從另一個服務器使用帳戶發送電子郵件,而不是Gmail,但發送時發生此異常。如何解決javax.net.ssl.SSLProtocolException:SSL握手中止

錯誤:

02-05 16:10:37.688: W/System.err(30346): javax.mail.MessagingException: Could not connect to SMTP host: smtp.suprema.net.br, port: 587; 
02-05 16:10:37.688: W/System.err(30346): nested exception is: 
02-05 16:10:37.688: W/System.err(30346): javax.net.ssl.SSLHandshakeException: Handshake failed 
02-05 16:10:37.688: W/System.err(30346): at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391) 
02-05 16:10:37.688: W/System.err(30346): at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412) 
02-05 16:10:37.688: W/System.err(30346): at javax.mail.Service.connect(Service.java:288) 
02-05 16:10:37.688: W/System.err(30346): at com.prologica.smart_detection.Mail.Send(Mail.java:71) 
02-05 16:10:37.689: W/System.err(30346): at com.prologica.smart_detection.utils.Utilities.SendMail(Utilities.java:162) 
02-05 16:10:37.689: W/System.err(30346): at com.prologica.smart_detection.utils.Utilities.NotifyContatcts(Utilities.java:106) 
02-05 16:10:37.689: W/System.err(30346): at com.prologica.smart_detection.services.NotificationService.NotifyContacts(NotificationService.java:106) 
02-05 16:10:37.689: W/System.err(30346): at com.prologica.smart_detection.services.NotificationService.GetNotifications(NotificationService.java:79) 
02-05 16:10:37.689: W/System.err(30346): at com.prologica.smart_detection.services.NotificationService.run(NotificationService.java:61) 
02-05 16:10:37.689: W/System.err(30346): at java.lang.Thread.run(Thread.java:818) 
02-05 16:10:37.689: W/System.err(30346): Caused by: javax.net.ssl.SSLHandshakeException: Handshake failed 
02-05 16:10:37.689: W/System.err(30346): at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:374) 
02-05 16:10:37.689: W/System.err(30346): at com.android.org.conscrypt.OpenSSLSocketImpl.waitForHandshake(OpenSSLSocketImpl.java:598) 
02-05 16:10:37.689: W/System.err(30346): at com.android.org.conscrypt.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:560) 
02-05 16:10:37.689: W/System.err(30346): at com.sun.mail.smtp.SMTPTransport.initStreams(SMTPTransport.java:1449) 
02-05 16:10:37.689: W/System.err(30346): at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1366) 
02-05 16:10:37.689: W/System.err(30346): ... 9 more 
02-05 16:10:37.689: W/System.err(30346): Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb7617058: Failure in SSL library, usually a protocol error 
02-05 16:10:37.689: W/System.err(30346): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:790 0xae73aedd:0x00000000) 
02-05 16:10:37.690: W/System.err(30346): at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method) 
02-05 16:10:37.690: W/System.err(30346): at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:302) 
02-05 16:10:37.690: W/System.err(30346): ... 13 more 

我的方法

public Boolean Send(String localFile, String subject, String body, String recipients) 
{ 
    Boolean ret = false; 

    try 
    { 
     String user = MailSenderConfiguration.USER; 
     String pass = MailSenderConfiguration.PASSWORD; 

     Properties props = System.getProperties(); 
     props.setProperty("mail.transport.protocol", "smtp"); 
     props.setProperty("mail.host", MailSenderConfiguration.SMTP); 
     props.put("mail.smtp.auth", "true"); 
     //props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.port", MailSenderConfiguration.PORT); 
     props.put("mail.smtp.socketFactory.port", MailSenderConfiguration.PORT); 
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.socketFactory.fallback", "false"); 
     props.setProperty("mail.smtp.quitwait", "false"); 

     Authenticator auth = new Autenticador(user,pass); 

     Session session = Session.getDefaultInstance(props, auth); 

     MimeMessage msg = new MimeMessage(session); 
     msg.setFrom(new InternetAddress(user)); 

     if (recipients.indexOf(',') > 0) 
       msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); 
      else 
       msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); 
     //msg.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recipients)); 

     msg.setSentDate(new Date()); 
     msg.setSubject(subject); 
     MimeBodyPart textPart = new MimeBodyPart(); 
     textPart.setContent(body, "text/plain"); 
     Multipart mp = new MimeMultipart(); 
     mp.addBodyPart(textPart); 
     msg.setContent(mp); 
     msg.saveChanges(); 
     Transport transport = session.getTransport("smtp"); 
     transport.connect(MailSenderConfiguration.SMTP, Integer.parseInt(MailSenderConfiguration.PORT), user, pass); 
     transport.sendMessage(msg, msg.getAllRecipients()); 
     transport.close(); 

     ret = true; 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 

    return ret; 
} 

回答

-2

你的代碼中包含幾個most common JavaMail mistakes的。從修復它們開始。

然後按照這些JavaMail FAQ tips for debugging

我無法從發佈的內容中知道您實際嘗試連接的端口,但也許您嘗試與非SSL端口建立SSL連接。或者,您可能會遇到防火牆或防病毒問題。