我正在使用Play Mailer模块。我在 mailerClient.send(email) 处收到NullPointerException 。
发送邮件代码
import javax.inject.Inject; import play.libs.mailer.Email; import play.libs.mailer.MailerClient; public class MailerService { @Inject MailerClient mailerClient; public void sendEmail() { Email email = new Email(); email.setSubject("Activation Link"); email.setFrom("from@gmail.com"); email.addTo("to@gmail.com"); email.setBodyText("hello"); mailerClient.send(email); } }
application.conf
# Logger provided to your application: logger.application=DEBUG play{ modules { enabled += "play.api.libs.mailer.MailerModule" #enabled += "play.api.libs.mailer.SMTPConfigurationModule" } mailer { host=smtp.gmail.com port=465 ssl=yes tls=no user="my gmail username" password="gmail password" debug=yes } }
我也有加入 “com.typesafe.play” %%“播放邮件”%“3.0.1” 在build.sbt
播放版本:-2.4
Java版本:1.8
我正在添加堆栈跟踪
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[NullPointerException: null]] at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:265) ~[play_2.11-2.4.6.jar:2.4.6] at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:191) ~[play_2.11-2.4.6.jar:2.4.6] at play.api.GlobalSettings$class.onError(GlobalSettings.scala:179) [play_2.11-2.4.6.jar:2.4.6] at play.api.DefaultGlobal$.onError(GlobalSettings.scala:212) [play_2.11-2.4.6.jar:2.4.6] at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:94) [play_2.11-2.4.6.jar:2.4.6] at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$9$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:151) [play-netty-server_2.11-2.4.6.jar:2.4.6] at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$9$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:148) [play-netty-server_2.11-2.4.6.jar:2.4.6] at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [scala-library-2.11.6.jar:na] at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:215) [scala-library-2.11.6.jar:na] at scala.util.Try$.apply(Try.scala:191) [scala-library-2.11.6.jar:na] at scala.util.Failure.recover(Try.scala:215) [scala-library-2.11.6.jar:na] at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324) [scala-library-2.11.6.jar:na] at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324) [scala-library-2.11.6.jar:na] at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) [scala-library-2.11.6.jar:na] at play.api.libs.iteratee.Execution$trampoline$.executeScheduled(Execution.scala:109) [play-iteratees_2.11-2.4.6.jar:2.4.6] at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:71) [play-iteratees_2.11-2.4.6.jar:2.4.6] at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40) [scala-library-2.11.6.jar:na] at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248) [scala-library-2.11.6.jar:na] at scala.concurrent.Promise$class.complete(Promise.scala:55) [scala-library-2.11.6.jar:na] at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153) [scala-library-2.11.6.jar:na] at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23) [scala-library-2.11.6.jar:na] at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) [akka-actor_2.11-2.3.13.jar:na] at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [akka-actor_2.11-2.3.13.jar:na] at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.6.jar:na] at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.6.jar:na] at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.6.jar:na] at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.6.jar:na] **Caused by: java.lang.NullPointerException: null** at mailer.MailerService.sendEmail(MailerService.java:17) ~[classes/:na] at controllers.Application.sendMailTest(Application.java:14) ~[classes/:na] at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$10$$anonfun$apply$10.apply(Routes.scala:324) ~[classes/:na] at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$10$$anonfun$apply$10.apply(Routes.scala:324) ~[classes/:na] at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:136) ~[play_2.11-2.4.6.jar:2.4.6] at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3$$anon$1.invocation(HandlerInvoker.scala:127) ~[play_2.11-2.4.6.jar:2.4.6] at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:70) ~[play_2.11-2.4.6.jar:2.4.6]
看来您未正确初始化MailerClient。
MailerClient
该示例显示您需要将其注入到控制器中,该控制器本身由Play框架初始化。
您将其注入自己的MailerService班级。我想,你这样做new MailerService()的controllers.Application.sendMailTest。结果,MailerClient不会注入到MailerService类中(如果您初始化MailerService自己,谁会这样做?)
MailerService
new MailerService()
controllers.Application.sendMailTest
解决方案:您需要MailerClient像示例中一样注入控制器,然后将其传递给您的MailerService类。
import play.libs.mailer.Email; import play.libs.mailer.MailerClient; public class MailerService { MailerClient mailerClient; public MailerService(MailerClient mailerClient){ this.mailerClient = mailerClient; } public void sendEmail() { Email email = new Email(); email.setSubject("Activation Link"); email.setFrom("from@gmail.com"); email.addTo("to@gmail.com"); email.setBodyText("hello"); mailerClient.send(email); } }