playerclient 未被 playframework 检测到

mailerclient not detected by playframework

注:本人初玩框架,菜鸟有误还望见谅

背景信息:

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

this git 中心页面支持 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

然后在projectRoot/conf/application.conf文件中配置MailerClient端口等

git中心页面上给出的示例:

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)
}

我的位于 application.conf 文件的底部,因为它不构成任何其他树前缀的一部分。

3。进入 Java Usage 标签

4.在 class(您选择的)中,添加代码:

@Inject 
MailerClient mailerClient;

在此 class 中,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.

我可以确认 MailerClient 在推送到 github 之前确实工作(被检测、实例化、发送电子邮件等),并且没有此后发生了变化。

澄清一下,问题是 plugin/library 没有被检测到。

正在检查它是否确实存在:

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 ...
//...

几天前我确实遇到了同样的问题。我通过切换到以前版本的播放邮件程序 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