【发布时间】:2017-11-08 08:46:05
【问题描述】:
我的 application.properties 文件包含以下配置:-
spring.mail.properties.mail.smtp.connecttimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000
spring.mail.host=smtp.office365.com
spring.mail.password=password
spring.mail.port=587
spring.mail.username=test@outlook.com
spring.mail.properties.mail.smtp.starttls.enable=true
security.require-ssl=true
spring.mail.properties.mail.smpt.auth=true
用于实现邮件服务器的 Java 类是:
@Component
public class SmtpMailSender {
@Autowired
private JavaMailSender javaMailSender;
public void sendMail(String to, String subject, String body) throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper;
helper = new MimeMessageHelper(message, true);//true indicates multipart message
helper.setSubject(subject);
helper.setTo(to);
helper.setText(body, true);//true indicates body is html
javaMailSender.send(message);
}
}
我的控制器类是:
@RestController
public class MailController {
@Autowired
SmtpMailSender smtpMailSender;
@RequestMapping(path = "/api/mail/send")
public void sendMail() throws MessagingException {
smtpMailSender.sendMail("test123@outlook.com", "testmail", "hello!");
}
}
当我发送获取请求 (/api/mail/send) 时出现以下错误:
{
"timestamp": 1496815958863,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.mail.MailAuthenticationException",
"message": "Authentication failed; nested exception is
javax.mail.AuthenticationFailedException: ;\n nested exception
is:\n\tjavax.mail.MessagingException: Exception reading response;\n nested
exception is:\n\tjava.net.SocketTimeoutException: Read timed out",
"path": "/api/mail/send"
}
任何帮助将不胜感激。
【问题讨论】:
-
邮件服务器连接失败;嵌套异常是 com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp-mail.outlook.com,995;感谢您的帮助。
-
@user7294900 我尝试了给定链接中提供的解决方案,但没有成功。感谢您的帮助
-
我现在收到此错误:{ "timestamp": 1496828104173, "status": 500, "error": "Internal Server Error", "exception": "org.springframework.mail.MailSendException ", "message": "失败的消息: javax.mail.MessagingException: 异常读取响应;\n 嵌套异常是:\n\tjava.net.SocketTimeoutException: 读取超时", "path": "/api/mail/发送”}
-
setFrom() 解决了我的问题。你应该接受答案。如果有任何问题,请发布它。请记住,如果您在公司外部,则不能将其发送到组织外部。并记住大多数邮件端口(如 25、587)被组织阻止。因此,请尝试禁用代理并尝试使用组织网络以外的其他网络关闭代理
标签: java spring spring-boot outlook office365