java - playframework 未检测到 mailerclient

标签 java email playframework playframework-2.0

注意:我是框架新手,如有任何菜鸟错误,请原谅我。

背景信息:

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

this 支持 MailerClient github 页面。

我使用 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端口等

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

我的位于 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

我可以确认 MailerClient 确实可以正常工作(被检测、实例化、发送电子邮件等),就像它在推送到 github 之前所做的那样,并且此后没有进行任何更改。

澄清一下,这是未检测到插件/库的问题。

检查它是否确实存在:

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 工具时,它确实解析了邮件程序插件

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

最佳答案

几天前我也遇到了同样的问题。我通过切换到以前版本的 play mailer(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

关于java - playframework 未检测到 mailerclient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46521236/

相关文章:

java - 如何解析 JavaMail api 上的签名?

java - Play 2.4.6,在测试中为字节码增强配置build设置

postgresql - 获取方法 org.postgresql.jdbc4.Jdbc4Connection.isValid(int) 尚未实现

java - 如何在 Spring 运行时设置 bean 限定符名称

java - 设置 JSlider 限制

php - 如何将 MySQL 报告发送到电子邮件

playframework - 需要在游戏框架示例中继续使用

java - HSSF工作簿错误

Java正则表达式匹配模式

php - 将文本转换为图像 - PHP/GD - 保存图像