【问题标题】:Grails 3 mail plugin not workingGrails 3邮件插件不起作用
【发布时间】:2015-12-12 15:39:31
【问题描述】:

使用 Grails 3 无法获取 mailService 对象的实例,DI 不起作用:

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
    }
}

测试控制器:

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?"
        }       
    }
}

出现以下错误:

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

所以 mailService 没有正确注入到 cotroller 类或集成测试中:

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?"
       }
    }
}

有什么问题??

更新:邮件插件版本错误,这个可以正常工作:

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

【问题讨论】:

标签: grails grails-plugin grails-3.0


【解决方案1】:

这是要安装的当前版本(在撰写本文时):

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

这是主插件的链接(我不知道为什么 google 只显示旧版本):
https://grails.org/plugins.html#plugin/mail 我希望这可以帮助某人

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多