gpt4 book ai didi

Java Spring Boot - JavaMailSender 错误

转载 作者:行者123 更新时间:2023-12-02 12:17:52 24 4
gpt4 key购买 nike

我对 Java spring 相当陌生 - 在尝试发送测试电子邮件时遇到以下错误。

发送电子邮件时出错:

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

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 端口。

关于Java Spring Boot - JavaMailSender 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46023973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com