我正在使用邮件插件,并希望在运行时设置抄送参数。
// 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/