我的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!");
}
}
当我发送get请求(/ 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"
}
任何帮助都会非常感激。
答案 0 :(得分:1)
您必须使用sender
方法指定setFrom
才能在outlook.com上执行身份验证:
@Component
public class SmtpMailSender {
@Value("${spring.mail.username}")
private String from;
@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.setFrom(from) // <--- THIS IS IMPORTANT
helper.setSubject(subject);
helper.setTo(to);
helper.setText(body, true);//true indicates body is html
javaMailSender.send(message);
}
}
outlook.com检查你是不是试图假装你是其他人。
答案 1 :(得分:0)
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=senderemail
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smpt.auth=true
support.email=senderemail
用于实现邮件服务器的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 = new MimeMessageHelper(message, true);//true indicates multipart message
helper.setSubject(subject);
helper.setTo(to);
helper.setText(body, true);//true indicates body is html
helper.setFrom(env.getProperty("support.email")); //set sender email and get it from application properties
helper.addAttachment("filename", new ClassPathResource("\\static\\path")); //You can add email attachment
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!");
}
}
请尝试一下,让我们知道它是否有效
答案 2 :(得分:0)
您需要以下选项:
Scaffold.of(context)
如果仍然无法运行,则必须通过允许不安全的连接来确保源电子邮件可用于发送电子邮件。如果不是,则可能是代理。
答案 3 :(得分:0)
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-mail.outlook.com
spring.mail.password=password
spring.mail.username=username
spring.mail.properties.mail.store.protocol=pop3
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smpt.auth=true
您应该使用 POP3 协议而不是 IMAP 协议连接到 Outlook.com