Created
April 29, 2021 19:04
-
-
Save mmafrar/9e6db5a8b4ae3ee1ff45ee703c6be7eb to your computer and use it in GitHub Desktop.
Sending emails in Spring Boot with Amazon SES SMTP Interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.mail.javamail.JavaMailSender; | |
import org.springframework.mail.javamail.MimeMessageHelper; | |
import org.springframework.scheduling.annotation.Async; | |
import org.springframework.stereotype.Service; | |
import javax.mail.MessagingException; | |
import javax.mail.internet.MimeMessage; | |
import java.io.UnsupportedEncodingException; | |
@Service | |
public class EmailService { | |
@Value("${from.email.address}") | |
private String fromEmailAddress; | |
@Autowired | |
private JavaMailSender mailSender; | |
@Async | |
public void sendEmail(String recipient, String subject, String content) throws UnsupportedEncodingException, MessagingException { | |
MimeMessage message = mailSender.createMimeMessage(); | |
MimeMessageHelper helper = new MimeMessageHelper(message); | |
helper.setFrom(fromEmailAddress, "My Email Address"); | |
helper.setTo(recipient); | |
helper.setSubject(subject); | |
helper.setText(content, true); | |
mailSender.send(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment