在groovy类中发送带有grails邮件插件的邮件 - null config error

时间:2014-07-14 19:46:48

标签: grails plugins groovy sendmail

我收到错误"由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

4 个答案:

答案 0 :(得分:1)

我通过将sendEmail部分移动到控制器来解决了这个问题。然后,从src / groovy中的任何类调用控制器方法。因为,在控制器中,可以使用def grailsApplication注入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()
           }
       }
   }
   }

答案 1 :(得分:0)

我想这个插件会将mailService注入您的应用程序。因此,不要像您一样使用构造函数创建服务:

def mailService = new MailService()

只需声明字段并让DI机制完成注入值的工作:

def mailService

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

希望它能帮助你!

答案 2 :(得分:0)

我认为这不会起作用:

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

答案 3 :(得分:-1)

打开您的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')
   } 
}