Search code examples
javascalaapache-commons

Apache Mailer throwing compiler errors in Scala?


Created a simple Apache Commons Email object and went to compile it. Imported: import org.apache.commons.mail._ and javax.mail.internet.MimeMessage and also did the following:

  def sendVerification(user: User) = {
    val email = new SimpleEmail()
        email.setHostName("smtp.sendgrid.net")
        email.setSmtpPort(465)
        email.setAuthenticator(new DefaultAuthenticator("user", "pass"))
        email.setSSLOnConnect(true)
        email.setFrom("Community Admin <[email protected]>")
        email.setSubject("TestMail")
        email.setMsg("""Welcome %s!

        Thanks for signing up. This email is to confirm your email address.

        Cheers,
        Admin Team
        """ format user.firstName)
        email.addTo(user.email)
        email.send()
  }

Errors are as following:

[error] C:\Users\path\to\AccountService\models\User.scala:43: object mail is not a member of package javax
[error] import javax.mail.internet.MimeMessage
[error]              ^
[error] error while loading Email, Missing dependency 'class javax.mail.internet.MimeMessage', required by C:\Users\path\lib\commons-email-1.2.jar(org/apache/commons/mail/Email.class)
[error] C:\Users\path\com\threetierlogic\AccountService\models\User.scala:256: value setHostName is not a member of org.apache.commons.mail.SimpleEmail
[error]                 email.setHostName("smtp.sendgrid.net")

However, when I go to compile it throws errors on every single method called on val email. Is this an issue related to mutability? Or has something changed in version 1.3?


Solution

  • Mel Nicholson was pointing towards the right issue, it is related to a dependency not being fetched. I didn't realize that it didn't come with the Apache JAR I downloaded. So instead I simply added it to my sbt Build.scala and voila! It compiled.

    val commonsMail = "org.apache.commons" % "commons-email" % "1.3"