0

I'm having trouble getting a RuntimeException while sending email using Java to Zimbra Mail Server.

Here is my Java class Test.java

public class Test {

    public static void main(String[] args) {
        sendMail("receiver@domain.com","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 = "user@domain.com;
        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("sender@domain.com"));
            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);
        }
    }

}

And I got the following RuntimeException stacktrace in my "main" thread.

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)

Gozus19
  • 167
  • 16
Ryan
  • 1

1 Answers1

0

Did you take a look at this? This could be your solutions. It seems that your settings are missing something.

In the link I gave you the same issue is solved by adding this code line.

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

That in this case should not contain "smtp.gmail.com" but "smtp.benvi.com" if that's your true host.

I hope to be helpful.

Gozus19
  • 167
  • 16