你可以在mulesoft的每个语句中获取附件文件名吗?

时间:2016-03-03 05:07:43

标签: mule filenames email-attachments

我有一封电子邮件发送到文件方案。电子邮件包含多个附件,我需要逐个处理它们以传递给另一个流程,我需要原始文件名。我可以在每个范围之前获取附件名称,但我不能将其用作目标文件的文件名。任何帮助或建议都将非常感激。

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:batch="http://www.mulesoft.org/schema/mule/batch" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:pop3="http://www.mulesoft.org/schema/mule/pop3" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/pop3 http://www.mulesoft.org/schema/mule/pop3/current/mule-pop3.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/batch http://www.mulesoft.org/schema/mule/batch/current/mule-batch.xsd">
    <file:connector name="File" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/>
    <flow name="Knauf_Load_List">
        <pop3:inbound-endpoint host="mailhost" user="exampleuser" password="examplepass" responseTimeout="10000" doc:name="Poll Email PDF"/>
        <set-session-variable variableName="State" value="#[message.inboundProperties.To]" doc:name="Session Variable"/>
        <set-variable variableName="filename()" value="#[message.inboundAttachments.keySet()]" doc:name="Variable"/>
        <expression-transformer doc:name="Expression">
            <return-argument evaluator="attachments-list" expression="*.pdf"/>
        </expression-transformer>
        <foreach doc:name="For Each">
            <set-payload value="#[payload.getContent()]" doc:name="Set Payload"/>
            <logger message="#[flowVars.filename]" level="INFO" doc:name="Logger"/>
            <file:outbound-endpoint path="\\HO-AWH-OCRPROD1\CustomerOrders\Knauff" outputPattern="Knauf_Bunnings_Orders-#[message.id].pdf" connector-ref="File" responseTimeout="10000" doc:name="File"/>
            <set-session-variable variableName="PDF" value="#[flowVars.counter]" doc:name="Session Variable"/>
        </foreach>
        <logger message="#[sessionVars.PDF] for #[sessionVars.State]" level="INFO" doc:name="Logger"/>
    </flow>
    <flow name="ocr_app_test_edi_flow">
        <file:inbound-endpoint path="\\HO-AWH-OCRPROD1\Output\Knauff\EDIEnterprise" moveToPattern="OCR_Archive-#[server.dateTime.format('dd-MM-yy_HH-mm-ss')].xml" moveToDirectory="S:\AWHMan\IT\IT Middleware Project\Ocr To EDI Archive" responseTimeout="10000" doc:name="File" connector-ref="File"/>
        <file:file-to-string-transformer doc:name="File to String"/>
        <set-variable variableName="order" value="#[xpath3('/Payload/WhsDockets/WhsDocket/Reference')]" doc:name="Variable"/>
        <logger level="INFO" doc:name="Logger"/>
        <file:outbound-endpoint path="\\HO-AWH-FS6\ediEnterprise Import Files" outputPattern="Knauf-#[flowVars.order].xml" connector-ref="File" responseTimeout="10000" doc:name="File"/>
    </flow>
</mule>

2 个答案:

答案 0 :(得分:0)

好,

不知道这是否可以提供,但我已为IMAP帐户实施了以下代码:

    <choice doc:name="Choice between mail with and without attachments">
        <when expression="#[message.dataType.mimeType == &quot;multipart/alternative&quot;]">
            <set-variable variableName="hasAttachment" value="false" doc:name="Set hasAttachment"/>
            <set-payload value="#[message.inboundAttachments['0'].getContent()]" doc:name="Set Payload from Body"></set-payload>
            <set-variable variableName="fileName" value="" doc:name="Set fileName"/>
        </when>
        <when expression="#[message.dataType.mimeType == &quot;multipart/mixed&quot;]">
            <set-variable variableName="hasAttachment" value="true" doc:name="Set hasAttachment"/>
            <expression-transformer doc:name="Set Payload from Attachments">
                <return-argument evaluator="attachments" expression="*.*"/>
            </expression-transformer>
            <set-variable variableName="fileName" value="#[payload.keySet().iterator().next()]" doc:name="Set fileName"/>
            <set-payload value="#[payload[flowVars.fileName].getContent()]" doc:name="Set Payload from firts Attachment content"></set-payload>
            <object-to-byte-array-transformer doc:name="Object to Byte Array"></object-to-byte-array-transformer>
        </when>
        <when expression="#[message.dataType.mimeType == &quot;text/plain&quot;]">
            <set-variable variableName="hasAttachment" value="false" doc:name="Set hasAttachment"/>
            <set-payload value="#[payload.getContent()]" doc:name="Set Payload from Text"/>
            <set-variable variableName="fileName" value="" doc:name="Set fileName"/>
        </when>
        <otherwise>
            <logger message="La informaci&#243;n del EMail no es v&#225;lida" level="INFO" doc:name="Logger"/>
        </otherwise>
    </choice>

正如您在WHEN“#[message.dataType.mimeType ==”multipart / mixed“]”中看到的那样,检索所有附件,在我的情况下,第一个附件我得到包含的第一个迭代器值附件名称:

#[payload.keySet().iterator().next()]

使用此名称,您可以直接访问此附件的内容,或者您​​可以使用相应的密钥访问它们。

问候语。

答案 1 :(得分:0)

使用#[key],这将在foreach语句中给出文件名。 我使用的是3.8版本