playframework未检测到mailerclient

时间:2017-10-02 07:35:20

标签: java email playframework playframework-2.0

注意:我是新玩的框架,请原谅我任何新手的错误。

背景信息:

在我的webapp中,我使用了电子邮件功能,这是通过使用作为sbt库提供的MailerClient来完成的。

this github页面支持MailerClient。

我使用github作为我的git存储库,因为这是一个团队项目。

我最初创建了项目并推送到存储库,这里是问题的开始

电子邮件支持说明(JAVA)(作为我采取的步骤的参考)

首先,要启用Java的电子邮件支持,需要采取一些步骤。

1。添加SBT MailerClient库

添加行

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

projectRoot/build.sbt文件

2。配置MailerClient

然后在MailerClient文件

中配置projectRoot/conf/application.conf端口等

github页面上给出的示例:

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位于application.conf文件的底部,因为它不构成任何其他树前缀的一部分。

第3。继续Java Usage标记

4。在课程内(您选择的),添加代码:

@Inject 
MailerClient mailerClient;

在此类中,MailerClient对象被实例化,可用于发送电子邮件。

使用Github页面中的给定示例,创建email对象:

String cid = "1234";
    Email email = new Email()
      .setSubject("Simple email")
      .setFrom("Mister FROM <from@email.com>")
      .addTo("Miss TO <to@email.com>")
      // 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>");

然后使用mailerClient发送电子邮件:

    mailerClient.send(email);

问题:

签出我的分支后(即它从github拉出webapp项目),PlayFramework没有检测到MailerClient

我可以在推送到github之前确认MailerClient 工作(被检测,实例化,发送电子邮件等),并且之后没有进行任何更改。

为了澄清,没有检测到插件/库的问题。

检查它是否确实存在:

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

启动sbt工具时,它确实解析了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 ...
//...

1 个答案:

答案 0 :(得分:1)

几天前我确实遇到了同样的问题。我通过切换到5.0的播放邮件的先前版本解决了我的问题。

以下是工作示例

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="youremail@gmail.com"
  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("Donald.Trump@gmail.com");
        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);
    }

}

<强>路由

GET     /mail                      controllers.YourController.Email