-
-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inject mailerClient in Global.java #91
Comments
I believe
Source: https://www.playframework.com/documentation/2.4.x/GlobalSettings#Java Instead I think you should create a |
Create service to send email, something like : @Singleton
public final class EmailService extends BaseService {
private final MailerClient mailerClient;
private final Configuration configuration;
@Inject
private final ActorSystem system;
@Inject
public EmailService(final Configuration configuration, MailerClient mailerClient, ActorSystem system) {
this.mailerClient = mailerClient;
this.configuration = configuration;
this.system = system;
}
public void sendMail(String receiver, String body, String subject, String replyTo, String senderName) {
system.scheduler().scheduleOnce(
Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay
() -> {
try {
Logger.info(String.format("Emailing (%s) from (%s) with subject (%s)", receiver, senderName, subject));
Email email = new Email();
email.setSubject(subject);
email.setReplyTo(replyTo);
email.setFrom(senderName + "<" + configuration.getString("play.mailer.from") + ">");
email.addTo(receiver);
email.setBodyHtml(body);
mailerClient.send(email);
Logger.info(String.format("Email sent to (%s) from (%s) with subject (%s)", receiver, senderName, subject));
} catch (Exception ex) {
Logger.error(ex.getMessage());
}
},
system.dispatcher()
);
}
} And replace Global.java file into this: Inside your
This is how I'm working with email service if I use it while application start up. if you want to use it in another service or controllers After you started application, just enough to have it as global variable:
|
Which library is used for IntelliJ imported |
Oh you don't mind this, remove it, I had a structure for my services, so it is my own |
Solution was posted, Global.java is long gone from Play. |
In my Global.java I'm trying to schedule a task which has to sent an email every now and then based on some parameters. I can't seem to access mailerClient though, because I always get a NullPointerException. Can someone explain what I'm doing wrong? My code looks roughly like this:
I can't seem to figure out what's going wrong, any help is appreciated.
The text was updated successfully, but these errors were encountered: