播放2.5.12 Mailer(播放邮件5.0.0)插件,当使用.send()方法时导致NullPointerException - mailerClient永远不会使用@Inject实例化

时间:2018-05-01 19:52:40

标签: java playframework

我使用Play Mailer 5.0.0使用Play 2.5.12创建了一个应用程序。调用.send()方法时,我收到NullPointerExeception错误。

我有一个Mail.java类和一个MailController.java控制器。 MailController拨打Mail.java。我根据这个解决方案设置了我的代码:

Play framework Mailer throwing NullPointerException at maile.send() method

这是我的MailController.java

package controllers;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import controllers.helpers.AccessMiddleware;
import models.Comment;
import models.Intake;
import models.User;
import models.utils.Mail;
import play.Configuration;
import play.i18n.Messages;
import play.libs.mailer.MailerClient;
import play.mvc.Controller;

public class MailController extends Controller{

    @Inject
    MailerClient mailerClient;

    public void notifyTeam(Intake intake, Comment comment, String emailType) throws MalformedURLException {
        // Set up globals...
        String subject = "";
        String message = "";
        String commentAuthor = "";
        String commentDate = "";
        String commentText = "";
        String testPrefix = Configuration.root().getString("application.emailsubject.testdisregard");
        String emailMode = Configuration.root().getString("application.emailmode");
        String emailState = Configuration.root().getString("application.emailstate");
        String userkey = AccessMiddleware.getSessionUserKey();
        User user = User.findByUserKey(userkey);
        String userEmail = user.getEmail();
        String projectID = intake.getProjectid();
        String projectName = intake.getProjectname();
        String intakeKey = intake.getRecordkey();

        // Build the Send To email addresses...
        List<String> emailArray = new ArrayList<String>();

        // Build the URL to send...
        String urlString = "https://" + Configuration.root().getString("server.hostname") + "/openintake/"
                + intakeKey;
        // Validate the URL, will throw an exception if bad...
        URL url = new URL(urlString);

        switch (emailType) {
        case "New":
            // Build the subject...
            subject = Messages.get("mail.newintake.subject", projectID);
            message = Messages.get("mail.newintake.message", url.toString());
            emailArray.add(intake.getBamanageremail());
            emailArray.add(intake.getSemanageremail());
            emailArray.add(intake.getBaassignedemail());
            emailArray.add(intake.getBaassignedemail1());
            emailArray.add(intake.getBaassignedemail2());
            emailArray.add(intake.getSeassignedemail());
            emailArray.add(intake.getSeassignedemail1());
            emailArray.add(intake.getSeassignedemail2());
            break;

        case "Comment":
        case "Edited Comment":
            // Build the subject...
            subject = Messages.get("mail.newcomment.subject", projectID, projectName);
            if (emailType.equals("Edited Comment")) {
                subject = Messages.get("mail.editcomment.subject", projectID, projectName);
            }
            // Let's add the comment to the message...
            commentAuthor = comment.getAuthor();
            commentDate = comment.getDatecreated().toString();
            commentText = comment.getComment();
            message = Messages.get("mail.newcomment.message", projectID, projectName, commentAuthor, commentDate,
                    commentText, url.toString());
            if (emailType.equals("Edited Comment")) {
                message = Messages.get("mail.editcomment.message", projectID, projectName, commentAuthor, commentDate,
                        commentText, url.toString());
            }
            emailArray.add(intake.getBamanageremail());
            emailArray.add(intake.getSemanageremail());
            emailArray.add(intake.getBaassignedemail());
            emailArray.add(intake.getBaassignedemail1());
            emailArray.add(intake.getBaassignedemail2());
            emailArray.add(intake.getSeassignedemail());
            emailArray.add(intake.getSeassignedemail1());
            emailArray.add(intake.getSeassignedemail2());
            break;

        case "Assigned":
            // Build the subject...
            subject = Messages.get("mail.assignedintake.subject", projectID, projectName);
            message = Messages.get("mail.assignedintake.message", url.toString());
            emailArray.add(intake.getRequesterEmail());
            break;

        default:
            return;
        }

        // Let's remove the current user from the list so not to send the
        // email to them...
        emailArray = Application.removeValueFromArray(emailArray, userEmail);

        // Let's make sure we have some addresses to send to...
        if (Application.isArrayEmpty(emailArray) != true) {
            // Are we in test email mode?
            if (emailMode.equals("test")) {
                subject = testPrefix + " " + subject;
                message = testPrefix + "<br><br>" + message;
            }
            Mail mailer = new Mail(mailerClient);
            Mail.Envelop envelop = new Mail.Envelop(subject, message, emailArray);
            // Are we sending email?
            if (emailState.equals("on")) {
                mailer.sendMail(envelop);
            }
        }
    }

}

当我调试时,mailerClient永远不会被实例化 - 它总是为空。

Mail.java来自PlayStartApphttps://github.com/yesnault/PlayStartApp)模板:

package models.utils;

import play.Configuration;
import play.Logger;
import play.libs.mailer.Email;
import play.libs.mailer.MailerClient;

import javax.inject.Inject;

import play.libs.Akka;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Send a mail with PlayStartApp.
 * <p/>
 * User: yesnault Date: 24/01/12
 */
public class Mail {

    MailerClient mailerClient;

    public Mail(MailerClient mailerClient) {
        this.mailerClient = mailerClient;
    }

    /**
     * 1 second delay on sending emails
     */
    private static final int DELAY = 1;

    /**
     * Envelop to prepare.
     */
    public static class Envelop {
        public String subject;
        public String message;
        public List<String> toEmails;

        /**
         * Constructor of Envelop.
         *
         * @param subject
         *            the subject
         * @param message
         *            a message
         * @param toEmails
         *            list of emails adress
         */
        public Envelop(String subject, String message, List<String> toEmails) {
            this.subject = subject;
            this.message = message;
            this.toEmails = toEmails;
        }

        public Envelop(String subject, String message, String email) {
            this.message = message;
            this.subject = subject;
            this.toEmails = new ArrayList<String>();
            this.toEmails.add(email);
        }
    }

    /**
     * Send a email, using Akka to offload it to an actor.
     *
     * @param envelop
     *            envelop to send
     */
    @SuppressWarnings("deprecation")
    public void sendMail(Mail.Envelop envelop) {
        EnvelopJob envelopJob = new EnvelopJob(envelop, mailerClient);
        final FiniteDuration delay = Duration.create(DELAY, TimeUnit.SECONDS);
        Akka.system().scheduler().scheduleOnce(delay, envelopJob, Akka.system().dispatcher());
    }

    static class EnvelopJob implements Runnable {
        MailerClient mailerClient;
        Mail.Envelop envelop;

        @Inject
        public EnvelopJob(Mail.Envelop envelop, MailerClient mailerClient) {
            this.envelop = envelop;
            this.mailerClient = mailerClient;
        }

        public void run() {
            Email email = new Email();

            final Configuration root = Configuration.root();
            final String mailFrom = root.getString("play.mailer.from");
            final String mailSign = root.getString("play.mailer.signature");

            // email.setFrom(mailFrom);
            email.setFrom("BASE Central <basecentral@state.co.us>");
            email.setSubject(envelop.subject);
            email.setBodyText(envelop.message + "\n\n " + mailSign);
            email.setBodyHtml(envelop.message + "<br><br>--<br>" + mailSign);
            for (String toEmail : envelop.toEmails) {
                email.addTo(toEmail);
                Logger.debug("Mail.sendMail: Mail will be sent to " + toEmail);
            }

            mailerClient.send(email);
            Logger.debug("Mail sent - SMTP:" + root.getString("play.mailer.host") + ":"
                    + root.getString("play.mailer.port") + " SSL:" + root.getString("play.mailer.ssl") + " User:"
                    + root.getString("play.mailer.user") + " Password:" + root.getString("play.mailer.password")
                    + " From:" + root.getString(mailFrom));
        }
    }
}

根据解决方案,我认为此代码应该有效,但我在mailerClient.send(email);的第108行的Mail.java上收到此错误:

[error] a.d.TaskInvocation - null
java.lang.NullPointerException: null
        at models.utils.Mail$EnvelopJob.run(Mail.java:108)
        at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:39)
        at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:415)
        at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
        at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
        at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

我很感激帮助。

1 个答案:

答案 0 :(得分:0)

mailerClient从未正确注入,因此始终为null。要解决此问题,请在MailController.java中更改:

@Inject
MailerClient mailerClient;

为:

private final MailerClient mailerClient;

@Inject
public MailController(MailerClient mailerClient) {
    this.mailerClient = mailerClient;
}

mailerClient现已注入并正常工作。