1、Java mail 邮件能够发送成功,但是会抛出这样的一个异常
org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: Exception reading response (java.net.SocketTimeoutException: Read timed out)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:448) ~[spring-context-support-4.1.0.RELEASE.jar:4.1.0.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:346) ~[spring-context-support-4.1.0.RELEASE.jar:4.1.0.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:341) ~[spring-context-support-4.1.0.RELEASE.jar:4.1.0.RELEASE]
2、Java代码结构如下:
String SSL_FACTORY="javax.net.ssl.SSLSocketFactory";
Properties prop = new Properties();
/* prop.put("mail.smtp.auth", "0".equals(smtp
.get("mail_needauth")) ? "true" : "false");*/
prop.put("mail.smtp.auth", "true");
// 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
prop.put("mail.smtp.timeout", smtp.get("SMTP_TIME_OUT"));
prop.put("mail.smtp.host", host);
prop.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
prop.setProperty("mail.smtp.socketFactory.fallback", String.valueOf(port));
prop.setProperty("mail.smtp.starttls.enable", "true");
prop.setProperty("mail.smtp.socketFactory.port", String.valueOf(port));
prop.setProperty("mail.smtp.auth", "true");
// 添加验证
EmailAutherticator auth = new EmailAutherticator(name, pwd);
Session session = Session.getDefaultInstance(prop, auth);
senderImpl.setSession(session);
3、解决办法:
设置超时时间即可解决…
版权声明:本文为iCopper_原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。