Mule ESB:如何根据主题或发件人过滤电子邮件? [英] Mule ESB: how to filter emails based on subject or sender?

查看:145
本文介绍了Mule ESB:如何根据主题或发件人过滤电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Mule 3.3的新功能,如果发件人字段和主题字段包含某些关键字,我正在尝试使用它从POP3服务器检索电子邮件并下载CSV附件。我已经使用了Muleoft网站上提供的示例,并且我已成功设法扫描收件箱中的新电子邮件,并且只下载CSV附件。但是,由于我无法弄清楚主题和发件人字段如何过滤电子邮件,所以我现在陷入困境。

I am new to Mule 3.3 and I am trying to use it to retrieve emails from a POP3 server and download the CSV attachments if the sender field and subject field contain certain keywords. I have used the example provided on Mulesoft website and I have successfully managed to scan my inbox for new emails and only download CSV attachments. However, I am now stuck because I can't figure out how to filter emails by subject and sender fields.

做了一些研究,我遇到了可以应用于端点的消息属性过滤器模式标签,但我不确定准确到哪个端点应用它,传入或传出。这两种方法似乎都不起作用,我找不到一个体面的例子来展示如何使用这个标签。我想实现的基本算法如下:

Doing some research I have come across a message-property-filter pattern tag that can be applied to an endpoint, but I am not sure exactly to which endpoint to apply it, incoming or outgoing. Neither approach seems to work and I can't find a decent example showing how to use this tag. The basic algorithm I want to implement is as follows:

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:

Here's the Mule xml I have so far:

<?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>

什么是最好的方法解决这个问题?

What is the best way to tackle this problem?

提前感谢

推荐答案

欢迎来到骡子!几个月前,我为客户实施了类似的方案。我看看你的流程,让我们开始重构。

Welcome to Mule! A few month ago I implemented a similar proejct for a customer. I take a look at your flow, let´s start refactoring.


  • 从入站端点
  • 中删除transformer-refs =returnAttachments
  • 添加您的流程的以下元素

  • Remove the transformer-refs="returnAttachments" from inbound-endpoint
  • Add the following elements to your flow

<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类添加到您的项目中。如果它们与正则表达式不匹配,所有消息将被丢弃。

  • Add my RecipientFilter as java class to your project. All messages will be discard if they don't match to the regex pattern.

    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;
        }
    }
    


  • mule表达框架是强大的,但在某些用例中,我更喜欢我自己的业务逻辑。

    The mule expression framework is powerful, but in some use cases I prefer my own business logic.


    • 使用应用程序属性(mule-app.properties)> < a href =http://www.mulesoft.org/documentation/display/MULE3USER/Using+Parameters+in+Your+Configuration+Files =nofollow>骡子文件

    • Use application properties (mule-app.properties) > mule documentation
    • MailProperties shows you all available message properties (EMail)
    • Take a look at the mule schema doc to see all available elements
    • Incoming payload (mails, etc) are transported by an DefaultMuleMessage (Payload, Properties, Attachments)

    这篇关于Mule ESB:如何根据主题或发件人过滤电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆