2017-09-19 44 views
0

SMTP & TS1を使用してJava電子メールに問題があります。この問題を解決するのを手伝ってください。私はその問題がトランスポートだと思うが、わからない。以下は私のコードとエラーのスニペットです、私が間違いや解決策を見つけるのを助けてください。Java電子メールSMTP&TSL、エラー電子メールを送信できません

TSLEmail.class

import java.util.Properties; 
import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 

public class TSLEmail 
{ 
    public static void main(String[] args) 
    { 
     final String fromEmail = "[email protected]"; //requires valid 
     gmail id 
     final String password = "mypass12"; // correct password for gmail id 
     final String toEmail = "[email protected]"; // can be any email id 

     System.out.println("TLSEmail Start"); 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", "10.20.200.220"); //SMTP Host 
     props.put("mail.user", "user12"); 
    //props.put("mail.password", password); 
    props.put("mail.smtp.port", "587"); //TLS Port 
    props.put("mail.smtp.auth", "true"); //enable authentication 
    props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS 

    //create Authenticator object to pass in Session.getInstance argument 
    Authenticator auth = new Authenticator() { 
        //override the getPasswordAuthentication method 
        protected PasswordAuthentication 
     getPasswordAuthentication() { 
            return new 
     PasswordAuthentication(fromEmail, password); 
        } 
     }; 
     Session session = Session.getInstance(props, auth); 

     EmailUtil.sendEmail(session, toEmail,"TLSEmail Testing Subject", 
     "TLSEmail Testing Body"); 

    } 
    } 

EmailUtil.class

import java.util.Date; 
import javax.mail.Message; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class EmailUtil 
{ 
    public static void sendEmail(Session session, String toEmail, String 
    subject, String body) 
    { 
     try 
     { 
     MimeMessage msg = new MimeMessage(session); 
     //set message headers 
     msg.addHeader("Content-type", "text/HTML; charset=UTF-8"); 
     msg.addHeader("format", "flowed"); 
     msg.addHeader("Content-Transfer-Encoding", "8bit"); 

     msg.setFrom(new InternetAddress("[email protected]")); 

     msg.setReplyTo(InternetAddress.parse("[email protected]")); 

     msg.setSubject(subject, "UTF-8"); 

     msg.setText(body, "UTF-8"); 

     msg.setSentDate(new Date()); 

     msg.setRecipients(Message.RecipientType.TO, 
     InternetAddress.parse(toEmail, false)); 

     System.out.println("Message is ready"); 
     Transport.send(msg); 

     System.out.println("EMail Sent Successfully!!"); 
     } 
     catch (Exception e) { 
     e.printStackTrace(); 
     } 
    } 
    } 

結果(ERROR):

TLSEmail Start 
Message is ready 
javax.mail.MessagingException: Can't send command to SMTP host; 
    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.sendCommand(SMTPTransport.java:1564) 
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1551) 
    at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:935) 
    at 
    com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:426) 
    at javax.mail.Service.connect(Service.java:310) 
    at javax.mail.Service.connect(Service.java:169) 
    at javax.mail.Service.connect(Service.java:118) 
    at javax.mail.Transport.send0(Transport.java:188) 
    at javax.mail.Transport.send(Transport.java:118) 
    at com.bca.controller.EmailUtil.sendEmail(EmailUtil.java:35) 
    at com.bca.controller.TSLEmail.main(TSLEmail.java:34) 
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.writeRecord(Unknown Source) 
    at sun.security.ssl.AppOutputStream.write(Unknown Source) 
    at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:114) 
    at java.io.BufferedOutputStream.flushBuffer(Unknown Source) 
    at java.io.BufferedOutputStream.flush(Unknown Source) 
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1562) 
     ... 10 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) 
     ... 22 more 
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: 
unable to find valid certification path to requested target 
    at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown 
     Source) 
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown 
     Source) 
    at java.security.cert.CertPathBuilder.build(Unknown Source) 
     ... 28 more 

私は私の問題のためのシンプルかつ一定の解決策を見つける助けてください。 はありがとうございました

+0

[JavaMail FAQ](https://javaee.github.io/javamail/FAQ#installcert)を参照してください。 (私は彼らがなぜそれをFAQと呼んでいるのだろうと思っていますか?) –

答えて

0

このプロパティも追加します。それは私のために働いた。

props.put("mail.smtp.ssl.trust", Smtp_host); 
+1

ポート587をstarttlsで使用している場合、そのプロパティを追加しないでください**。 –

関連する問題