繁体   English   中英

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

[英]Unable to send emails in Spring Boot using Gmail SMTP

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

org.springframework.mail.MailSendException:失败消息:com.sun.mail.smtp.SMTPSendFailedException:530-5.5.1需要身份验证。 要了解更多信息,请访问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

我的MailService类:

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
@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上关注了与该问题相关的几乎所有线程,但仍然无法解决。 请帮忙!

可能是您配置中的错字?

看着

 sprint.mail.username 

不是spring...吗?

开启安全性较低的应用

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

仅供参考: https : //support.google.com/accounts/answer/6010255?hl=zh_CN

然后重新测试发送电子邮件。

暂无
暂无

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

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