Play Framework 2.2.0 Java Email

3.8k views Asked by At

I'm using the Play Framework 2.2.0 with java 7.4.

I've been trying all week to find an email module or package that I can use to send an email from my play application. I have tried many different packages and each time I get either a package not found exception, a compilation error or package not found exception.

I have had many variations of the Build.scala, and the Build.sbt. I also tried unmanaged dependencies and managed dependencies pulling from 'maven' I believe it's called and pulling from my /lib directory respectively. The packages I have downloaded to my /lib directory are:

commons-io-2.3

javax.mail

play-plugins-mailer_2.2.0

My versions of my Build.scala have been:

Build.scala

1st attempt:

import sbt._
import Keys._

object ApplicationBuild extends Build {

    val appName         = "Asset Manager"
    val appVersion      = "1.0"

    val appDependencies = Seq(
        "mysql" % "mysql-connector-java" % "5.1.27",
        "org.scala-tools" %% "scala-stm" % "0.3",
        "org.apache.derby" % "derby" % "10.4.1.3" % "test",
        "org.apache.commons" % "commons-email" % "1.3.1",
        "commons-io" % "commons-io" % "2.3"
    )
}

2nd attempt:

import sbt._
import Keys._

object ApplicationBuild extends Build {

    lazy val buildVersion = "2.2.0"
    lazy val playVersion = "2.2.0"

    val appName         = "Asset Manager"
    val appVersion      = "1.0"

    val appDependencies = Seq(
        "mysql" % "mysql-connector-java" % "5.1.27",
        "org.scala-tools" %% "scala-stm" % "0.3",
        "org.apache.derby" % "derby" % "10.4.1.3" % "test",
        "commons-io" % "commons-io" % "2.3"
    )

    libraryDependencies += "org.apache.commons" % "commons-email" % "1.3.1";
    libraryDependencies += "com.typesafe" %% "play-plugins-util" % buildVersion;

}

My Emailing Java File Parts.java:

1st attempt

package controllers;

import play.libs.*;

import java.io.*;
import java.util.*;

import org.apache.commons.mail.*;

...

        SimpleEmail email = new SimpleEmail();
        email.setFrom(User.getByUsername(Session.get("username")).email);
        email.addTo(app.configuration().getString("ownerEmail"));
        email.addTo(part.email);
        email.setSubject("Part Added: " + part.vendor + " - " + part.label);
        email.setMsg("A Part has been added to the Asset Manager:\n\n"
            + part.toString());
        Mail.send(email);

...

2nd attempt:

package controllers;

import play.libs.*;

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

import com.typesafe.plugin._;

...

            MailerAPI mail = play.Play.application().plugin(MailerPlugin.class).email();
            mail.setSubject("test");
            mail.addRecipient("");
            mail.addFrom("");
            mail.sendHtml("A Part has been added to the Asset Manager:\n\n" + part.toString());

...

3rd attempt:

import java.io.*;
import java.util.*;
import java.util.*;
import javax.mail.internet.*;
import javax.activation.*;

...

            String to = "...";
            String from = "...";
            String host = "localhost";
            Properties properties = System.getProperties();
            properties.setProperty("mail.smtp.host", host);

            // Get the default Session object.
            Session session = Session.getDefaultInstance(properties);

            try{
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(from));
                message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));
                message.setSubject("This is the Subject Line!");
                message.setText("This is actual message");

                // Send message
                Transport.send(message);
                System.out.println("Sent message successfully....");
            }catch (MessagingException me) {
                me.printStackTrace();
            }
...

My question is how do I set up my Play Framework so that it doesn't tell me this:

**Compilation error**

error: cannot find symbol
In ..\app\controllers\Parts.java at line 116.

113            // mail.addFrom("");
114            // mail.sendHtml("A Part has been added to the Asset Manager:\n\n" + part.toString());
115
116            SimpleEmail email = new SimpleEmail();
117            email.setHostName("smtp.googlemail.com");
118            email.setSmtpPort(465);
119            email.setFrom("","");
120            // email.setFrom(User.getByUsername(Session.get("username")).email);
121            email.addTo("");

**note: I emptied out some strings that contained personal information.

Whenever I try to refresh my page. I need to know where I put my package specific information, how the build.scala should look, whether the command order (../play clean; ../play dependencies; ../play run) is correct, whether I should be using managed or unmanaged and ultimately whether or not this is even possible!?!?

Thanks all.

3

There are 3 answers

0
mk7upurz87 On BEST ANSWER

Thank you for the response. I just solved it this morning! The error was caused by my packages not being added to my path. I highly recommend using a power IDE for classpath modifications. I entered it into Eclipse and instantly I had my object reference's corrected. For the mailer itself here's my results:

...

import javax.mail.*;
import javax.mail.internet.*;

...

    try {
        String host = "smtp.gmail.com";
        String username = "[email protected]";
        String password = "password";
        InternetAddress[] addresses = {new InternetAddress("[email protected]"),
            new InternetAddress(bid.email),
            new InternetAddress("[email protected]")};
        Properties props = new Properties();

        // set any needed mail.smtps.* properties here
        Session session = Session.getInstance(props);
        MimeMessage message = new MimeMessage(session);
        message.setSubject("my subject placed here");
        message.setContent("my message placed here:\n\n"
                + part.toString(), "text/plain");
        message.setRecipients(Message.RecipientType.TO, addresses);

        // set the message content here
        Transport t = session.getTransport("smtps");
        try {
            t.connect(host, username, password);
            t.sendMessage(message, message.getAllRecipients());
        } finally {
            t.close();
        }          
    } catch (MessagingException me) {
        me.printStackTrace();
    }
1
Rico On

Looks like you have multiple questions:

  1. How to get play to work with email.
    • For this I suggest look at the play authenticate code and see how they make it work. I'm currently using the module to send email. play-authenticate
  2. You may have an issue with getting email logs. Did you try looking at the system logs? /var/log/syslog (for example on Ubuntu/Debian). There's also log configuration options for play here

  3. (Looks like you solved it?) You have a:

    Compilation error

    error: cannot find symbol In ..\app\controllers\Parts.java at line 116.

    • There's nothing really special about setting up paths for play as long as you have the play executable in the path.
0
abhig On

Try this :

https://github.com/typesafehub/play-plugins/tree/master/mailer

It worked like a charm for me

Let me know if you have any questions