CybeX
CybeX

Reputation: 2386

mailerclient not detected by playframework

Note: I am new to play framework, please forgive me for any rookie mistakes.

Background Info:

In my webapp, I make use of email functionality, and this is done by using the MailerClient provided as a sbt library.

The MailerClient is supported on this github page.

I am using github as my git repository, as this is a team project.

I initally created the project and pushed to the repository, here is the start of the problem.


Instructions for Email Support (JAVA) (As a reference of my steps taken)

Firstly, for enabling email support for Java, a few steps need to be taken.

1. Add SBT MailerClient library

Add the lines

libraryDependencies += "com.typesafe.play" %% "play-mailer" % "6.0.1"
libraryDependencies += "com.typesafe.play" %% "play-mailer-guice" % "6.0.1"

to the projectRoot/build.sbt file

2. Configure MailerClient

Then configure the MailerClient ports, etc in the projectRoot/conf/application.conf file

example given on the github page:

play.mailer {
  host = "example.com" // (mandatory)
  port = 25 // (defaults to 25)
  ssl = no // (defaults to no)
  tls = no // (defaults to no)
  tlsRequired = no // (defaults to no)
  user = null // (optional)
  password = null // (optional)
  debug = no // (defaults to no, to take effect you also need to set the log level to "DEBUG" for the application logger)
  timeout = null // (defaults to 60s in milliseconds)
  connectiontimeout = null // (defaults to 60s in milliseconds)
  mock = no // (defaults to no, will only log all the email properties instead of sending an email)
}

mine is at the bottom of the application.conf file as it doesn't form part of any other tree prefix.

3. Proceed to the Java Usage tag

4. Inside a class (of your choice), add the code:

@Inject 
MailerClient mailerClient;

Within this class, the MailerClient object is instantiated and can be used to send emails.

With the given example from the Github page, create the email object:

String cid = "1234";
    Email email = new Email()
      .setSubject("Simple email")
      .setFrom("Mister FROM <[email protected]>")
      .addTo("Miss TO <[email protected]>")
      // adds attachment
      .addAttachment("attachment.pdf", new File("/some/path/attachment.pdf"))
      // adds inline attachment from byte array
      .addAttachment("data.txt", "data".getBytes(), "text/plain", "Simple data", EmailAttachment.INLINE)
      // adds cid attachment
      .addAttachment("image.jpg", new File("/some/path/image.jpg"), cid)
      // sends text, HTML or both...
      .setBodyText("A text message")
      .setBodyHtml("<html><body><p>An <b>html</b> message with cid <img src=\"cid:" + cid + "\"></p></body></html>");

Then send the email using the mailerClient:

    mailerClient.send(email);

Problem:

After checking out my branch (i.e. it pulls the webapp project from github), PlayFramework didn't detect the MailerClient.

I can confirm that the MailerClient does work (was detected, instantiated, sends email, etc) as it did so before pushing to github, and no change has been made since.

To clarify, it is problem with the plugin/library not being detected.

Checking to see if it actually exists:

09:15:08 ✔ cybex@arch-laptop ~/.ivy2 $ find | grep mailer
./cache/com.typesafe.play/play-mailer_2.12
./cache/com.typesafe.play/play-mailer_2.12/ivy-6.0.1.xml.original
./cache/com.typesafe.play/play-mailer_2.12/ivydata-6.0.1.properties
./cache/com.typesafe.play/play-mailer_2.12/ivy-6.0.1.xml
./cache/com.typesafe.play/play-mailer_2.12/jars
./cache/com.typesafe.play/play-mailer_2.12/jars/play-mailer_2.12-6.0.1.jar
./cache/com.typesafe.play/play-mailer_2.12/srcs
./cache/com.typesafe.play/play-mailer_2.12/srcs/play-mailer_2.12-6.0.1-sources.jar
./cache/com.typesafe.play/play-mailer-guice_2.12
./cache/com.typesafe.play/play-mailer-guice_2.12/ivy-6.0.1.xml.original
./cache/com.typesafe.play/play-mailer-guice_2.12/ivydata-6.0.1.properties
./cache/com.typesafe.play/play-mailer-guice_2.12/ivy-6.0.1.xml
./cache/com.typesafe.play/play-mailer-guice_2.12/jars
./cache/com.typesafe.play/play-mailer-guice_2.12/jars/play-mailer-guice_2.12-6.0.1.jar
./cache/com.typesafe.play/play-mailer-guice_2.12/srcs
./cache/com.typesafe.play/play-mailer-guice_2.12/srcs/play-mailer-guice_2.12-6.0.1-sources.jar

When initiating the sbt tool, it does resolve the mailer plugin

//...
[info] Resolving com.typesafe.play#play-mailer_2.12;6.0.1 ...
[info] Resolving org.apache.commons#commons-email;1.5 ...
[info] Resolving com.sun.mail#javax.mail;1.5.6 ...
[info] Resolving javax.activation#activation;1.1 ...
[info] Resolving com.typesafe.play#play-mailer-guice_2.12;6.0.1 ...
//...

Upvotes: 0

Views: 223

Answers (1)

Somnath Sarode
Somnath Sarode

Reputation: 477

Few days back i did experienced the same problem. I solved my problem by switching to previous version of play mailer which is 5.0.

Below is the working example

build.sbt

libraryDependencies +="com.typesafe.play" %% "play-mailer" % "5.0.0-M1"

application.conf

play.mailer {
  host="smtp.gmail.com"
  port=465
  ssl=yes
  tls=no
  user="[email protected]"
  password="password"
  debug=no
  timeout=1000
  connectiontimeout=1000
  mock=no
  // (defaults to no, will only log all the email properties instead of sending an email)
}

YourController.java

public class YourController extends Controller {

private final MailerClient mailer ;

    @Inject
    public EventUser(MailerClient mailer) {
        this.mailer = mailer;
    }

public Result Email()
    {
        sendMail("[email protected]");
        return ok("done");
    }


private void sendMail(String userEmail)
    {

        Email email = new Email()
                .setSubject("BLah Blah Messsage ")
                .setFrom("")
                .addTo(" <"+userEmail+">")
                // adds attachment
                .setBodyText("Please register to Event at ");
        //.setBodyHtml("<html><body><p>An <b>html</b> message with cid <img src=\"cid:" + cid + "\"></p></body></html>");
        if(mailer!=null)
            mailer.send(email);
    }

}

routes

GET     /mail                      controllers.YourController.Email

Upvotes: 1

Related Questions