Send Emails from an application
Update Spring Boot application configuration:¶
- Update your application.properties or application.yml file with the Amazon SES SMTP settings, as shown in the previous response. Replace region, your_smtp_username, and your_smtp_password with your actual SES SMTP credentials.
Spring configuration should look like
spring.mail.host=email-smtp.{region}.amazonaws.com
spring.mail.port=587
spring.mail.username=your_smtp_username
spring.mail.password=your_smtp_password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
Please note you need to replace {region}
with the actual region, such as us-east-1
Postman Request¶
This is what our post request will look like using postman.
SpringBoot Java Files¶
Here is our REST controller which receives the request
@RestController
public class EmailController {
@Autowired
private EmailService emailService;
@PostMapping("/email")
public ResponseEntity<Void> sendEmail(@RequestBody Map<String, String> param) {
emailService.sendSimpleMail(
param.get("to"),
param.get("subject"),
param.get("content")
);
return ResponseEntity.ok().build();
}
}
EmailService
which sends the email
@Service
public class EmailService {
@Autowired
private JavaMailSender mailSender;
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
String fromName = "Thomas Wilde";
String fromEmail = "support@wildebeastmedia.com";
message.setFrom(fromName + " <" + fromEmail + ">");
message.setTo(to);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
}
Resulting Email¶
Here is the email received to my personal gmail