【问题标题】:Sending mails with grails mail plugin in groovy class - null config error在 groovy 类中使​​用 grails 邮件插件发送邮件 - 空配置错误
【发布时间】:2014-07-14 19:46:48
【问题描述】:

我收到错误“由 NullPointerException 引起:null ->> 64 | grails.plugin.mail.MailService 中的 getMailConfig”。我已经按照邮件插件提供的文档配置了 config.groovy。

请帮我解决这个问题。

在下面找到我的 config.groovy 代码。

grails
{

    mail {
        host = "smtp.gmail.com"
        port = 465
        username = "xxxxxx@gmail.com"
        password = "xxxxxx"
        props = ["mail.smtp.auth":"true",
                 "mail.smtp.socketFactory.port":"465",
                 "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
                 "mail.smtp.socketFactory.fallback":"false"]
      }
}

在下面找到我的 groovy 类代码。

package common

import grails.plugin.mail.*;

public class FlowSchedule implements Job {

    def mailService = new MailService()

    public void execute(JobExecutionContext context)
           throws JobExecutionException {

                    //some extra logic here
                   sendEmail(schedulerEntry.name,schedulerEntry.email)

    }
    def sendEmail(String name,String email)
    {

            mailService.sendMail {
                to "amith.ravuru@citrix.com"
                subject "Hello Amith"
                body 'this is some text'
            }
    }
}

完整的错误跟踪:

Error |
2014-07-14 12:41:00,041 [DefaultQuartzScheduler_Worker-4] ERROR core.ErrorLogger  - Job     (group.Job_1 threw an exception.

Message: Job threw an unhandled exception.
   Line | Method
->> 213 | run in org.quartz.core.JobRunShell

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

**^   573 | run in org.quartz.simpl.SimpleThreadPool$WorkerThread
Caused by NullPointerException: null**

**->>  64 | getMailConfig in grails.plugin.mail.MailService**

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    59 | sendMail in     ''
|    94 | sendEmail in common.FlowSchedule$$EOk1HyVA
|    79 | execute in     ''
|   202 | run in org.quartz.core.JobRunShell
^   573 | run in org.quartz.simpl.SimpleThreadPool$WorkerThread

【问题讨论】:

  • 你能在你的工作中打印grailsApplication.config.grails.mail的值,看看它是否正确吗?
  • 不,grailsApplication 以 Null 形式出现。我通过将我的代码移动到一个新的控制器中并从 src/groovy 文件夹中存在的类中调用该函数来解决它。当我的 groovy 类出现在 src/groovy 文件夹中时,没有发生注入。

标签: grails plugins groovy sendmail


【解决方案1】:

我通过将 sendEmail 部分移动到控制器解决了这个问题。然后,从 src/groovy 中的任何类调用控制器方法。因为,在控制器中,grailsApplication 可以使用 def grailsApplication 注入。请在下面找到控制器代码。

   class SendMailController 
   {

   def grailsApplication

   def sendEmail(String name,String email,String mailsubject)
   {

       if(email != null)
       {


           try
           {

               sendMail {
                   to email 
                   from "exabgpnotifier@gmail.com"
                   subject "ExaBGP Notification"
                   body "Hello "+name+"\n\n"+ mailsubject +" \n\nRegards,\nExaBGP Team"
                }
           }
           catch(Exception e)
           {
               e.printStackTrace()
               println "ERROR!!: Unable to send the notification to email "+ finalemaillist.toString()
           }
       }
   }
   }

【讨论】:

    【解决方案2】:

    我猜这个插件将 mailService 注入到您的应用程序中。因此,不要像您所做的那样使用构造函数创建服务:

    def mailService = new MailService()
    

    只要声明字段,让DI机制来做值注入工作:

    def mailService
    

    代码的其余部分看起来还不错。考虑更改 sendMail 方法的名称,现在当您的方法名称与来自 mailService 的方法名称相同时,这有点令人困惑。

    希望对您有所帮助!

    【讨论】:

    • 我收到以下错误消息,如果我使用 def mailService,则无法在 null 对象上调用方法 sendMail。 ^ 573 |在 org.quartz.simpl.SimpleThreadPool$WorkerThread 中运行由 NullPointerException 引起:无法在 null 对象上调用方法 sendMail() ->> 91 | sendEmail in common.FlowSchedule
    • FlowSchedule 文件的位置在哪里?您可以尝试将文件重命名为 FlowScheduleJob 吗?
    【解决方案3】:

    我认为这行不通:

    grails
    {
    
        mail {
            host = "smtp.gmail.com"
            port = 465
            username = "xxxxxx@gmail.com"
            password = "xxxxxx"
            props = ["mail.smtp.auth":"true",
                     "mail.smtp.socketFactory.port":"465",
                     "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
                     "mail.smtp.socketFactory.fallback":"false"]
          }
    }
    

    这是有效的代码,但不符合您的预期。 grails 之后的左大括号需要在同一行,因此闭包被视为 grails 方法的参数。

    grails {
    
        mail {
            host = "smtp.gmail.com"
            port = 465
            username = "xxxxxx@gmail.com"
            password = "xxxxxx"
            props = ["mail.smtp.auth":"true",
                     "mail.smtp.socketFactory.port":"465",
                     "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
                     "mail.smtp.socketFactory.fallback":"false"]
          }
    }
    

    【讨论】:

    • 我按照您的建议更改了代码,但我得到了与之前提到的相同的错误。 “由 NullPointerException 引起:null ->> 64 | grails.plugin.mail.MailService 中的 getMailConfig”
    【解决方案4】:

    打开你的 resources.groovy 文件并输入这个条目。

    import common.FlowSchedule
    beans = {
       // make sure you import flow schedule class
       flowSchedule(FlowSchedule) {
          mailService = ref('mailService')        
      }
    }
    

    然后在你的课堂上。

    package common
    
    import grails.plugin.mail.*;
    
    public class FlowSchedule implements Job {
    
      def mailService
    
      public void execute(JobExecutionContext context)
           throws JobExecutionException {
    
                    //some extra logic here
                   sendEmail(schedulerEntry.name,schedulerEntry.email)
    
      }
      def sendEmail(String name,String email)
      {
    
            mailService.sendMail {
                to "amith.ravuru@citrix.com"
                subject "Hello Amith"
                body 'this is some text'
            }
      }
    }
    

    当您需要任何控制器或服务中的流调度时,只需执行此操作

    class MyService {
    
       def flowSchedule
    
       def myMethod() {
          flowSchedule.sendEmail('test','test@test.com')
       } 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 2016-03-16
      相关资源
      最近更新 更多