【问题标题】:Example for adding attachment to email at mule在 mule 中向电子邮件添加附件的示例
【发布时间】:2014-02-03 16:33:29
【问题描述】:

请问如何在Mule(3.4.0)中添加文件作为附件?

我尝试了很多解决方案并在 Google 上搜索了很多,但没有找到任何好的解决方案。

这就是我现在所拥有的(发帖前的最后一次尝试):

<sub-flow name="sendBackMail" doc:name="sendBackMail">
    <set-attachment attachmentName="changed.xml" value="#[payload]" contentType="text/xml" doc:name="Attachment"/>
    <logger message="Attachment ok" level="INFO" doc:name="Logger"/>
    <file:file-to-byte-array-transformer doc:name="File to Byte Array"/>
    <logger message="Attachment ok. Message: #[message]" level="INFO" doc:name="Logger"/>
    <smtps:outbound-endpoint host="${mailSMTP}" port="${mailSendPort}" 
        user="${mailUser}" password="${mailPass}" to="${receiver}" 
        from="${mailUser}" responseTimeout="60000"  doc:name="SMTP" 
        connector-ref="SMTP"  mimeType="text/xml" subject="msp2bass" >
    </smtps:outbound-endpoint>
</sub-flow>

它发送邮件,但我将 xml 文件的内容作为邮件正文。

我应该做什么/更改,以便邮件作为附件发送。我从另一个服务获取文件,对其执行 XSLT,然后我应该将其发送到某个邮件。

我应该提供更多信息吗?

谢谢!

编辑

我如何称呼 bean:

<spring:bean id="SetAttachment" name="SetAttachment" class="si.irose.msp.cust.bass.SetAttachment">
</spring:bean>

<component doc:name="Java">
    <spring-object bean="SetAttachment"/>
</component>

Java 类:

package si.irose.msp.cust.bass;

import org.mule.api.MuleEventContext;
import org.mule.api.MuleMessage;
import org.mule.api.lifecycle.Callable;

public class SetAttachment implements Callable{ 
    private MuleMessage mule;
    private String name;
    @Override
    public Object onCall(MuleEventContext eventContext) throws Exception {
        mule = eventContext.getMessage();
        String tryit="routeid";
        for (int i=0;i<mule.getInvocationPropertyNames().toArray().length;i++) {
            if (mule.getInvocationPropertyNames().toArray()[i].equals(tryit)) {
                name=mule.getInvocationProperty(mule.getInvocationPropertyNames().toArray()[i].toString()).toString();
                break;
            }
        }
        mule.addOutboundAttachment(name, mule.getInvocationProperty(name), "text/xml");
        return null;
    }
}

【问题讨论】:

标签: email smtp mule


【解决方案1】:

好的问题解决了。

当您从入站文件读取或使用石英(字符串或文件)生成时,您可以直接添加“附件”,但如果您进行 XSLT 转换然后尝试通过 smtp 发送该有效负载,那么您需要添加“对象到String" 连接前的变压器。

最终的工作解决方案:

<sub-flow name="sendBackMail" doc:name="sendBackMail">
    <object-to-string-transformer doc:name="Object to String"/>
    <set-attachment attachmentName="changed.xml" value="#[payload]" contentType="text/xml" doc:name="Attachment"/>
    <logger message="Attachment ok" level="INFO" doc:name="Logger"/>
    <file:file-to-byte-array-transformer doc:name="File to Byte Array"/>
    <logger message="Attachment ok. Message: #[message]" level="INFO" doc:name="Logger"/>
    <smtps:outbound-endpoint host="${mailSMTP}" port="${mailSendPort}" 
        user="${mailUser}" password="${mailPass}" to="${receiver}" 
        from="${mailUser}" responseTimeout="60000"  doc:name="SMTP" 
        connector-ref="SMTP"  mimeType="text/xml" subject="msp2bass" >
    </smtps:outbound-endpoint>
</sub-flow>

如果你做任何其他工作而不是 XSLT,我还没有检查你需要应用什么转换器,所以如果有人想为此添加一些知识,那么请这样做并添加评论。

【讨论】:

    【解决方案2】:

    您需要在smtps:outbound-endpoint 之前使用set-payload 将电子邮件正文设置为您想要的任何内容,否则它将包含与set-attachment 相同的值。

    同时删除file:file-to-byte-array-transformer:尚不清楚它的用途。

    【讨论】:

    • 这很合理,但我根本没有依恋。之前 body 是 xml 内容,现在它是我放入 set payload 的值。我认为问题在于设置附件,因为我不发送任何邮件。
    • 这很奇怪,但我发现set-attachment 配置元素存在问题。您可以使用MuleMessage 上的addOutboundAttachment 方法通过代码尝试。
    • 有什么办法可以解决这个问题或其他任何事情吗?也许指向有用链接的方向(但我在这里问之前做了很多谷歌搜索)?
    • 当您使用set-attachment 时,您是否在日志中收到任何错误/警告?当您尝试通过代码附加时会怎样?
    • 当我使用设置附件时,日志中没有任何内容,当我调用 java.util.看看编辑,我发布了我是如何调用 bean 的。
    猜你喜欢
    • 2011-02-03
    • 2012-06-06
    • 2023-03-22
    • 2023-03-21
    • 2019-12-02
    • 2019-05-05
    • 2020-03-06
    • 2020-11-27
    • 1970-01-01
    相关资源
    最近更新 更多