注入的Grails邮件服务bean在控制器

时间:2016-02-01 09:52:40

标签: email grails intellij-idea

我正在尝试使用以下Grails Mail插件:https://grails.org/plugin/mail

我在 BuildConfig.groovy 中添加了依赖:

plugins {
 //mail plugin
        compile "org.grails.plugins:mail:1.0.7"
}

我已将其配置为使用特定电子邮件,方法是在 Config.groovy 中添加以下代码:

grails {
    mail {
            host = "smtp.gmail.com"
            port = 465
            username = "-my email-"
            password = "-my password-"
            props = ["mail.smtp.auth":"true",
                     "mail.smtp.socketFactory.port":"465",
                     "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
                     "mail.smtp.socketFactory.fallback":"false"]
            from = "no-reply@kunega.com"
        }
}

我有一个控制器,我声明 mailService 所以它应该作为bean注入:

@Secured("permitAll")
class RegisterController {

    def mailService

    def springSecurityService

    @Transactional
    def registerAccount(UserCommand userCommand) {
        def model
        if (springSecurityService.isLoggedIn()) {
            model = [success: false, message: 'Log out to register a new account.']
            response.status = 400
        } else if (userCommand.validate()) {
            User u = userCommand.createUser()
            u.save(flush: true);
            Role role = Role.findByAuthority("ROLE_USER")
            UserRole.create u, role, true

            def link = createLink(controller: 'register', action: 'activateAccount', params: [code: u.confirmCode])

            mailService.sendMail {
                async true
                to 'kunega@mailinator.com'
                html "<a href=" $ { link } ">Activate your account on Kunega</a>"
            }

            model = [success: true, message: 'An activation link has been sent to your email.']
            response.status = 201
        } else {
            model = [success: false, errors: userCommand.getErrors()]
            response.status = 400
        }
        render model as JSON
    }
}

我正在尝试在控制器的 registerAccount 方法中使用 sendMail 方法。但是我收到一个错误,它基本上表示mailService对象为null。以下是错误消息:

errors.GrailsExceptionResolver NullPointerException occurred when processing request: [POST] /Kunega/register/createAccount
Cannot invoke method $() on null object. Stacktrace follows:
java.lang.NullPointerException: Cannot invoke method $() on null object
    at com.kunega.RegisterController$_$tt__registerAccount_closure2.doCall(RegisterController.groovy:32)
    at grails.plugin.mail.MailService.sendMail(MailService.groovy:53)
    at grails.plugin.mail.MailService.sendMail(MailService.groovy:59)
    at com.kunega.RegisterController.$tt__registerAccount(RegisterController.groovy:29)
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53)
    at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:49)
    at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:82)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

还有一件我应该提到的奇怪的事情。我正在使用IntelliJ Ultimate Edition,这是一个古玩的事情: enter image description here

如果您在突出显示的区域内注意到红色,则表明IDE无法识别传递给 sendEmail 的闭包内的参数。 我之前从未使用过这个插件,所以我只是按照文档中的步骤进行操作,但显然有些错误。谢谢您的帮助。

1 个答案:

答案 0 :(得分:4)

在您的代码中,您有:

html "<a href=" $ { link } ">Activate your account on Kunega</a>"

我认为应该是:

html "<a href=\""+ link + "\">Activate your account on Kunega</a>"

html "<a href=\"${ link }\">Activate your account on Kunega</a>"

否则,您使用参数html"<a href="$(返回“链接”的闭包)和{ link }调用方法">Activate your account on Kunega</a>"