2017-03-06 68 views
0

我跟着this教程發送使用JavaMail API郵件,但它表明我這個錯誤,當我嘗試運行代碼:的JavaMail API錯誤

Exception in thread "main" java.lang.RuntimeException: 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 SendEmail.main(SendEmail.java:64) 

我有javax.mail.jar文件中的類路徑和我雙重檢查代碼,它仍然無法正常工作。

這是代碼:

import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class SendEmail { 
public static void main(String[] args) { 
    // Recipient's email ID needs to be mentioned. 
    String to = "[email protected]"; 

    // Sender's email ID needs to be mentioned 
    String from = "[email protected]"; 
    final String username = "[email protected]";//change accordingly 
    final String password = "password";//change accordingly 

    // Assuming you are sending email through relay.jangosmtp.net 
    String host = "smtp.gmail.com"; 

    Properties props = new Properties(); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.port", "587"); 

    // Get the Session object. 
    Session session = Session.getInstance(props, 
    new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(username, password); 
    } 
    }); 

    try { 
    // Create a default MimeMessage object. 
    Message message = new MimeMessage(session); 

    // Set From: header field of the header. 
    message.setFrom(new InternetAddress(from)); 

    // Set To: header field of the header. 
    message.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse(to)); 

    // Set Subject: header field 
    message.setSubject("Testing Subject"); 

    // Now set the actual message 
    message.setText("Hello, this is sample for to check send " + 
    "email using JavaMailAPI "); 

    // Send message 
    Transport.send(message); 

    System.out.println("Sent message successfully...."); 

    } catch (MessagingException e) { 
    throw new RuntimeException(e); 
    } 

}}

+0

它可以正常工作... –

+0

你是google的例外消息嗎? https://www.google.com/search?q=PKIX+path+building+failed:+sun.security.provider.certpath.SunCertPathBuilderException:+unable+to+find+valid+certification+path+to+requested+ target&sa = G&gbv = 1&sei = CNC9WLTuNJ64jAP55ragAw – Robert

+0

是的,沒有什麼幫助 –

回答

0

添加並再次嘗試。

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

不起作用:(在線程「main」中顯示不同的錯誤異常java.lang.RuntimeException:javax.mail.AuthenticationFailedException:534-5.7.14

+0

問題與證書有關。試試這個http://stackoverflow.com/questions/9619030/resolving-javax-net-ssl-sslhandshakeexception-sun- security-validator-validatore如果有幫助 – Maverick

+0

其實,它的工作原理 –