如何从Grails中的域/服务发送邮件

时间:2012-07-18 09:25:39

标签: grails grails-plugin grails-2.0

我安装了this plugin,我想从Service / Domain类方法发送邮件,我这样做

   class TrainingService {
      def mailService
      public def sendMail() {
        mailService.sendMail {
            multipart true
            to "abc@xyz.com"
            subject "Hello,"
            body 'How are you?'

          }
}

我收到错误“无法在null对象上调用方法sendMail()”,如何解决此问题

3 个答案:

答案 0 :(得分:1)

我错过了“from”属性,如果您使用的是多部分电子邮件,则还应该从我的代码中填写电子邮件的其他部分,代码段:

mailService.sendMail {
            multipart true
            from '"Some account" <someaccount@email.com>'
            to 'anotheremail@somedomain.com'
            bcc emailAddresses.toArray()
            subject dto.title
            body emailPart1
            html g.render(
                    template: 'emailNotification',
                    model: [ name: dto.name ]
            )

}

答案 1 :(得分:0)

您是否配置了SMTP服务器。您需要在配置文件中使用这些条目才能使邮件正常工作。你有这些吗?

grails {
    mail {
                    host = "hostname"
                    pop_port = 25
                    username = "username"
                    password = "password"
                    type = "pop3"

                }
}

您可以找到有关配置here

的更多信息

答案 2 :(得分:0)

当我传递名为&#39;的变量时,我收到了此错误。和&#39;到&#39;进入使用sendMail的方法。尝试重命名sendMail方法,并确保没有任何冲突的变量名称。