2012-11-28 2 views
6

SMTP 메일을 사용하여 이메일을 보내고 있습니다. 아래에 주어진 smtp 설정 :ssl이없는 Java 메일 - PKIX 경로 구축 실패 :

 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); 

smtp 자격 증명은 위의 세부 사항으로 내 smtpHost에 telnetting하여 확인되었습니다. 그러나 자바 메일에서 위의 설정을 사용할 때 다음 예외가 발생합니다. 그것은 다시 동일한 인증 실패 예외를 생성

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

: 나는 줄을 추가

 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 

. 두 번째 예외에 관한 다양한 포럼 스레드를 거쳐

 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) 

:

나는 mail.smtp.starttls.enable 사실 에가, 인증이 성공 설정하면, 그러나 나는 다음과 같은 예외가 InstallCert 프로그램을 실행하여 서버의 자체 서명 된 인증서를 가져 왔습니다. InstallCert 다음과 같은 예외가 발생합니다 : 내 서버가 SSL이없는 것처럼

  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 

그래서, 보이는,하지만 STARTTLS를 사용할 수 있습니다. STARTTLS를 사용하여 메일을 보내는 올바른 매개 변수는 SSL이없는 서버에 무엇입니까?

답변

10

This JavaMail FAQ entry이 도움이됩니다.

것은이 같은 MailSSLSocketFactory를 사용해보십시오 :

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

나는 그 일을하려고 않은 경우. FAQ에 설명 된대로 InstallCert를 사용했는데 "javax.net.ssl.SSLException : Unrecognized SSL message, plaintext connection?"예외가 발생했습니다. 내 서버가 SSL을 전혀 사용하지 않는 것 같습니다. – janenz00

+0

InstallCert 예외도 함께 내 질문을 편집했습니다. – janenz00

+0

InstallCert의 문제점은 SSL 연결을 시작할 수있는 경우에만 작동한다는 것입니다. 서버가 SSL (TLS) 연결로 변환되는 일반 텍스트 연결 만 지원하는 경우 InstalCert는 도움이되지 않습니다. [MailSSLSocketFactory] (http://javamail.kenai.com/nonav/javadocs/com/sun/mail/util/MailSSLSocketFactory.html) 클래스를 보면 자체 서명 된 클래스를 더 쉽게 처리 할 수 ​​있습니다. 그렇지 않으면 검증되지 않는 인증서. –

2

:)

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

근무 I이 부동산 문제를 업데이트 한 후 자바 8.이 문제를했다가

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

application.property에 사용되는 스프링 부팅

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

관련 문제