2017-07-07 7 views
0

Javaを使用してZimbra Mail Serverにメールを送信しているときにRuntimeExceptionを取得できません。Javaが有効な証明書を見つけることができません

は、ここに私の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); 
     } 
    } 

} 

であり、私は私の "メイン" スレッドで次の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を見てみましたか? これはあなたのソリューションかもしれません。あなたの設定に何かがないようです。

このコード行を追加することで、私は同じ問題を解決しました。それはあなたの本当のホストの場合、この場合には"smtp.gmail.com"が、"smtp.benvi.com"を含むべきではありません

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

私は役に立ちそうです。

関連する問題