问题描述
我正在使用play Mailer模块.我在 mailerClient.send(email)处收到NullPointerException.
I am using play Mailer module. I am getting NullPointerException at mailerClient.send(email).
发送邮件代码
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("[email protected]");
email.addTo("[email protected]");
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
}
}
我还在build.sbt中添加了"com.typesafe.play" %%播放邮件程序"%"3.0.1"
Also i have added "com.typesafe.play" %% "play-mailer" % "3.0.1" in build.sbt
播放版本:-2.4
java版本:1.8
java version: 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
.
It looks like you incorrectly initialize MailerClient
.
示例显示您需要将其注入到控制器中,该控制器本身由播放框架.
The example shows that you need to inject it into the controller, that itself initialized by the Play Framework.
您将其注入到自己的MailerService
类中.我想您在controllers.Application.sendMailTest
中执行类似new MailerService()
的操作.结果,MailerClient
不会注入到MailerService
类中(如果您自己初始化MailerService
,谁会这样做?)
You inject it into your own MailerService
class. I suppose that you do something like new MailerService()
in the controllers.Application.sendMailTest
. As a result, MailerClient
does not inject into the MailerService
class (Who will do this if you initialise MailerService
yourself ?)
解决方案:您需要像示例中一样将MailerClient
注入到控制器中,然后将其传递到您的MailerService
类中.
Solution: You need to inject MailerClient
into the controller, like in the example, and then pass it into your MailerService
class.
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("[email protected]");
email.addTo("[email protected]");
email.setBodyText("hello");
mailerClient.send(email);
}
}
这篇关于播放框架Mailer在maile.send()方法上抛出NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!