Mule下载电子邮件正文和附件

时间:2014-06-05 15:58:15

标签: mule email-attachments pop3

如何让Mule使用pop3下载附件?我尝试尽可能地跟踪http://www.mulesoft.org/documentation/display/current/POP3+Transport+Reference上的示例,但我一直收到两个文件:一个带有电子邮件正文,另一个带有实际附件。这是我正在使用的流程:

<pop3:connector name="pop3Connector" checkFrequency="5000" doc:name="POP3"/>

<expression-transformer name="returnAttachments" doc:name="Expression">
    <return-argument evaluator="attachments-list" expression="*" />
</expression-transformer>

<file:connector name="fileName" doc:name="File">
    <file:expression-filename-parser/>
</file:connector>

<flow name="incoming-orders" doc:name="incoming-orders">
    <pop3s:inbound-endpoint host="pop.gmail.com" port="995" user="myuser%40mydomain" password="mypassword" responseTimeout="10000" doc:name="POP3" transformer-refs="returnAttachments" />
    <collection-splitter doc:name="Collection Splitter"/>
    <file:outbound-endpoint path="C:/popthreetest" outputPattern="#[function:datestamp].dat" doc:name="File">
        <expression-transformer>
            <return-argument expression="payload.inputStream" evaluator="groovy" />
        </expression-transformer>
    </file:outbound-endpoint>                   
</flow>

谢谢!

修改

这是基于@David Dossot答案的最终流程。我有一个额外的复杂性,我正在读取一个JSON文件,该文件指定附件名称和附件的任意目的地。我添加了replaceAll,因为我在路径file:///C:\中收到有关无效字符的错误。

<pop3:connector name="pop3Connector" checkFrequency="5000" doc:name="POP3"/>

<expression-transformer name="returnAttachments" doc:name="Expression">
    <return-argument evaluator="attachments-list" expression="*" />
</expression-transformer>

<file:connector name="DestinationsFileConnector" doc:name="File" autoDelete="false" streaming="true" validateConnections="true">
    <file:expression-filename-parser/>
</file:connector>
<file:endpoint path="C:/popthreetest/" name="DestinationsFileEndpoint" responseTimeout="10000" doc:name="File" connector-ref="DestinationsFileConnector">
    <file:filename-regex-filter pattern="destinations\.json" caseSensitive="true"/>
</file:endpoint>
<mulerequester:config name="DestinationsMuleRequestorConnector" doc:name="Mule Requester"/>

<flow name="incoming-orders" doc:name="incoming-orders">
    <pop3s:inbound-endpoint host="pop.gmail.com" port="995" user="myusername%40mydomain" password="mypassword" responseTimeout="10000" doc:name="POP3" transformer-refs="returnAttachments" />
    <collection-splitter doc:name="Collection Splitter"/>
    <set-variable variableName="MessagePart" value="#[message.payload]" doc:name="MessagePart"/>
    <logger message="Got #[message.payload.dataSource.name]." level="INFO" doc:name="Logger"/>
    <mulerequester:request config-ref="DestinationsMuleRequestorConnector" resource="DestinationsFileEndpoint" doc:name="GetDestinations"/>
    <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
    <choice doc:name="Choice">
        <when expression="#[message.payload.get(MessagePart.dataSource.name) != null]">
            <set-payload value="#[message.payload.get(MessagePart.dataSource.name)]" doc:name="Destination List"/>
            <foreach doc:name="For Each">
                <logger message="Saving to #[message.payload]." level="INFO" doc:name="Logger"/>
                <set-variable variableName="DestinationPath" value="#[java.nio.file.Paths.get(message.payload).getParent().toString().replaceAll('\\\\', '/')]" doc:name="DestinationPath"/>
                <set-variable variableName="DestinationPattern" value="#[java.nio.file.Paths.get(message.payload).getFileName()]" doc:name="DestinationPattern"/>
                <logger message="Saving to #[DestinationPattern] in #[DestinationPath]." level="INFO" doc:name="Logger"/>
                <set-payload value="#[MessagePart]" doc:name="MessagePart"/>
                <file:outbound-endpoint path="#[DestinationPath]" outputPattern="#[DestinationPattern]" doc:name="File">
                    <expression-transformer> 
                        <return-argument expression="payload.inputStream" evaluator="groovy"/> 
                    </expression-transformer>
                </file:outbound-endpoint>
            </foreach>
        </when>
        <otherwise>
            <logger message="Did not find destination(s) for #[MessagePart.dataSource.name]." level="INFO" doc:name="Logger"/>
        </otherwise>
    </choice>
</flow>

为了完整性,这是JSON文件:

{
    "attachment-name.txt": [
        "C:/popthreetest/firstDestination.txt"
      , "C:/path/to/secondDestination.txt"
      , "C:\\popthreetest\\destination\\using-backslashes.txt"
    ]
}

1 个答案:

答案 0 :(得分:1)

带附件的电子邮件是一个多部分电子邮件,附件是部件,但也是正文。因此,Mule只能下载“整个包”,并为您提供不同的部分。

您应该能够根据部件名称在collection-splitter之后过滤身体部位。

或者,您可以使用MEL表达式删除集合的第一个元素,通常是正文(Mule在内部使用此技术来设置消息有效负载:https://github.com/mulesoft/mule/blob/mule-3.x/transports/email/src/main/java/org/mule/transport/email/transformers/EmailMessageToString.java#L50