【问题标题】:Send multiple attachments with Grails Async Mail Plugin使用 Grails 异步邮件插件发送多个附件
【发布时间】:2012-03-13 04:28:15
【问题描述】:

我正在使用 grails Async Mail 插件在我的应用程序中发送带有附件的电子邮件。为了实现这一点,我编写了以下代码

def sendEmail = {
    def documentsInstances = Documents.getAll(params.list('ids[]'))
    def s3Service = new AmazonS3Service()
    documentsInstances.each(){documentsInstance->
            asyncMailService.sendMail {
                multipart true
                to documentsInstance.author.emailAddress
                subject 'Test';
                html '<body><u>Test</u></body>';
                attachBytes documentsInstance.filename , 'text/plain', s3Service.getBytes(session.uid,documentsInstance.filename);
            }
    }//

}

现在上面的代码几乎可以正常工作,但它会为每个附件发送一封电子邮件,我不确定如何将这个循环移动到发送邮件中,以便我可以在一封电子邮件中发送多个附件。

还有没有办法发送电子邮件,这样我就不必在 byte[] 中加载整个文件?

我正在使用 JetS3t 访问 Amazon S3,并尝试使用“附加”方法

new InputStreamResource(s3Obj.getDataInputStream())

attach documentsInstance.filename , 'text/plain', new InputStreamResource(s3Obj.getDataInputStream());

但我得到了

"Passed-in Resource contains an open stream: invalid argument. JavaMail requires an InputStreamSource that creates a fresh stream for every call"

【问题讨论】:

    标签: email grails plugins groovy


    【解决方案1】:

    你需要你的循环里面电子邮件:

    def sendEmail = {
        def documentsInstances = Documents.getAll(params.list('ids[]'))
        def s3Service = new AmazonS3Service()
        asyncMailService.sendMail {
            multipart true
            to documentsInstance.author.emailAddress
            subject 'Test';
            html '<body><u>Test</u></body>';
    
            // loop over attachments
            documentsInstances.each{ documentsInstance->
                attachBytes documentsInstance.filename , 'text/plain', s3Service.getBytes(session.uid, documentsInstance.filename);
            }
        }
    }
    

    这应该附加多个文件。

    如果它抛出关于无法找到attachBytes 方法的错误,您可能需要显式调用owner.attachBytes,它应该引用外部sendMail 闭包。


    基于 cmets 的更新:

    Async 插件看起来像普通邮件插件一样。普通邮件插件的文档描述了how to use multiple attachments

    这看起来像:

       // loop over attachments
        documentsInstances.each{ documentsInstance->
            attach documentsInstance.filename , 'text/plain', s3Service.getBytes(session.uid, documentsInstance.filename);
          //^^^^^^ Notice, just 'attach', not 'attachBytes'
        }
    

    我不知道您正在使用的 S3 插件,但如果有办法从中检索 InputStreamSource,您似乎可以将字节直接流式传输到邮件插件,而不是将它们加载到内存中.

    【讨论】:

    • 看起来 attachBytes 已被弃用且文档已过时,请查看 MailMessageBuilder 插件 src 中的“附加”方法
    • 啊,如果异步插件遵循直邮插件,那么this page of the mail plugin 可能有助于使多个附件正常工作。
    • 谢谢,我曾尝试在“sendMail”中移动循环,但我收到一个关于无法找到 attachBytes 方法的错误。但现在它正在使用您的解决方案。我正在为我的服务使用 JetS3t,我尝试使用新 InputStreamResource(s3Obj.getDataInputStream()) 的“附加”方法,但我得到“传入的资源包含一个开放流:无效参数。JavaMail 需要一个创建新流的 InputStreamSource每次通话”
    • @OverZealous 我试图通过创建自己的实现 InputStreamSrouce 的类来规避 InputStream 的问题,但现在在相同的代码中我得到 groovy.lang.MissingMethodException: No signature of method: com.fluffystorage。 DocumentsController.attach() 适用于参数类型:(java.lang.String, java.lang.String, com.fluffystorage.AmazonS3Resource) 任何帮助将不胜感激
    • 检查我上面所说的:你可能不得不打电话给owner.attach。它似乎正在寻找DocumentsController 上的附加方法。如果这不起作用,请尝试delegate.attachthis.attach(尽管我怀疑这些是否正确)。或者,您可能正在处理异步邮件插件根本不支持attach 的问题。
    猜你喜欢
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多