I implemented gmail service in spring boot. i configured spring.mail.password is app password but it still threw an exception
jakarta.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. For more information, go to 535 5.7.8 https://support.google.com/mail/?p=BadCredentials 98e67ed59e1d1-2bf80503816sm5287295a91.5 – gsmtp.
I have searched for a while but it’s not work at all.
my application’s configuration
spring:
profiles:
active: dev
mail:
host: smtp.gmail.com
port: 587
username: ${GMAIL_USERNAME}
password: ${GMAIL_PASSWORD}
properties:
mail:
transport:
protocol: smtp
smtp:
auth: true
starttls:
enable: true
required: true
GMAIL_PASSWORD: qeripdncnmpfersq
here is my simple mail service:
@Service
public class MailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Async
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
}
mail controller
@RestController
@AllArgsConstructor
public class AppController {
private MailService mailService;
@PostMapping("/sendmail")
public ResponseEntity<?> sendEmail(@RequestBody String to){
try{
mailService.sendSimpleMail(to,"This is a simple mail","This is a simple mail");
return ResponseEntity.ok().build();
}catch (Exception e){
e.printStackTrace();
return ResponseEntity.badRequest().build();
}
}
}
i tried creating others app password, but nothing change. it still cannot connect.