2012-11-28 2 views
6

J'utilise le courrier java pour envoyer des emails via smtp. Les réglages de smtp donnés ci-dessous:Le courrier Java sans ssl - Le développement du chemin PKIX a échoué:

 Properties props = new Properties(); 
     Object put = props.put("mail.smtp.host", smtpHost); 
     props.put("mail.smtp.user", smtpUser); 
     props.put("mail.smtp.auth", true); 
     props.put("mail.debug", mailDebug); 
     props.put("mail.smtp.port", port); 

Les pouvoirs smtp ont été vérifiés par telnetting à mon smtpHost les détails ci-dessus. Cependant, je reçois l'exception suivante lorsque j'utilise les paramètres ci-dessus dans le courrier java.

 250-AUTH PLAIN LOGIN 
     250-STARTTLS 
     250 HELP 
     DEBUG SMTP: Found extension "SIZE", arg "52428800" 
     DEBUG SMTP: Found extension "8BITMIME", arg "" 
     DEBUG SMTP: Found extension "PIPELINING", arg "" 
     DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN" 
     DEBUG SMTP: Found extension "STARTTLS", arg "" 
     DEBUG SMTP: Found extension "HELP", arg "" 
     DEBUG SMTP: Attempt to authenticate 
     DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
     DEBUG SMTP: AUTH LOGIN command trace suppressed 
     DEBUG SMTP: AUTH LOGIN failed 
     Nov 29, 2012 11:54:40 AM com.Test main 
     SEVERE: null 
     javax.mail.AuthenticationFailedException: 535 Incorrect authentication data 

Quand j'ajoute la ligne:

 props.put("mail.smtp.starttls.enable", false); 

Il génère à nouveau la même authentification exception a échoué.

Si je mets mail.smtp.starttls.enable à vrai, l'authentification réussit, mais je reçois l'exception suivante:

 220 TLS go ahead 
    Nov 28, 2012 5:32:36 PM com.Test main 
    SEVERE: null 
    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:1918) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:652) 
    at javax.mail.Service.connect(Service.java:317) 

Après avoir traversé diverses discussions du forum concernant la deuxième exception, J'ai exécuté le programme InstallCert pour récupérer le certificat auto-signé du serveur. Le InstallCert jette l'exception suivante:

  Opening connection to mydomain.com.au:443... 
      Starting SSL handshake... 
      javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? 
        at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:542) 
        at sun.security.ssl.InputRecord.read(InputRecord.java:374) 
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:850) 
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1190) 
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1217) 
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1201) 
        at InstallCert.main(InstallCert.java:100) 
      Could not obtain server certificate chain 

Alors, on dirait que mon serveur n'a pas ssl, mais starttls est activé. Quels sont les paramètres corrects pour envoyer du courrier avec STARTTLS, sur un serveur sans SSL?

Répondre

10

This JavaMail FAQ entry devrait aider.

Essayez d'utiliser MailSSLSocketFactory comme ceci:

MailSSLSocketFactory sf = new MailSSLSocketFactory(); 
    sf.setTrustAllHosts(true); 
    props.put("mail.smtp.ssl.socketFactory", sf); 
+0

J'ai essayé celui-là. Utilisé InstallCert comme décrit dans la FAQ, et j'ai obtenu l'exception "javax.net.ssl.SSLException: message SSL non reconnu, connexion en clair?". On dirait que mon serveur n'utilise pas SSL du tout. – janenz00

+0

Edité ma question avec l'exception InstallCert aussi. – janenz00

+0

Le problème avec InstallCert est que cela ne fonctionne que si vous pouvez établir une connexion SSL pour commencer. Si votre serveur ne prend en charge que les connexions en texte brut qui sont converties en connexion SSL (TLS), InstalCert n'aidera pas. Vous pouvez regarder la classe [MailSSLSocketFactory] (http://javamail.kenai.com/nonav/javadocs/com/sun/mail/util/MailSSLSocketFactory.html), ce qui vous permet de gérer plus facilement les auto-signatures. certificats qui autrement ne seront pas vérifiés. –

2

a marché pour moi :)

Properties props = new Properties(); 
     props.put("mail.transport.protocol", "smtp"); 
     props.put("mail.smtp.host", "smtp.companydomain.biz"); // 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.debug", "true"); 
     props.put("mail.smtp.starttls.enable", "true");`enter code here` 
     props.put("mail.smtp.port", "25"); 
     props.put("mail.smtp.socketFactory.port", "25"); 
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.socketFactory.fallback", "true"); 

     MailSSLSocketFactory sf = null; 
     try { 
      sf = new MailSSLSocketFactory(); 
     } catch (GeneralSecurityException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     sf.setTrustAllHosts(true); 
     props.put("mail.smtp.ssl.socketFactory", sf); 

     Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() { 

      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication("[email protected]", "password"); 
      } 
     }); 

     mailSession.setDebug(true); // Enable the debug mode 

     Message msg = new MimeMessage(mailSession); 

     //--[ Set the FROM, TO, DATE and SUBJECT fields 
     try { 
      msg.setFrom(new InternetAddress("[email protected]")); 
     } catch (AddressException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (MessagingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse("[email protected]")); 
     } catch (AddressException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (MessagingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     //msg.setSentDate(new Date()); 
     try { 
      msg.setSubject("Hello World!"); 
     } catch (MessagingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     //--[ Create the body of the mail 
     try { 
      msg.setText("Hello from my first e-mail sent with JavaMail"); 
     } catch (MessagingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     //--[ Ask the Transport class to send our mail message 
     try { 
      Transport.send(msg); 
     } catch (MessagingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
0

J'ai eu ce problème avec la mise à jour java 8. Après ce problème de propriété résolu

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

si chaussure utilisée printemps dans application.property

spring.mail.properties.mail.smtp.ssl.trust = smtp.gmail.com

Questions connexes