-1

I'm working with spring-boot in back end , i tried to send an email to a gmail account using smtp but i'm fascing some errors .

Here is my code following this tutorial :

application.properties :

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=xxx@gmail.com
spring.mail.password=xxxpwd
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Class MailObject.java :

    package interv.Web.Notifications;

import org.hibernate.validator.constraints.Email;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class MailObject {

    @Email
    @NotNull
    @Size(min = 1, message = "Please, set an email address to send the message to it")
    private String to;
    private String subject;
    private String text;

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

MailController.java :

    @Controller
public class MailController {

    @Autowired
    public EmailServiceImpl emailService;

    @Autowired
    public SimpleMailMessage template;

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public String createMail(@RequestBody MailObject mailObject) {


        emailService.sendSimpleMessage(mailObject.getTo(),
                mailObject.getSubject(), mailObject.getText());

        return "that was succesfull";

    }
}

EmailService.java

public interface EmailService {

    void sendSimpleMessage(String to,
                           String subject,
                           String text);
    void sendSimpleMessageUsingTemplate(String to,
                                        String subject,
                                        SimpleMailMessage template,
                                        String templateArgs);

}

EmailServiceImpl.java

@Component
public class EmailServiceImpl implements EmailService{

    @Autowired
    public JavaMailSender emailSender;

    @Override
    public void sendSimpleMessage(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);

        emailSender.send(message);
    }

    @Override
    public void sendSimpleMessageUsingTemplate(String to, String subject, SimpleMailMessage template, String templateArgs) {
        String text = String.format(template.getText(), templateArgs);
        sendSimpleMessage(to, subject, text);
    }
}

While running i get no errors in console , but when i try to send an email i get this error :

servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target. Failed messages: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target] with root cause

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141) ~[na:1.8.0_161]
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126) ~[na:1.8.0_161]
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280) ~[na:1.8.0_161]

I don't understand why i'm getting this error : any idea ?

user207421
  • 305,947
  • 44
  • 307
  • 483
dEs12ZER
  • 788
  • 4
  • 24
  • 51

2 Answers2

1

I think the reason is that Gmail blocks Access for less secure apps by default for security reasons check this similar question it's answer may help you Error sending email with gmail

Asta
  • 32
  • 4
  • That was helpful , i'm not getting this error now , but another one if you can see the post please , i have edited it :) – dEs12ZER Jun 02 '18 at 00:24
  • Check this Answer https://stackoverflow.com/questions/16115453/javamail-could-not-convert-socket-to-tls-gmail – Asta Jun 02 '18 at 00:57
0

The error clearly says ‘UnknownHost’. Check if your app has access to the internet and is able to resolve the name.