Created
October 26, 2017 15:33
-
-
Save geraldstanje/5adedf63b7ac9108453572a6351e72f6 to your computer and use it in GitHub Desktop.
This file contains 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
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