6

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
  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 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("test123@outlook.com", "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.

Ramesh Khadka
  • 484
  • 1
  • 7
  • 19
  • see https://stackoverflow.com/questions/14430962/send-javamail-using-office365 – Ori Marko Jun 07 '17 at 06:40
  • Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp-mail.outlook.com, 995; Thank you for your help. – Ramesh Khadka Jun 07 '17 at 06:43
  • @user7294900 I tried the solution provided in the given link but it didn't work. thanks for your help – Ramesh Khadka Jun 07 '17 at 09:21
  • I am getting this error now: { "timestamp": 1496828104173, "status": 500, "error": "Internal Server Error", "exception": "org.springframework.mail.MailSendException", "message": "Failed messages: javax.mail.MessagingException: Exception reading response;\n nested exception is:\n\tjava.net.SocketTimeoutException: Read timed out", "path": "/api/mail/send" } – Ramesh Khadka Jun 07 '17 at 09:41
  • 1
    setFrom() is solved my problem. You should accept the answer. If any problem, post it. Remember if you are in corporate outloo, you can not send it to outside of your organizaton. & remember mostly mail port like 25, 587 are blocked by organization. So, try to disable proxy and try with other network other than your organization network with turning off proxy – Satish Patro Apr 25 '19 at 14:07

3 Answers3

6

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.

1

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

you should connect to Outlook.com using the POP3 protocol instead of the IMAP protocol

This is JavaMAil Frequently Asked Question.

JackSlow
  • 11
  • 2
-3

You need these options:

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  

If it still doesn't run, you have to be sure that your source email is available to send emails by letting insecure connections. If not, it could be the proxy.

Community
  • 1
  • 1
German20
  • 17
  • 3