Email Grails注入的邮件服务bean在控制器中为空
我正在尝试使用以下Grails邮件插件: 我在BuildConfig.groovy中添加了依赖性:Email Grails注入的邮件服务bean在控制器中为空,email,grails,intellij-idea,Email,Grails,Intellij Idea,我正在尝试使用以下Grails邮件插件: 我在BuildConfig.groovy中添加了依赖性: plugins { //mail plugin compile "org.grails.plugins:mail:1.0.7" } grails { mail { host = "smtp.gmail.com" port = 465 username = "-my email-"
plugins {
//mail plugin
compile "org.grails.plugins:mail:1.0.7"
}
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"
}
}
通过在Config.groovy中添加以下代码,我已将其配置为使用特定的电子邮件:
plugins {
//mail plugin
compile "org.grails.plugins:mail:1.0.7"
}
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"
}
}
我有一个控制器,我在其中声明邮件服务,因此它应该作为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
}
}
还有一件奇怪的事我应该提一下。我使用的是IntelliJ Ultimate Edition,这里有一件古玩:
如果您注意到在突出显示的红色区域内,IDE显示它无法识别传递给sendmail的闭包内的参数。
我以前从未使用过这个插件,所以我只是按照文档中的步骤操作,但显然有些地方出了问题。谢谢你的帮助 在代码中,您有:
html "<a href=" $ { link } ">Activate your account on Kunega</a>"
html“”
我想应该是:
html "<a href=\""+ link + "\">Activate your account on Kunega</a>"
html“”
或
html“”
否则,在代码中调用带有参数的方法html
”:
html "<a href=" $ { link } ">Activate your account on Kunega</a>"
html“”
我想应该是:
html "<a href=\""+ link + "\">Activate your account on Kunega</a>"
html“”
或
html“”
否则,使用params调用方法
html
”如果在IDE中显示行号(在本例中为RegisterController的不完整源代码),则调试更容易。如果在执行此任务的IDE中显示行号(在本例中为RegisterController的不完整源代码),则调试更容易。我无法通过使用错误/异常消息来理解它。诀窍是,使用闭包调用mailService.sendMail
方法。该闭包是在特定的上下文中执行的(通过更改闭包的委托
),因此调用堆栈看起来很奇怪。这就完成了任务。我无法通过使用错误/异常消息来理解它。诀窍是,使用闭包调用mailService.sendMail
方法。该闭包是针对特定上下文执行的(通过更改闭包的委托),因此调用堆栈看起来很奇怪。