首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Gmail Api在spring boot中: javax.mail.MessagingException:无法将套接字转换为TLS

Gmail Api在spring boot中: javax.mail.MessagingException:无法将套接字转换为TLS
EN

Stack Overflow用户
提问于 2018-06-02 07:40:49
回答 2查看 2.9K关注 0票数 -1

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

下面是我在这个tutorial后面的代码:

application.properties:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
    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:

代码语言:javascript
复制
    @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

代码语言:javascript
复制
public interface EmailService {

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

}

EmailServiceImpl.java

代码语言:javascript
复制
@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);
    }
}

在运行时,我在控制台中没有收到错误,但当我尝试发送电子邮件时,我收到以下错误:

代码语言:javascript
复制
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]

我不明白为什么我会得到这个错误:你知道吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-02 07:53:05

我认为原因是出于安全原因,Gmail默认阻止访问安全性较低的应用程序检查类似的问题它的答案可能会帮助您Error sending email with gmail

票数 1
EN

Stack Overflow用户

发布于 2018-06-02 07:47:25

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50652182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档