Grails 3 mail plugin not working

回眸只為那壹抹淺笑 提交于 2020-01-04 05:05:12

问题


Using Grails 3 it's impossible to get an instance of mailService object, DI is not working:

build.gradle

compile "org.grails.plugins:mail:1.0.7"
testCompile "org.grails.plugins:mail:1.0.7"

application.groovy

environments {
    development {
     //grails.logging.jul.usebridge = true
     grails.plugin.springsecurity.debug.useFilter = true
     grails {
        mail {
            host = "main.mydomain.com"
            port = 25
            username = "login"
            password = "password"
            props = ["mail.smtp.auth":"true"]

        }
    }
    grails.mail.default.from="noreply@mydomain.com"

    }
    production {
        grails.logging.jul.usebridge = false
    }
}

testController:

import groovy.xml.MarkupBuilder
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.security.access.annotation.Secured

@Secured(["hasRole('PERM_LOGIN')"])
class TestLogController {

    def Logger logger = LoggerFactory.getLogger(this.getClass())
    def mailService

    def index() {

        logger.info("Hello");

        mailService.sendMail {
           to "user@daomain.com"
           subject "Hello Fred"
           text "How are you?"
        }       
    }
}

The following error occurs:

Caused by NullPointerException: Cannot invoke method sendMail() on null    object
->>   18 | index                    in TestLogController.groovy

So mailService has not been injected properly to cotroller class or to integration test:

import grails.test.mixin.integration.Integration
import grails.util.Holders
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationContext
import spock.lang.Shared
import spock.lang.Specification

@Integration
class SendMailSpec extends Specification {

    @Shared Logger logger
    @Shared def mailService

    def setup() {
        logger = LoggerFactory.getLogger(this.getClass())
        ApplicationContext ctx = Holders.grailsApplication.mainContext
        mailService = ctx.getBean("mailService");

    }

    def cleanup() {
    }

    void "test mailService is not null"() {
       expect:
       mailService !=null
    }

    void "test send mail"() {
      expect:"mail send"
       mailService.sendMail {
          to "user@domain.com"
          subject "Hello Fred"
          text "How are you?"
       }
    }
}

What is a problem ??

UPDATE: It was wrong mail plugin version, this one works fine:

compile "org.grails.plugins:mail:2.0.0.RC2"

回答1:


here's the current version (at the moment of writing this) to install :

compile 'org.grails.plugins:mail:2.0.0.RC6'

Here's the link to the main plugin (I don't know why google shows only the old versions) :
https://grails.org/plugins.html#plugin/mail I hope this helps someone



来源:https://stackoverflow.com/questions/32602032/grails-3-mail-plugin-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!