I am using JavaMail set send email via Microsoft Exchange Server.
When I run this program, it show this error message:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is 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. Failed messages: 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; message exception details (1) are: Failed message 1: 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 com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666) at javax.mail.Service.connect(Service.java:295) at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:501) at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:421) at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345) at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340) at albert.practice.mail.ExchangeServerMailTest.sendMail(ExchangeServerMailTest.java:49) at albert.practice.mail.ExchangeServerMailTest.main(ExchangeServerMailTest.java:28) |
The code snippet looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | private JavaMailSenderImpl getJavaMailSender() { Properties props = new Properties(); props.put("mail.smtp.auth", true); // mail server configuration String host = "your smtp"; int port = 25; String userName = "your user name"; String password = "your password"; JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setJavaMailProperties(props); sender.setHost(host); sender.setPort(port); sender.setUsername(userName); sender.setPassword(password); sender.setDefaultEncoding("UTF-8"); return sender; } |
How-To
You need to set mail.smtp.ssl.trust in your JavaMail properties.
If set, and a socket factory hasn't been specified, enables use of a MailSSLSocketFactory.
If set to "*", all hosts are trusted.
If set to a whitespace separated list of hosts, those hosts are trusted. Otherwise, trust depends on the certificate the server presents.
The updated code snippet looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | private JavaMailSenderImpl getJavaMailSender() { Properties props = new Properties(); props.put("mail.smtp.auth", true); // Fix Excpetion: Mail server connection failed; nested exception is // javax.mail.MessagingException: Could not convert socket to TLS // If set, and a socket factory hasn't been specified, enables use of a // MailSSLSocketFactory. If set to "*", all hosts are trusted. If set to a whitespace // separated list of hosts, those hosts are trusted. Otherwise, trust depends on the // certificate the server presents. props.put("mail.smtp.ssl.trust", "*"); // mail server configuration String host = "your smtp"; int port = 25; String userName = "your user name"; String password = "your password"; JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setJavaMailProperties(props); sender.setHost(host); sender.setPort(port); sender.setUsername(userName); sender.setPassword(password); sender.setDefaultEncoding("UTF-8"); return sender; } |
Reference
[1] https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html
No comments:
Post a Comment