【问题标题】:Gmail Api in spring boot : javax.mail.MessagingException: Could not convert socket to TLS春季启动中的 Gmail Api:javax.mail.MessagingException:无法将套接字转换为 TLS
【发布时间】:2018-06-01 23:40:48
【问题描述】:

我正在后端使用 spring-boot,我尝试使用 smtp 向 gmail 帐户发送电子邮件,但我遇到了一些错误。

这是我在tutorial 之后的代码:

应用程序属性:

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

类 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);
    }
}

运行时控制台中没有错误,但是当我尝试发送电子邮件时出现此错误:

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]

我不明白为什么会出现此错误:知道吗?

【问题讨论】:

标签: java spring spring-boot gmail jakarta-mail


【解决方案1】:

我认为原因是 Gmail 出于安全原因默认阻止访问不太安全的应用程序 检查这个类似的问题它的答案可能会帮助你 Error sending email with gmail

【讨论】:

【解决方案2】:

错误清楚地显示“UnknownHost”。检查您的应用程序是否可以访问互联网并能够解析名称。

【讨论】:

  • 对不起,我有个问题,我的本地应用程序不能访问 smtp 吗?
  • 它可以在本地主机上运行,​​但需要访问互联网才能与 smtp 服务器通信。
  • 啊,我的应用可以上网了
猜你喜欢
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 2023-03-08
  • 1970-01-01
  • 2018-12-17
  • 1970-01-01
  • 2013-06-22
相关资源
最近更新 更多