Created
March 1, 2018 19:13
-
-
Save Kenoshen/f61bb92450b469a55e6acf79808ad35c to your computer and use it in GitHub Desktop.
Send an HTML email using gmail's free SMTP server
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
| package utils | |
| /* in build.sbt | |
| libraryDependencies += "javax.mail" % "javax.mail-api" % "1.6.1" | |
| libraryDependencies += "com.sun.mail" % "javax.mail" % "1.6.0" | |
| */ | |
| import javax.mail._ | |
| import javax.mail.internet._ | |
| import java.util.Date | |
| import java.util.Properties | |
| import javax.activation.DataHandler | |
| import javax.mail.util.ByteArrayDataSource | |
| object SendEmail { | |
| private val from: String = "[email protected]" | |
| private val password: String = "password123" | |
| private val props: Properties = System.getProperties | |
| props.put("mail.smtp.host", "smtp.gmail.com") | |
| props.put("mail.smtp.auth", "true") | |
| props.put("mail.smtp.port", "465") | |
| props.put("mail.smtp.socketFactory.port", "465") | |
| props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory") | |
| def apply(subject:String, content:String, to: List[String], cc: List[String] = Nil, bcc: List[String] = Nil): Unit = { | |
| val message: Message = createMessage | |
| message.setSubject(subject) | |
| setMessageBody(message, content) | |
| setMessageRecipients(message, to, Message.RecipientType.TO) | |
| setMessageRecipients(message, cc, Message.RecipientType.CC) | |
| setMessageRecipients(message, bcc, Message.RecipientType.BCC) | |
| Transport.send(message, from, password) | |
| } | |
| private def createMessage: Message = { | |
| val session = Session.getInstance(props) | |
| val msg = new MimeMessage(session) | |
| msg.setFrom(new InternetAddress(from)) | |
| msg.setSentDate(new Date()) | |
| msg | |
| } | |
| // throws AddressException, MessagingException | |
| private def setMessageRecipients(msg:Message, recipients: List[String], recipientType: Message.RecipientType): Unit = { | |
| if (recipients.nonEmpty) { | |
| val addressArray = InternetAddress.parse(recipients.mkString(",")).asInstanceOf[Array[Address]] | |
| if (addressArray != null && addressArray.nonEmpty) { | |
| msg.setRecipients(recipientType, addressArray) | |
| } | |
| } | |
| } | |
| private def setMessageBody(msg: Message, content:String): Unit = { | |
| val subject = msg.getSubject | |
| val sb = new StringBuffer | |
| sb.append("<HTML>\n") | |
| .append("<HEAD>\n") | |
| .append("<TITLE>\n") | |
| .append(subject + "\n") | |
| .append("</TITLE>\n") | |
| .append("</HEAD>\n") | |
| .append("<BODY>\n") | |
| .append(content).append("\n") | |
| .append("</BODY>\n") | |
| .append("</HTML>\n") | |
| msg.setDataHandler(new DataHandler(new ByteArrayDataSource(sb.toString, "text/html"))) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment