简体   繁体   English

Java spring boot - JavaMailSender 错误

[英]Java spring boot - JavaMailSender errors

I am fairly new to Java spring - I am getting the following errors when trying to send a test email.我对 Java spring 相当陌生 - 尝试发送测试电子邮件时出现以下错误。

Error in sending email:发送电子邮件时出错:

org.springframework.mail.MailSendException: Mail server connection failed; org.springframework.mail.MailSendException: 邮件服务器连接失败; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;嵌套异常是 javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?.嵌套异常是:javax.net.ssl.SSLException:无法识别的 SSL 消息,纯文本连接?。 Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;失败的消息:javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?;嵌套异常是:javax.net.ssl.SSLException:无法识别的 SSL 消息,纯文本连接?; message exceptions (1) are: Failed message 1: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;消息异常 (1) 是: 失败消息 1:javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?"嵌套异常是:javax.net.ssl.SSLException:无法识别的 SSL 消息,纯文本连接?”

SimepleEmailController.java简单的电子邮件控制器.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);
    }
}

The application.properties settings are as follows - testing on a test account application.properties设置如下 - 在测试帐户上测试

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

Remove this line from your application.properties : spring.mail.properties.mail.smtp.ssl.enable=trueapplication.properties删除这一行: spring.mail.properties.mail.smtp.ssl.enable=true

Since your are using port 587 which is for sending message with TLS.由于您使用的是用于使用 TLS 发送消息的端口587 You should use above configuration if you are using port 465 which is a SMTP SSL port.如果您使用的是 SMTP SSL 端口465端口,则应使用上述配置。

Try to use your @ as setTo() param for a first quick test.尝试使用您的 @ 作为 setTo() 参数进行第一次快速测试。 Then, you can let default configuration, you don't need much of it.然后,你可以让默认配置,你不需要太多。

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

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

Problem fixed --- javax.mail.AuthenticationFailedException is thrown while sending email in java -- have to configure the gmail to allow less secure apps --问题已修复 --- 在 java 中发送电子邮件时抛出 javax.mail.AuthenticationFailedException -- 必须配置 gmail 以允许不太安全的应用程序 --

spring.mail.port=465
spring.mail.host=smtp.gmail.com
spring.mail.username=xx@gmail.com
spring.mail.password=yyyyy
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 = 465
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false
spring.mail.properties.mail.smtp.ssl.enable=false

support.mail.address=xx@gmail.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM