Saturday, 24 October 2015

Scala Email Sender

Basically, it use javax.mail library. Add libraryDependencies += "com.sun.mail" % "javax.mail" % "1.5.4" in build.sbt.

Please Note:
1. java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
Use "com.sum.mail:javax.mail" instead of "javax.mail:javax.mail-api"

2. Must issue a STARTTLS command first.

props.put("mail.smtp.starttls.enable","true");
3. javax.mail.AuthenticationFailedException: failed to connect, no password specified?

val session = Session.getDefaultInstance(properties, new SMTPAuthenticator())




import javax.mail._
import javax.mail.internet._
import java.util.Date
import java.util.Properties
import scala.collection.JavaConversions._

class MailAgent(to: String, cc: String, from: String, password: String,subject: String, content: String)
{
  var message: Message = createMessage
  message.setFrom(new InternetAddress(from))
  setToCcRecipients

  message.setSentDate(new Date())
  message.setSubject(subject)
  message.setText(content)

  // throws MessagingException
  def sendMessage {
    Transport.send(message)
  }

  def createMessage: Message = {
    val properties = new Properties()

    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");
    //properties.put("mail.debug", "true");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable","true");
    properties.put("mail.smtp.EnableSSL.enable","true");

    val session = Session.getDefaultInstance(properties, new SMTPAuthenticator())
    return new MimeMessage(session)
  }

  // throws AddressException, MessagingException
  def setToCcRecipients {
    setMessageRecipients(to, Message.RecipientType.TO)
    if (cc != null) {
      setMessageRecipients(cc, Message.RecipientType.CC)
    }

  }

  // throws AddressException, MessagingException
  def setMessageRecipients(recipient: String, recipientType: Message.RecipientType) {
    // had to do the asInstanceOf[...] call here to make scala happy
    val addressArray = buildInternetAddressArray(recipient).asInstanceOf[Array[Address]]
    if ((addressArray != null) && (addressArray.length > 0))
    {
      message.setRecipients(recipientType, addressArray)
    }
  }

  // throws AddressException
  def buildInternetAddressArray(address: String): Array[InternetAddress] = {
    // could test for a null or blank String but I'm letting parse just throw an exception
    return InternetAddress.parse(address)
  }

  private class SMTPAuthenticator extends Authenticator {
    override def getPasswordAuthentication():PasswordAuthentication = {
      new PasswordAuthentication(from, password)
    }
  }
}



Reference:
http://stackoverflow.com/questions/24807472/java-mail-issue-with-session-getinstance
http://stackoverflow.com/questions/6610572/javax-mail-authenticationfailedexception-failed-to-connect-no-password-specifi
http://stackoverflow.com/questions/26774057/how-to-send-emails-via-spring/29676871#29676871

No comments:

Post a Comment