Skip to content

Instantly share code, notes, and snippets.

@geraldstanje geraldstanje/main.scala
Last active Oct 26, 2017

Embed
What would you like to do?
Send Email from AWS SES using Smtp
// make sure the receiver emails are verified:
// https://console.aws.amazon.com/ses/home?region=us-east-1#verified-senders-email:
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: https://console.aws.amazon.com/ses/home?region=us-east-1#smtp-settings:
val smtpUsername = "xxx"
val smtpPassword = "xxx"
val hostName = "email-smtp.us-east-1.amazonaws.com"
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.setSubject(subject)
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.