2017-07-07 69 views
0

我在使用Java發送電子郵件到Zimbra郵件服務器時遇到了RuntimeException問題。Java無法找到有效的認證

這裏是我的Test.java

public class Test { 

    public static void main(String[] args) { 
     sendMail("[email protected]","Test","Test Message",null); 
    } 

    public static Properties getMailProperties() { 
     Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", "smtp.bevmi.com"); 
     props.put("mail.smtp.port", "587"); 
     return props; 
    } 

    public static boolean sendMail(String to, String subject,String text,String cc) { 
     final String username = "[email protected]; 
     final String password = "password"; 
     Properties props = getMailProperties(); 
     Session session = Session.getInstance(props, 
     new javax.mail.Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(username, password); 
      } 
     }); 
     try { 
      MimeMessage message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(username)); 
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 
      message.setSender(new InternetAddress("[email protected]")); 
      if ((cc!=null) && (!cc.isEmpty())) { 
       message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc)); 
      } 
      message.setSubject(subject); 
      message.setText(text, "UTF-8", "html"); 
      System.out.println("Sending Message to :" + to); 
      Transport.send(message); 
      System.out.println("Email Sent"); 
      return true; 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
    } 

} 

Java階級和我在 「主」 線程得到了以下的RuntimeException堆棧跟蹤。

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 Test.sendMail(Test.java:57) at Test.main(Test.java:13)

回答

0

你看看this? 這可能是你的解決方案。看來你的設置缺少一些東西。

在我給你的鏈接相同的問題是通過添加此代碼行解決。

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

,在這種情況下,不應該包含"smtp.gmail.com""smtp.benvi.com"如果這是你真正的主人。

我希望能有所幫助。

相關問題