Grails 3邮件插件无法正常工作 [英] Grails 3 mail plugin not working
问题描述
build.gradle
compileorg.grails.plugins:mail:1.0.7
testCompileorg.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
}
生产{
grails.logging.jul.usebridge = false
}
}
testController:
import groovy.xml.M arkupBuilder
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.security.access.annotation.Secured
@Secured([ hasRole('PERM_LOGIN')])$ b $ class TestLogController {
def Logger logger = LoggerFactory.getLogger(this.getClass())
def mailService
def index(){
logger.info(Hello);
mailService.sendMail {
到user@daomain.com
主题Hello Fred
文字你好吗?
$ $ $
$ b 发生以下错误: / p>
引起NullPointerException:无法调用null对象上的sendMail()方法
- >> 18 |在TestLogController.groovy中的索引
因此,mailService没有被正确注入到cotroller类或集成测试中:
导入grails.test.mixin.integration.Integration
导入grails.util.Holders
导入组织。 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);
$ b def cleanup(){
}
voidtest mailService is not null(){
期望:
mailService!= null
}
voidtest send mail(){
expect:mail send
mailService.sendMail {
到user@domain.com
主题Hello Fred
文字你好吗?
}
}
}
什么是问题?
更新:这是错误的邮件插件版本,这个工作正常:
compileorg.grails.plugins:mail:2.0.0.RC2
解决 compile'org 这是当前版本.grails.plugins:mail:2.0.0.RC6'
这里是主插件的链接(我不知道为什么谷歌只显示旧版本):
https: //grails.org/plugins.html#plugin/mail
我希望这可以帮助别人
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"
解决方案 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
这篇关于Grails 3邮件插件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文