1
votes

Need to send a mail from gmail using spring boot

But i'm getting an error like

This application has no explicit mapping for /error, so you are seeing this as a fallback.

There was an unexpected error (type=Internal Server Error, status=500). Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout 5000; nested exception is: java.net.SocketTimeoutException: connect timed out. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout 5000; nested exception is: java.net.SocketTimeoutException: connect timed out

application.properties

spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
spring.mail.password=********
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000

Controller

@Autowired
private JavaMailSender sender;

@RequestMapping(value="/sendMail/{mail}")
public String sendMail(@PathVariable String mail) {
    MimeMessage message = sender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message);

    try {
        helper.setTo(mail);
        helper.setText("Greetings :)");
        helper.setSubject("Mail From Spring Boot");
    } catch (MessagingException e) {
        e.printStackTrace();
        return "Error while sending mail ..";
    }
    sender.send(message);
    return "Mail Sent Success!";
}

Also allowed less secured apps in mail setting

3
I suppose you've checked this code from several scenarios and discarded that it's a network issue?Xtreme Biker

3 Answers

0
votes

Use this code snippet instead

public class ExceptionMailer{
    private String smtpHost;
    private String smtpPort;
    private String senderMail;
    private String password;
    static final Properties props = new Properties();

ExceptionMailer(){
    this.smtpHost = "smtp.gmail.com";
    this.smtpPort = "465";
    this.senderMail = "[email protected]";
    this.password = "xxxxxxxx";

    props.put("mail.smtp.host", this.smtpHost);
    props.put("mail.smtp.socketFactory.port", this.smtpPort);
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", this.smtpPort);
}

public void send(){
           //get authentication 
        Session session = Session.getDefaultInstance(props,new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication(){
                    return new PasswordAuthentication(senderMail, password);
                }
        });

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(senderMail));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));
message.setSubject(MAIL_SUBJECT);
message.setContent("This is a message", "text/html; charset=utf-8");
Transport.send(message);

}

}
0
votes

The below properties are required for you to send email with Spring.

spring.mail.host=
spring.mail.port=465
spring.mail.protocol=smtps

#Mail server Username & Password
spring.mail.username=
spring.mail.password=

spring.mail.properties.mail.transport.protocol=smtps
spring.mail.properties.mail.smtps.auth=true
spring.mail.properties.mail.smtps.starttls.enable=true
spring.mail.properties.mail.smtps.timeout=8000

You can refer to the below class to send an email with Spring.

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class SendEmailService  {

    @Autowired
    private JavaMailSender mailSender;

    public void sendEmail(final UserObject userObject) {            

        String toAddress = userObject.getEmail();
        String subjectText = userObject.getSubject();
        SimpleMailMessage emailMessage = composeEmail(toAddress, subjectText);

        mailSender.send(emailMessage);
    }

    private SimpleMailMessage composeEmail(final String toAddress, final String subjectText) {

        final SimpleMailMessage email = new SimpleMailMessage();    
        email.setTo(toAddress);
        email.setSubject(subjectText);
        email.setText("Some Text");
        email.setFrom("From Address");

        return email;
    }

}
0
votes

Since the underlying problem is a socket timeout, most likely the problem is a firewall that's preventing you from connecting directly. The JavaMail FAQ has connection debugging tips and instructions for connecting through a proxy server.