0

I am fairly new to Java spring - I am getting the following errors when trying to send a test email.

Error in sending email:

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?; message exceptions (1) are: Failed message 1: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?"

SimepleEmailController.java

package controller;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SimpleEmailController {

    @Autowired
    private JavaMailSender sender;

    @RequestMapping("/simpleemail")
    @ResponseBody
    String home() {
        try {
            sendEmail();
            return "Email Sent!";
        }catch(Exception ex) {
            return "Error in sending email: "+ex;
        }
    }

    private void sendEmail() throws Exception{
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);

        helper.setTo("[email protected]");
        helper.setText("How are you?");
        helper.setSubject("Hi");

        sender.send(message);
    }
}

The application.properties settings are as follows - testing on a test account

spring.mail.port=587
spring.mail.host=smtp.gmail.com
[email protected]
spring.mail.password=zzzzzzzzzzz
spring.mail.protocol=smtp
spring.mail.defaultEncoding=UTF-8

spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.socketFactory.port = 25
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = true
spring.mail.properties.mail.smtp.ssl.enable=true

[email protected]
2
  • I tried a different port - 465 -- are these configurations correct etc.. -- Commented Sep 3, 2017 at 13:42
  • Please hide your personal data from code(e.g. email login pass, host, port), because it is not secure Commented Feb 29, 2020 at 11:45

4 Answers 4

1

Remove this line from your application.properties: spring.mail.properties.mail.smtp.ssl.enable=true

Since your are using port 587 which is for sending message with TLS. You should use above configuration if you are using port 465 which is a SMTP SSL port.

Sign up to request clarification or add additional context in comments.

3 Comments

Error in sending email: org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 support.google.com/mail/answer/78754 17sm825599ljp.80 - gsmtp -- with port 465
Error in sending email: org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
- no still fail -- Error in sending email: org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 support.google.com/mail/answer/78754 100sm393966lfs.45 - gsmtp
0

Try to use your @ as setTo() param for a first quick test. Then, you can let default configuration, you don't need much of it.

spring.mail.host=smtp.gmail.com
[email protected]
spring.mail.password=****** #hope it wasn't your real password :)
spring.mail.properties.mail.smtp.auth = true

3 Comments

so take out a bulk of properties?
protocol will be auto detected, ports have conventional default values, so yes, works fine for me..
failed --- Error in sending email: org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. b75sm830640lff.61 - gsmtp ; message exceptions (1) are: Failed message 1: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. b75sm830640lff.61 - gsmtp
0

As per the error you are getting, just set spring.mail.properties.mail.smtp.ssl.enable=true property value to false and try.

1 Comment

I think I tried - that - but will take another look
0

Problem fixed --- javax.mail.AuthenticationFailedException is thrown while sending email in java -- have to configure the gmail to allow less secure apps --

spring.mail.port=465
spring.mail.host=smtp.gmail.com
[email protected]
spring.mail.password=yyyyy
spring.mail.protocol=smtp
spring.mail.defaultEncoding=UTF-8

spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false
spring.mail.properties.mail.smtp.ssl.enable=false

[email protected]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.