Java Spring Boot-JavaMailSender错误 [英] Java spring boot - JavaMailSender errors

查看:95
本文介绍了Java Spring Boot-JavaMailSender错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java spring还是很陌生-尝试发送测试电子邮件时出现以下错误.

I am fairly new to Java spring - I am getting the following errors when trying to send a test email.

发送电子邮件时出现错误:

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

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?; message exceptions (1) are: Failed message 1: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?"

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 设置如下-在测试帐户上进行测试

The application.properties settings are as follows - testing on a test account

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

推荐答案

从您的 application.properties 删除此行: spring.mail.properties.mail.smtp.ssl.enable = true

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

Since your are using port 587 which is for sending message with TLS. You should use above configuration if you are using port 465 which is a SMTP SSL port.

这篇关于Java Spring Boot-JavaMailSender错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆