grails - 无法在 grails 应用程序中调用 sendMail 函数

标签 grails

我正在尝试在 grails 中调用发送邮件。我从 Controller 调用

class TournamentController {
    static scaffold = true
    //controller functions


    def emailParticipants(Tournament t) {
        def emailSubject = "Tournament ${t.title} complete"

        for (Prediction p : t.predictions){
            if (p.email != null){
                def emailBody = """\
                    Hello ${p.name}!  Thank you for participating in the ${t.title} tournament.
                    Please find a link below with statistics and match results.
                    """
                sendMail {
                    async true
                    to p.email
                    subject emailSubject
                    body emailBody
                }
            }
        }
    }
}

我得到的错误是带有以下消息的 MissingMethodException:
No signature of method: predictionpal.MailService.sendMail() is applicable for
argument types(predictionpal.TournamentController$_emailParticipants_closure2)
values
[predictionpal.TournamentController$_emailParticipants_closure2@51fc1bbb]
Possible solutions: findAll()

这是我添加到 BuildConfig.groovy 的内容
grails.project.dependency.resolution = {
    //There is a stuff I didn't add here

    plugins {
        // plugins for the build system only
        build ":tomcat:7.0.55"

        // **I ONLY ADDED THIS LINE**
        compile ":mail:1.0.7"

        // plugins for the compile step
        compile ":scaffolding:2.1.2"
        compile ':cache:1.1.8'
        compile ":asset-pipeline:1.9.9"

        // plugins needed at runtime but not for compilation
        runtime ":hibernate:3.6.10.18" // or ":hibernate4:4.3.6.1" 
        runtime ":database-migration:1.4.0"
        runtime ":jquery:1.11.1"
        runtime ":console:1.5.3"

        // Uncomment these to enable additional asset-pipeline capabilities
        //compile ":sass-asset-pipeline:1.9.0"
        //compile ":less-asset-pipeline:1.10.0"
        //compile ":coffee-asset-pipeline:1.8.0"
        //compile ":handlebars-asset-pipeline:1.3.0.3"
    }
}

我试过做
grails stop-app
grails clean
grails run-app 

但我的问题仍未解决。

最佳答案

您必须使用 mailService Controller 类中的bean:

class TournamentController {
    static scaffold = true

    def mailService // Inject the bean

    def emailParticipants(Tournament t) {
    def emailSubject = "Tournament ${t.title} complete"

    for (Prediction p : t.predictions){
        if (p.email != null){
            def emailBody = """\
                Hello ${p.name}!  Thank you for participating in the ${t.title} tournament.
                Please find a link below with statistics and match results.
                """
            mailService.sendMail { // Use the bean's sendMail method
                async true
                to p.email
                subject emailSubject
                body emailBody
            }

关于grails - 无法在 grails 应用程序中调用 sendMail 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28844436/

相关文章:

grails - Grails:找不到创建 Controller ?

grails - 如何将具有两个提交按钮的表单提交给Grails中的两个不同的 Controller 操作

spring - 将 Camel 2.1 与 Grails 1.2.1 一起使用 - 类加载问题

hibernate - hibernate 列表与4个表不同

grails - 将grails 2.3.0升级到2.3.8时出错

grails - 错误 pool.ConnectionPool - 无法创建池的初始连接

Grails 2.4 命令对象可为空约束和依赖注入(inject)

macos - Grails创建应用错误

sql - 我怎样才能使 Grails/GORM 在 postgres 中使用默认序列值?

grails - Grails-在 View 中获取对登录用户的引用