Reputation: 839
I want to send emails in Spring Boot using JavaMailSender
. Whenever I run my project I get this error:
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
I'm not using 2-step verification for my account and the password is right too, I checked it.
This is my application.properties file:
spring.mail.host = smtp.gmail.com
sprint.mail.username = [email protected]
sprint.mail.password = *******
send.from.email= [email protected]
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
My MailService class:
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("[email protected]");
mail.setFrom("[email protected]");
mail.setSubject("Test");
mail.setText("test mail");
javaMailSender.send(mail);
}
}
This is my 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";
}
}
I followed almost all the threads related to this problem on stackoverflow but still not able to figure it out. Please help!
Upvotes: 2
Views: 6236
Reputation: 52498
Turn on Less secure apps
https://www.google.com/settings/security/lesssecureapps
FYI: https://support.google.com/accounts/answer/6010255?hl=en
Then re-test sending email.
Upvotes: 5
Reputation: 24251
Any chance that might be a typo in your configuration?
Look at
sprint.mail.username
Shouldn't it be spring...
?
Upvotes: 4