Java 如何使用spring boot通过outlook发送邮件?
My application.properties文件包含以下配置:-Java 如何使用spring boot通过outlook发送邮件?,java,spring,spring-boot,outlook,office365,Java,Spring,Spring Boot,Outlook,Office365,My 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
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"
}
非常感谢您提供的任何帮助。您必须使用
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);
}
}
@组件
公共类SmtpMailSender{
@值(${spring.mail.username}”)
来自的私有字符串;
@自动连线
私有JavaMailSender JavaMailSender;
public void sendMail(字符串收件人、字符串主题、字符串正文)抛出MessaginException{
MimeMessage message=javamailssender.createMimeMessage();
mimessagehelper;
helper=new mimessagehelper(message,true);//true表示多部分消息
helper.setFrom(from)/应用程序.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
@RestController
public class MailController {
@Autowired
SmtpMailSender smtpMailSender;
@RequestMapping(path = "/api/mail/send")
public void sendMail() throws MessagingException {
smtpMailSender.sendMail("test123@outlook.com", "testmail", "hello!");
}
}
用于实现邮件服务器的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);
}
}
控制器类
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
@RestController
public class MailController {
@Autowired
SmtpMailSender smtpMailSender;
@RequestMapping(path = "/api/mail/send")
public void sendMail() throws MessagingException {
smtpMailSender.sendMail("test123@outlook.com", "testmail", "hello!");
}
}
请试试这个,让我们知道它是否有效
您需要以下选项:
spring:
mail:
protocol: smtp
host: smtp.gmail.com
username: email@..
password: password..email
port: 587
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: false
test-connection: false
如果它仍然没有运行,您必须确保您的源电子邮件可以通过不安全的连接发送电子邮件。如果没有,它可能是代理。请参阅邮件服务器连接失败;嵌套异常是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.MessaginException:异常读取响应;\n嵌套的异常为:\n\tjava.net.SocketTimeoutException:读取超时”,“路径”:“/api/mail/send”}setFrom()解决了我的问题。您应该接受答案。如果有任何问题,请发布。请记住,如果您在公司outloo,则无法将其发送到组织外部。请记住,大多数邮件端口(如25587)都被组织阻止。因此,请尝试禁用代理,并在关闭pr的情况下尝试使用组织网络以外的其他网络oxymine也找不到发件人。感谢tipThanks的提示,这导致了很多问题。这里使用的电子邮件id是什么?post将office365配置为邮件服务器。