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

}

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)

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.