gpt4 book ai didi

java - 无法使用 Gmail SMTP 在 Spring Boot 中发送电子邮件

转载 作者:行者123 更新时间:2023-11-30 06:52:19 26 4
gpt4 key购买 nike

我想在 Spring Boot 中使用 JavaMailSender 发送电子邮件。每当我运行我的项目时,我都会收到此错误:

org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 https://support.google.com/mail/answer/14257 ps2sm10628859pab.10 - gsmtp

我没有为我的帐户使用两步验证,密码也正确,我检查过了。

这是我的 application.properties 文件:

spring.mail.host = smtp.gmail.com
sprint.mail.username = apptestacc1234@gmail.com
sprint.mail.password = *******
send.from.email= apptestacc1234@gmail.com
spring.mail.properties.mail.smtp.auth = true;
spring.mail.properties.mail.smtp.starttls.enable = true
spring.mail.properties.mail.smtp.ssl.enable = true
spring.mail.properties.mail.socketFactory.port=587
spring.mail.properties.mail.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.socketFactory.fallback=false
spring.mail.smtp.port= 587

我的邮件服务类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class MailService {

@Autowired
private JavaMailSender javaMailSender;


//public MailService(JavaMailSender javaMailSender){
// this.javaMailSender = javaMailSender;
//}


public void sendMail() throws MailException{

SimpleMailMessage mail = new SimpleMailMessage();
mail.setTo("loonaishmita@gmail.com");
mail.setFrom("apptestacc1234@gmail.com");
mail.setSubject("Test");
mail.setText("test mail");
javaMailSender.send(mail);
}

这是我的 Controller :

@Controller
@EnableAutoConfiguration
public class MailController {
@Autowired
private MailService mailService;

@RequestMapping("/api/sendmail")
@ResponseBody
private String sendMail() {
try{
mailService.sendMail();
return "mail sent";
}catch(MailException e){
e.printStackTrace();
}
return "thanks";
}

我在 stackoverflow 上关注了几乎所有与此问题相关的线程,但仍然无法弄清楚。请帮忙!

最佳答案

开启安全性较低的应用

https://www.google.com/settings/security/lesssecureapps

仅供引用:https://support.google.com/accounts/answer/6010255?hl=en

然后重新测试发送邮件。

关于java - 无法使用 Gmail SMTP 在 Spring Boot 中发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39037796/

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