在Mule 3.9中提取电子邮件附件

时间:2018-10-02 17:54:27

标签: java mule email-attachments

我需要提取电子邮件附件并将其保存到mule 3.9运行时的文件夹中。 尝试了几种方法,但我的附件文件始终为空。

以下是创建附件的代码:

        <dw:transform-message doc:name="Transform Message">
                <dw:set-payload><![CDATA[%dw 1.0
    %output application/xml
    ---
    {
        "key":"value"
    }]]></dw:set-payload>
            </dw:transform-message>
            <set-attachment attachmentName="myattachment.txt"
                value="#[payload]" contentType="text/xml" doc:name="Attachment" />
            <set-payload value="#['Mail Body']" mimeType="text/plain"
                doc:name="Set Payload" />
            <smtp:outbound-endpoint host="${email.host}"
                port="${email.port}" user="${email.user}" password="${email.password}"
                connector-ref="Gmail" to="${email.receivers}" from="${email.sender}"
                subject="${email.subject}" responseTimeout="10000" doc:name="SMTP" />

Below is the code to extract attachment:
<imaps:inbound-endpoint host="${email.inbound.host}"
            port="${email.inbound.port}" user="${email.user}" password="${email.password}"
            connector-ref="IMAP" responseTimeout="10000" doc:name="IMAP" />
        <message-properties-transformer scope="invocation" doc:name="Message Properties">
            <add-message-property key="subject" value="#[message.inboundProperties.subject]"/>
            <add-message-property key="emailAttachments" value="#[message.inboundAttachments]"/>
            <add-message-property key="emailSender" value="#[message.inboundProperties['List:X-Google-Original-From'][0]]"/>
            <add-message-property key="createdDate" value="#[server.dateTime.format(&quot;dd-MM-YYYY hh:mm:ss&quot;)]"/>
        </message-properties-transformer>

        <sub-flow name="writeEmail-to-File">

        <choice doc:name="Choice">
            <when expression="#[flowVars.emailAttachments.size()!=0]">
                <foreach collection="#[message.inboundAttachments]"
                    doc:name="For Each">
                    <dw:transform-message doc:name="Read Attachment">
                        <dw:set-payload><![CDATA[ %dw 1.0
%output application/java
---
{
    "fileName": payload.dataSource.part.fileName,
    "filedata": payload.dataSource.inputStream,
    "fileSize": payload.dataSource.part.size
}
        ]]></dw:set-payload>
                        <dw:set-variable variableName="fileName"><![CDATA[%dw 1.0
%output application/java
---
{
    "fileName": payload.dataSource.part.fileName
}]]></dw:set-variable>
                    </dw:transform-message>
                    <set-variable variableName="tempFilePattern"
                        value="${file.write.path}\#[flowVars.emailSender]\#[flowVars.subject.replace(&quot; &quot;,&quot;&quot;) + server.dateTime.format(&quot;dd-mm-yyyy&quot;)]"
                        doc:name="File Pattern" />
                    <set-payload value="#[payload.filedata]" doc:name="Set Payload" />
                    <byte-array-to-string-transformer
                        doc:name="Byte Array to String" />
                    <file:outbound-endpoint
                        path="${file.write.path}\#[flowVars.emailSender]\#[flowVars.subject.replace(&quot; &quot;,&quot;&quot;) + '-' + server.dateTime.format(&quot;dd-mm-yyyy&quot;)]"
                        outputPattern="#[flowVars.fileName.fileName]" connector-ref="File"
                        responseTimeout="10000" doc:name="File" />
                </foreach>
            </when>
            <otherwise>
                <set-variable variableName="filePattern"
                    value="#[flowVars.subject + &quot; - &quot; +flowVars.createdDate.replace(':','-')].txt"
                    doc:name="File Pattern" />
                <file:outbound-endpoint path="${file.write.path}\#[flowVars.emailSender]"
                    outputPattern="#[flowVars.filePattern]" connector-ref="File"
                    responseTimeout="10000" doc:name="File" />
            </otherwise>
        </choice>
    </sub-flow>

如果邮件没有任何附件,则流程正常。但是,如果有任何附件,我只能看到空文件,而看不到要驱动的附件的内容。 任何帮助将不胜感激! 谢谢!

0 个答案:

没有答案