How to send mail via outlook using spring boot?
17,965
Solution 1
You must specify the sender
using setFrom
method to perform authentication on 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 checks that you're not trying to pretend you're somebody else.
Solution 2
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 classes for implementing the mail servers are :
@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);
}
}
Controller Class
@RestController
public class MailController {
@Autowired
SmtpMailSender smtpMailSender;
@RequestMapping(path = "/api/mail/send")
public void sendMail() throws MessagingException {
smtpMailSender.sendMail("[email protected]", "testmail", "hello!");
}
}
Please try this and let us know if it works
Author by
Ramesh Khadka
Updated on June 26, 2022Comments
-
Ramesh Khadka almost 2 years
My application.properties file contains following configuration :-
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 [email protected] spring.mail.properties.mail.smtp.starttls.enable=true security.require-ssl=true spring.mail.properties.mail.smpt.auth=true
Java classes for implemting the mail servers are :
@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); } }
My controller class is :
@RestController public class MailController { @Autowired SmtpMailSender smtpMailSender; @RequestMapping(path = "/api/mail/send") public void sendMail() throws MessagingException { smtpMailSender.sendMail("[email protected]", "testmail", "hello!"); } }
when I send get request (/api/mail/send) following error occurs:
{ "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" }
Any help would be heartily appreciated.
-
marionmaiden over 4 yearsmine was also missing the sender. Thanks for the tip
-
abhinav3414 almost 4 yearspost is to configure office365 as mail server.
-
Kumar Abhishek almost 4 yearsThanks for the tip this from had caused a lot of problem.