我想使用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上跟踪了几乎所有与此问题相关的线程,但仍然无法弄明白。请帮忙!
答案 0 :(得分:3)
您的配置中是否有可能出现拼写错误?
看看
sprint.mail.username
不应该是spring...
吗?
答案 1 :(得分:1)
启用安全性较低的应用
https://www.google.com/settings/security/lesssecureapps
仅供参考:https://support.google.com/accounts/answer/6010255?hl=en
然后重新测试发送电子邮件。