Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import javax.activation.DataHandler
import javax.activation.DataSource
import javax.activation.FileDataSource
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 = {
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.