package object mail { | |
implicit def stringToSeq(single: String): Seq[String] = Seq(single) | |
implicit def liftToOption[T](t: T): Option[T] = Some(t) | |
sealed abstract class MailType | |
case object Plain extends MailType | |
case object Rich extends MailType | |
case object MultiPart extends MailType | |
case class Mail( | |
from: (String, String), // (email -> name) | |
to: Seq[String], | |
cc: Seq[String] = Seq.empty, | |
bcc: Seq[String] = Seq.empty, | |
subject: String, | |
message: String, | |
richMessage: Option[String] = None, | |
attachment: Option[(java.io.File)] = None | |
) | |
object send { | |
def a(mail: Mail) { | |
import org.apache.commons.mail._ | |
val format = | |
if (mail.attachment.isDefined) MultiPart | |
else if (mail.richMessage.isDefined) Rich | |
else Plain | |
val commonsMail: Email = format match { | |
case Plain => new SimpleEmail().setMsg(mail.message) | |
case Rich => new HtmlEmail().setHtmlMsg(mail.richMessage.get).setTextMsg(mail.message) | |
case MultiPart => { | |
val attachment = new EmailAttachment() | |
attachment.setPath(mail.attachment.get.getAbsolutePath) | |
attachment.setDisposition(EmailAttachment.ATTACHMENT) | |
attachment.setName(mail.attachment.get.getName) | |
new MultiPartEmail().attach(attachment).setMsg(mail.message) | |
} | |
} | |
// TODO Set authentication from your configuration, sys properties or w/e | |
// Can't add these via fluent API because it produces exceptions | |
mail.to foreach (commonsMail.addTo(_)) | |
mail.cc foreach (commonsMail.addCc(_)) | |
mail.bcc foreach (commonsMail.addBcc(_)) | |
commonsMail. | |
setFrom(mail.from._1, mail.from._2). | |
setSubject(mail.subject). | |
send() | |
} | |
} | |
} |
package something | |
object Demo { | |
import mail._ | |
send a new Mail ( | |
from = ("john.smith@mycompany.com", "John Smith"), | |
to = "boss@mycompany.com", | |
cc = "hr@mycompany.com", | |
subject = "Import stuff", | |
message = "Dear Boss..." | |
) | |
send a new Mail ( | |
from = "john.smith@mycompany.com" -> "John Smith", | |
to = Seq("dev@mycompany.com", "marketing@mycompany.com"), | |
subject = "Our New Strategy (tm)", | |
message = "Please find attach the latest strategy document.", | |
richMessage = "Here's the <blink>latest</blink> <strong>Strategy</strong>..." | |
) | |
send a new Mail ( | |
from = "john.smith@mycompany.com" -> "John Smith", | |
to = "dev@mycompany.com" :: "marketing@mycompany.com" :: Nil, | |
subject = "Our 5-year plan", | |
message = "Here is the presentation with the stuff we're going to for the next five years.", | |
attachment = new java.io.File("/home/boss/important-presentation.ppt") | |
) | |
} |
This comment has been minimized.
This comment has been minimized.
Thanks man, really appreciate |
This comment has been minimized.
This comment has been minimized.
Thank you for sharing this |
This comment has been minimized.
This comment has been minimized.
Hi, thanks for sharing. What's the license on this code? Can it be used in commercial projects? |
This comment has been minimized.
This comment has been minimized.
Can you add a license header? |
This comment has been minimized.
This comment has been minimized.
Thank you for sharing this |
This comment has been minimized.
This comment has been minimized.
Hi, I'm posting html, but get html content like attachments. What is the problem?
Thanks, Aziz |
This comment has been minimized.
This comment has been minimized.
This is awesome |
This comment has been minimized.
This comment has been minimized.
Thank you for sharing this |
This comment has been minimized.
This comment has been minimized.
this is so fluent. thanks @mariussoutier |
This comment has been minimized.
This comment has been minimized.
Beautifully written. Thanks. |
This comment has been minimized.
This comment has been minimized.
Thanks a lot |
This comment has been minimized.
This comment has been minimized.
Thanks for sharing! |
This comment has been minimized.
This comment has been minimized.
Hi mariussoutier, I am getting below error Exception in thread "main" org.apache.commons.mail.EmailException: Cannot find valid hostname for mail session |
This comment has been minimized.
This comment has been minimized.
You need to set email server hostname by commonsMail.setHostName("your email server host") |
This comment has been minimized.
This comment has been minimized.
Tiny nit: The example should be |
This comment has been minimized.
This comment has been minimized.
Thanks a lot man |
This comment has been minimized.
This comment has been minimized.
sample for gmail and multi file support package object Mail {
implicit def stringToSeq(single: String): Seq[String] = Seq(single)
implicit def liftToOption[T](t: T): Option[T] = Some(t)
sealed abstract class MailType
case object Plain extends MailType
case object Rich extends MailType
case object MultiPart extends MailType
case class Mail(
from: (String, String), // (email -> name)
to: Seq[String],
cc: Seq[String] = Seq.empty,
bcc: Seq[String] = Seq.empty,
subject: String,
message: String,
richMessage: Option[String] = None,
attachments: Seq[(java.io.File)] = Seq.empty
)
object send {
def a(mail: Mail) {
import org.apache.commons.mail._
val format =
if (mail.attachments.nonEmpty) MultiPart
else if (mail.richMessage.isDefined) Rich
else Plain
val commonsMail: Email = format match {
case Plain => new SimpleEmail().setMsg(mail.message)
case Rich => new HtmlEmail().setHtmlMsg(mail.richMessage.get).setTextMsg(mail.message)
case MultiPart => {
val multipartEmail = new MultiPartEmail()
mail.attachments.foreach { file =>
val attachment = new EmailAttachment()
attachment.setPath(file.getAbsolutePath)
attachment.setDisposition(EmailAttachment.ATTACHMENT)
attachment.setName(file.getName)
multipartEmail.attach(attachment)
}
multipartEmail.setMsg(mail.message)
}
}
// TODO Set authentication from your configuration, sys properties or w/e
// Can't add these via fluent API because it produces exceptions
mail.to foreach (commonsMail.addTo(_))
mail.cc foreach (commonsMail.addCc(_))
mail.bcc foreach (commonsMail.addBcc(_))
// gmail config
commonsMail.setHostName("smtp.googlemail.com")
commonsMail.setAuthentication("email","pass")
commonsMail.setSSLOnConnect(true)
commonsMail.setSmtpPort(465)
commonsMail.
setFrom(mail.from._1, mail.from._2).
setSubject(mail.subject).
send()
}
}
} |
This comment has been minimized.
This comment has been minimized.
Hi All, Thanks, advance. Is there any possible ways to send Spark dataframe via mail, in normal or HTML using this Code? |
This comment has been minimized.
This comment has been minimized.
Hi All, Error during processing of request: 'Sending the email to the following server failed : smtp.gmail.com:465'. Completing with 500 Internal Server Error response. To change default exception handling behavior, provide a custom ExceptionHandler. ConfigurationcommonsMail.setHostName("smtp.gmail.com") |
This comment has been minimized.
This comment has been minimized.
I got the below error while using the given code snippet.
As well found the solution for the issue using the below link So is it safe? |
This comment has been minimized.
Thank you for sharing this.