使用set-attachment在mule ESB中发送具有多个附件的电子邮件不起作用

时间:2015-09-30 15:37:11

标签: mule mule-studio mule-component

<flow name="SendmailFlow" >
    <http:listener config-ref="HTTP_Listener_Configuration" path="${sendmail.path}" doc:name="HTTP"/>
    <!--  http:inbound-endpoint exchange-pattern="request-response"
        host="${sendmail.host}" port="${sendmail.port}" path="${sendmail.path}" doc:name="HTTP" /-->

    <logger message="logging #[payload]" level="INFO" doc:name="Logger" />

    <json:json-to-object-transformer
        returnClass="com.rsa.esbcommons.mailsender.MailData" doc:name="JSON to Object" />

    <foreach collection="#[payload.getAttachments()]" doc:name="For Each Attachment in MailData">           
        <set-attachment attachmentName="#[payload.getFilename()]" value="#[payload.getBase64AttachmentContent()]"
            contentType="#[payload.getContentType()]" doc:name="Create Attachment" />
    </foreach>  

    <smtp:outbound-endpoint user="${smtp.user}" password="${smtp.password}" host="${smtp.host}" from="${smtp.from}" responseTimeout="10000"
        doc:name="SMTP" mimeType="text/html" transformer-refs="MailDataToEmailTransformer StringToEmail" />
    <custom-transformer class="com.rsa.esbcommons.mailsender.MailResponseMessageTransformer" doc:name="MailResponseTransformer"/>
    <json:object-to-json-transformer sourceClass="com.rsa.esbcommons.mailsender.BaseResponse" doc:name="Sendmail response to JSON"/>

</flow>

这里的问题是在上面给出的foreach循环中使用set-attachment时附件失败。这是用于通过电子邮件发送多个附件。我们可以在foreach循环中使用set-attachment吗?请提前帮助谢谢

3 个答案:

答案 0 :(得分:1)

这篇文章已经过时但是我为那些有同样问题的人揭露了我的解决方案。

这里的问题是在foreach句子之后,mule消息正在恢复他的原始状态(空的outboundAttachments)。您必须创建临时流var,并使用当前message.outboundAttachments在每次迭代中覆盖它。最后,对于每个范围,必须设置从临时var复制的message.outboundAttachments。这是您应该尝试的代码。

<flow name="SendmailFlow" >
<http:listener config-ref="HTTP_Listener_Configuration" path="${sendmail.path}" doc:name="HTTP"/>

    <json:json-to-object-transformer
        returnClass="com.rsa.esbcommons.mailsender.MailData" doc:name="JSON to Object" />

    <set-variable variableName="attaches" value="#[new java.util.HashMap()]" />

    <foreach collection="#[payload.getAttachments()]" doc:name="For Each Attachment in MailData">           
        <set-attachment attachmentName="#[payload.getFilename()]" value="#[payload.getBase64AttachmentContent()]"
            contentType="#[payload.getContentType()]" doc:name="Create Attachment" />

        <expression-component><![CDATA[java.util.Set keys = message.outboundAttachments.keySet();
        for(Object key:keys){flowVars.attaches.put(key, message.outboundAttachments.get(key));
        }]]>
        </expression-component>
    </foreach>

    <expression-component>
    <![CDATA[
    message.outboundAttachments.putAll(flowVars.attaches);
    ]]>
    </expression-component>

<smtp:outbound-endpoint user="${smtp.user}" password="${smtp.password}" host="${smtp.host}" from="${smtp.from}" responseTimeout="10000"
        doc:name="SMTP" mimeType="text/html" transformer-refs="MailDataToEmailTransformer StringToEmail" />
    <custom-transformer class="com.rsa.esbcommons.mailsender.MailResponseMessageTransformer" doc:name="MailResponseTransformer"/>
    <json:object-to-json-transformer sourceClass="com.rsa.esbcommons.mailsender.BaseResponse" doc:name="Sendmail response to JSON"/>

</flow>`

我不确定你是否应该删除字符串到电子邮件变换器ref。这对我有用。

答案 1 :(得分:0)

您可以在foreach组件中使用set-attachment。没问题。你遇到的问题与其他问题有关。尝试调试,看看发生了什么。更好地检查您的MEL表达式是否正确返回值(将记录器保持为打印值)。请查看以下链接以获取进一步的帮助

How to add multiple mail attachment in smtp using Mule ESB

https://dzone.com/articles/handling-file-attachments

答案 2 :(得分:0)

我的解决方案与J.Antonio Rubio的解决方案非常相似,但是我将foreach内的表达式简化了一点:

<json:json-to-object-transformer
    returnClass="com.rsa.esbcommons.mailsender.MailData" doc:name="JSON to Object" />

<set-variable variableName="attaches" value="#[new java.util.HashMap()]" />

<foreach collection="#[payload.getAttachments()]" doc:name="For Each Attachment in MailData">           
    <set-attachment attachmentName="#[payload.getFilename()]" value="#[payload.getBase64AttachmentContent()]"
        contentType="#[payload.getContentType()]" doc:name="Create Attachment" />

    <expression-component><![CDATA[flowVars.attaches.putAll(message.outboundAttachments);]]>
    </expression-component>
</foreach>

<expression-component>
<![CDATA[
message.outboundAttachments.putAll(flowVars.attaches);
]]>
</expression-component>