Java Email SMTP& TSL,错误无法发送电子邮件

时间:2017-09-19 04:19:28

标签: java maven server smtp javamail

我使用SMTP& amp; 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 = "mail@gmail.com"; //requires valid 
      gmail id
      final String password = "mypass12"; // correct password for gmail id
      final String toEmail = "recipients@gmail.com"; // 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("mail@gmail.com"));

        msg.setReplyTo(InternetAddress.parse("recipients@gmail.com"));

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

结果(错误):

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

请帮我找一个简单明了的解决方案来解决我的问题。 谢谢

2 个答案:

答案 0 :(得分:0)

听起来您需要将服务器返回的证书添加到JRE的信任库中,

有很多好文章解释了如何做到这一点,包括:

Java Keytool Essentials: Working with Java Keystores

如果这是一个Web应用程序(例如,Tomcat或JBoss),那么在服务器级别(而不是JRE)执行此操作。

答案 1 :(得分:0)

也添加此属性。它对我有用。

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