Klapsa2503
Klapsa2503

Reputation: 919

How to set JavaMail properties in Play-Mailer

I wonder how to set JavaMail properties such as:

mail.mime.address.strict

in Play-Mailer plugin.

Upvotes: 3

Views: 485

Answers (2)

andyczerwonka
andyczerwonka

Reputation: 4260

Not directly an answer, but it does show how to set the properties for raw javax.mail. This approach is simple, because you don't need a Play plugin to solve this and I always opt for simplicity and less infrastructure when possible.

Start by including it in your build.sbt

"javax.mail" % "mail" % "1.4.5"

and off you go...

import javax.mail._
import javax.mail.internet._

use the standard Play configuration system for whatever parameters you need

private val host = config.getOptional[String]("smtp.host").getOrElse("localhost")
private val port = config.getOptional[String]("smtp.port").getOrElse("25")
private val account = config.getOptional[String]("smtp.account").getOrElse("not configured")

setup your mail properties once you've pulled them from config...

val props = System.getProperties
props.setProperty("mail.smtp.host", host)
props.setProperty("mail.smtp.port", port)
props.setProperty("mail.smtp.auth", "true")
props.setProperty("mail.smtp.starttls.enable", "true")

setup your auth...

val auth = new Authenticator() {
  override def getPasswordAuthentication: PasswordAuthentication = {
    new PasswordAuthentication(account, passwd)
  }
}

and send your message

def send(toAddress: String, subject: String, htmlBody: String): Unit = {
  val session = Session.getInstance(props, auth)
  val msg = new MimeMessage(session)
  msg.setFrom(new InternetAddress(account, "Someone"))
  msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress))
  msg.setSubject(subject)
  msg.setSentDate(new Date())
  val multiPart = new MimeMultipart("related")
  val htmlBodyPart = new MimeBodyPart()
  htmlBodyPart.setContent(htmlBody, "text/html")
  multiPart.addBodyPart(htmlBodyPart)
  msg.setContent(multiPart)
  Logger.info(s"Sending '$subject' to $toAddress")
  Transport.send(msg)
}

That should do it. You should probably wrap the whole thing in a Try monad given this can throw exceptions. Also, if you need to send attachments, it's already a multi-part message, you just add add more parts to the html message.

Upvotes: 3

Klapsa2503
Klapsa2503

Reputation: 919

There is probably no such a possibility. Below there is SMTPMailer implementation:

private lazy val instance = {
  if (smtpConfiguration.mock) {
    new MockMailer()
  } else {
    new CommonsMailer(smtpConfiguration) {
      override def send(email: MultiPartEmail): String = email.send()
      override def createMultiPartEmail(): MultiPartEmail = new MultiPartEmail()
      override def createHtmlEmail(): HtmlEmail = new HtmlEmail()
    }
  }
}

and properties are set deeper in email.send() which uses commons-email from apache. The only way to set those properties is to set those properties in System properties as they are used later:

final Properties properties = new Properties(System.getProperties());

but they may be overriden later as in the next lines there are some properties being set:

properties.setProperty(MAIL_PORT, this.smtpPort);

Upvotes: 2

Related Questions