Skip to content

Instantly share code, notes, and snippets.

@mmafrar
Created April 29, 2021 19:04
Show Gist options
  • Save mmafrar/9e6db5a8b4ae3ee1ff45ee703c6be7eb to your computer and use it in GitHub Desktop.
Save mmafrar/9e6db5a8b4ae3ee1ff45ee703c6be7eb to your computer and use it in GitHub Desktop.
Sending emails in Spring Boot with Amazon SES SMTP Interface
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