Java spring boot - JavaMailSender错误

时间:2017-09-03 13:37:26

标签: java spring email spring-boot

我对Java spring很新 - 我在尝试发送测试电子邮件时遇到以下错误。

发送电子邮件时出错:

  

org.springframework.mail.MailSendException:邮件服务器连接   失败;嵌套异常是javax.mail.MessagingException:无法   连接到SMTP主机:smtp.gmail.com,port:587;         嵌套异常是:         javax.net.ssl.SSLException:无法识别的SSL消息,明文连接?失败的消息:javax.mail.MessagingException:无法   连接到SMTP主机:smtp.gmail.com,port:587;         嵌套异常是:         javax.net.ssl.SSLException:无法识别的SSL消息,明文连接?消息异常(1)是:       消息1失败:javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,port:587;         嵌套异常是:         javax.net.ssl.SSLException:无法识别的SSL消息,明文连接?“

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("set-your-recipient-email-here@gmail.com");
        helper.setText("How are you?");
        helper.setSubject("Hi");

        sender.send(message);
    }
}

application.properties设置如下 - 在测试帐户上进行测试

spring.mail.port=587
spring.mail.host=smtp.gmail.com
spring.mail.username=romanineers@gmail.com
spring.mail.password=romanineers1
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

support.mail.address=romanineers@gmail.com

4 个答案:

答案 0 :(得分:1)

application.properties中删除此行: spring.mail.properties.mail.smtp.ssl.enable=true

由于您使用的端口587用于通过TLS发送消息。如果您使用的端口465是SMTP SSL端口,则应使用上述配置。

答案 1 :(得分:0)

尝试使用@ as setTo()参数进行第一次快速测试。 然后,你可以让默认配置,你不需要太多。

spring.mail.host=smtp.gmail.com
spring.mail.username=romanineers@gmail.com
spring.mail.password=****** #hope it wasn't your real password :)
spring.mail.properties.mail.smtp.auth = true

答案 2 :(得分:0)

根据您收到的错误,只需设置spring.mail.properties.mail.smtp.ssl.enable=true 属性值为false并尝试。

答案 3 :(得分:0)

问题已解决--- javax.mail.AuthenticationFailedException is thrown while sending email in java - 必须配置gmail以允许安全性较低的应用 -

{{1}}