有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

playframework未检测到java mailerclient

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

背景信息:

在我的webapp中,我使用电子邮件功能,这是通过使用作为sbt库提供的MailerClient实现的

MailerClient在thisgithub页面上受支持

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

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


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

首先,为了支持Java的电子邮件,需要采取一些步骤

1。添加SBT邮件客户端库

添加行

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工具时,它会解析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) 个答案

  1. # 1 楼答案

    几天前我也遇到过同样的问题。我通过切换到play mailer的早期版本(5.0)解决了我的问题

    下面是工作示例

    构建。sbt

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

    应用程序。形态

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

    您的控制器。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