【问题标题】:Send an email using Grails - beginner使用 Grails 发送电子邮件 - 初学者
【发布时间】:2014-06-24 11:45:44
【问题描述】:

我在 BuildConfig.groovy 文件中取消了以下注释。

mail:1.0.4

现在我正在尝试发送确认电子邮件。

我在 config.groovy 文件中添加了以下内容。

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

   }
}

我还有一个控制器叫UserController.groovy,它有一个方法叫:

def sendEmail ()= {

 // how can i send the email

} 

我在这里迷路了,我应该包含什么代码才能成功发送这封电子邮件?

【问题讨论】:

  • 本帖没有问题

标签: grails


【解决方案1】:

这就是您将在控制器文件中执行的操作。

class UserController {

     def mailService // injecting the mail service bean in your controller.

     def sendEmail () {

        mailService.sendMail {
           to ....
           from ....
           ....
        }
     } 
}

【讨论】:

  • 我收到此错误534-5.7.9 Application-specific password required. Learn more at 534 5.7.9 http://support.google.com/accounts/bin/answer.py?answer=185833 qo12sm41860326igb.21 - gsmtp
  • @Illep 您是否尝试过阅读错误消息链接到的页面?您的 gmail 帐户似乎启用了两步验证。
  • 是的,我愿意。但该页面无法访问。
  • @Illep 如果您已经到了这一点,那么您的邮件插件实际上正在工作。如果您启用链接中指出的验证过程,那么您应该一切顺利。
【解决方案2】:

您需要的一切都在documentation

编辑:@Illep,我使用您最后的评论更新了代码

导入 mailService 并使用以下示例发送您的电子邮件:

class UserController {

  def mailService

  def sendEmail() {

    mailService.sendMail {
       to "fred@g2one.com","ginger@g2one.com"
       from "john@g2one.com"
       cc "marge@g2one.com", "ed@g2one.com"
       bcc "joe@g2one.com"
       subject "Hello John"
       body 'this is some text'
    }
  }
}

对于 HTML 电子邮件,它有点不同,您将使用 groovy 渲染。

【讨论】:

  • 我收到了这个错误No such property: mailService for class: com.app.auth.UserController
  • @Illep 您必须按照惯例使用 grails 自动装配。当您在服务中调用此代码时,您只需在此处添加 def mailService
  • 您可以完全删除 mailService 部分,插件应该可以解决这个问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多