Zan
Zan

Reputation: 290

Grails injected Mail Service bean is null inside a controller

I am trying to use the following Grails Mail plugin: https://grails.org/plugin/mail

I've added the depedency in BuildConfig.groovy:

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

The I've configured it to use a specific email by adding the following code in 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 = "[email protected]"
        }
}

I have a controller where I declare the mailService so it should be injectd as a 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 '[email protected]'
                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
    }
}

I am trying to use the sendMail method it in the registerAccount method of the controller. However I get an error, which basically says that the mailService object is null. Here is the error message:

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)

And there is another strange thing that I should mention. I'm using IntelliJ Ultimate Edition, and here is a curios thing: enter image description here

If you notice inside the highlighted area with red, the IDE is showing that it can't recognize the arguments inside the closure that is passed to sendEmail. I've never used this plugin before, so I just followed the steps in the docs, but apparently something is wrong. Thank you for your help.

Upvotes: 1

Views: 749

Answers (1)

defectus
defectus

Reputation: 1987

In your code you have:

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

which I suppose should be either:

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

or

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

otherwise you call a method html with params "<a href=", $, { link } (closure that returns "link") and ">Activate your account on Kunega</a>".

Upvotes: 4

Related Questions