Last active
October 26, 2017 17:24
-
-
Save geraldstanje/291e090d6fb1fa72f7838148771cd724 to your computer and use it in GitHub Desktop.
Send Email from AWS SES using Smtp
This file contains hidden or 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
// 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