Skip to content

Instantly share code, notes, and snippets.



Last active Oct 26, 2017
What would you like to do?
Send Email from AWS SES using Smtp
// make sure the receiver emails are verified:
import javax.activation.DataHandler
import javax.activation.DataSource
import javax.activation.FileDataSource
import javax.mail.Session
import javax.mail.Message
import javax.mail.MessagingException
import javax.mail.internet.AddressException
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMultipart
import javax.mail.internet.MimeUtility
import java.util.Properties
def sendAWSSESEmail(toEmails: Vector[String], fromEmail: String, subject: String, body: String): Unit = {
// get credentials from here:
val smtpUsername = "xxx"
val smtpPassword = "xxx"
val hostName = ""
val port = "25"
val properties = new Properties
val session = Session.getDefaultInstance(properties)
try {
val messageBody = body
val message = new MimeMessage(session)
message.setFrom(new InternetAddress(fromEmail))
for (toEmail <- toEmails) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail))
message.setHeader("Content-Type", "text/plain;")
message.setContent(messageBody, "text/plain")
val transport = session.getTransport("smtp")
transport.connect(hostName, smtpUsername, smtpPassword)
transport.sendMessage(message, message.getAllRecipients)
} catch {
case exception: Exception =>
println("Mail delivery failed. " + exception)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.