Grails 邮件插件设置 CC 参数

标签 grails

我正在使用邮件插件,并希望在运行时设置抄送参数。

  // build CC list at runtime
  def arr = []

  arr.add("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d7c79796f2c5d69786e69337e7270" rel="noreferrer noopener nofollow">[email protected]</a>")
  arr.add("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04656060763644706177702a676b69" rel="noreferrer noopener nofollow">[email protected]</a>")

  try {
    mailService.sendMail {
       to params.toAddress
       from "myname <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d30381d35323038733e3230" rel="noreferrer noopener nofollow">[email protected]</a>>"
       subject subjectLine
       cc arr
       body params.emailContent
    }    
  } catch(all) {
    log.error all
  } 

如果 cc 为空,则会抛出异常:cc 不能为 null 或空

如何设置cc参数或在不需要时将其删除?

最佳答案

给你:

try {
    mailService.sendMail {
        to params.toAddress
        from "myname <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81ece4c1e9eeece4afe2eeec" rel="noreferrer noopener nofollow">[email protected]</a>>"
        subject subjectLine
        if (arr) {    // Check if there is any value in the cc list
            cc arr
        }
        body params.emailContent
    }
} catch(all) {
    log.error all
}

闭包 就像常规方法一样,您可以在其中使用任何表达式、赋值。因此,只需使用 Groovy 的 true 来测试 List 中是否存在任何值。

此外,我认为您还必须在将 arr 传递给 cc() 方法之前调用 toArray :

cc(arr.toArray())

因为,文档是这么说的:

There is a pitfall when using a List for storing the recipients. You'll have to invoke toArray when providing it to the builder.

关于Grails 邮件插件设置 CC 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33412732/

相关文章:

Grails GORM 日期数据绑定(bind)

grails - 网址中的编码斜杠问题

java - 如何在grails gform中呈现java.time.localdate

java - Wildfly Jboss NoClassDefFoundError : Failed to link Error for org/jboss/ws/core/jaxws/spi/ProviderImpl

grails - 无法解析Grails插件项目导入javax.websocket

hibernate - 使用 Grails GORM 从旧数据库中的 char 字段中去除尾随空格

grails - 依靠 hasMany 关系在 grails 中创建标准

mongodb - 是否可以将 groupProperty 与 mongoDB 和 Grails 一起使用?

grails - 在Grails/spring security中收集/定义最后访问的信息的最佳方法是什么?

jquery - 在 Grails REST 中使用 params 获取 JSON 对象