Java 无法在Spring Boot中使用Gmail SMTP发送电子邮件
我想在Spring Boot中使用Java 无法在Spring Boot中使用Gmail SMTP发送电子邮件,java,spring,email,spring-mvc,spring-boot,Java,Spring,Email,Spring Mvc,Spring Boot,我想在Spring Boot中使用JavaMailSender发送电子邮件。每当我运行项目时,都会出现以下错误: org.springframework.mail.MailSendException:失败的邮件:com.sun.mail.smtp.SMTPSendFailedException:530-5.5.1需要身份验证。了解更多信息,请访问 530 5.5.1 ps2sm10628859pab.10-gsmtp 我没有对我的帐户使用两步验证,密码也是正确的,我检查过了 这是我的applic
JavaMailSender
发送电子邮件。每当我运行项目时,都会出现以下错误:
org.springframework.mail.MailSendException:失败的邮件:com.sun.mail.smtp.SMTPSendFailedException:530-5.5.1需要身份验证。了解更多信息,请访问
530 5.5.1 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
@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
是不是应该是春天…?打开不太安全的应用程序 供参考:
然后重新测试发送电子邮件。您是否将gmail帐户配置为接受IMAP/POP3请求?这是一个实际问题,有很多代码可用,但谷歌拒绝登录。