[英]Mule ESB: how to filter emails based on subject or sender?
我是Mule 3.3的新手,如果发件人字段和主题字段包含某些关键字,我正尝试使用它从POP3服务器检索电子邮件并下载CSV附件。 我使用了Mulesoft网站上提供的示例,我已成功设法扫描我的收件箱中的新电子邮件,并且只下载了CSV附件。 但是,我现在卡住了,因为我无法弄清楚如何按主题和发件人字段过滤电子邮件。
做一些研究我遇到了一个可以应用于端点的消息属性过滤器模式标签,但我不确定应用它的哪个端点,传入或传出。 这两种方法似乎都不起作用,我找不到一个体面的例子来展示如何使用这个标签。 我想实现的基本算法如下:
if email is from "Bob"
if attachment of type "CSV"
then download CSV attachment
if email subject field contains "keyword"
if attachment of type CSV
then download CSV attachment
这是我到目前为止的Mule xml:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:pop3s="http://www.mulesoft.org/schema/mule/pop3s" 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" version="CE-3.3.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/pop3s http://www.mulesoft.org/schema/mule/pop3s/current/mule-pop3s.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
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 ">
<expression-transformer expression="#[attachments-list:*.csv]"
name="returnAttachments" doc:name="Expression">
</expression-transformer>
<pop3s:connector name="POP3Connector"
checkFrequency="5000"
deleteReadMessages="false"
defaultProcessMessageAction="RECENT"
doc:name="POP3"
validateConnections="true">
</pop3s:connector>
<file:connector name="fileName" doc:name="File">
<file:expression-filename-parser />
</file:connector>
<flow name="incoming-orders" doc:name="incoming-orders">
<pop3s:inbound-endpoint user="my_username"
password="my_password"
host="pop.gmail.com"
port="995"
transformer-refs="returnAttachments"
doc:name="GetMail"
connector-ref="POP3Connector"
responseTimeout="10000"/>
<collection-splitter doc:name="Collection Splitter"/>
<echo-component doc:name="Echo"/>
<file:outbound-endpoint path="/attachments"
outputPattern="#[function:datestamp].csv"
doc:name="File" responseTimeout="10000">
<expression-transformer expression="payload.inputStream"/>
<message-property-filter pattern="from=(.*)(bob@email.com)(.*)" caseSensitive="false"/>
</file:outbound-endpoint>
</flow>
解决这个问题的最佳方法是什么?
提前致谢。
为了帮助您,这里有两个配置位:
以下过滤器仅接受其中fromAddress
为“Bob”且主题包含“keyword”的邮件:
<expression-filter expression="#[message.inboundProperties.fromAddress == 'Bob' || message.inboundProperties.subject contains 'keyword']" />
以下转换器提取名称以“.csv”结尾的所有附件:
<expression-transformer expression="#[($.value in message.inboundAttachments.entrySet() if $.key ~= '.*\\\\.csv')]" />
欢迎来到骡子! 几个月前,我为客户实施了类似的项目。 我看看你的流程,让我们开始重构。
将以下元素添加到您的流中
<pop3:inbound-endpoint ... /> <custom-filter class="com.benasmussen.mail.filter.RecipientFilter"> <spring:property name="regex" value=".*bob.bent@.*" /> </custom-filter> <expression-transformer> <return-argument expression="*.csv" evaluator="attachments-list" /> </expression-transformer> <collection-splitter doc:name="Collection Splitter" />
将我的RecipientFilter作为java类添加到您的项目中。 如果它们与正则表达式模式不匹配,则将丢弃所有消息。
package com.benasmussen.mail.filter; import java.util.Collection; import java.util.Set; import java.util.regex.Pattern; import org.mule.api.MuleMessage; import org.mule.api.lifecycle.Initialisable; import org.mule.api.lifecycle.InitialisationException; import org.mule.api.routing.filter.Filter; import org.mule.config.i18n.CoreMessages; import org.mule.transport.email.MailProperties; public class RecipientFilter implements Filter, Initialisable { private String regex; private Pattern pattern; public boolean accept(MuleMessage message) { String from = message.findPropertyInAnyScope(MailProperties.FROM_ADDRESS_PROPERTY, null); return isMatch(from); } public void initialise() throws InitialisationException { if (regex == null) { throw new InitialisationException(CoreMessages.createStaticMessage("Property regex is not set"), this); } pattern = Pattern.compile(regex); } public boolean isMatch(String from) { return pattern.matcher(from).matches(); } public void setRegex(String regex) { this.regex = regex; } }
骡子表达框架功能强大,但在某些用例中我更喜欢自己的业务逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.