3

我是 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>

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

提前致谢。

4

2 回答 2

8

To help you, here are two configuration bits:

  • The following filter accepts only messages where fromAddress is 'Bob' and where subject contains 'keyword':

    <expression-filter
        expression="#[message.inboundProperties.fromAddress == 'Bob' || message.inboundProperties.subject contains 'keyword']" />
    
  • The following transformer extracts all the attachments whose names end with '.csv':

    <expression-transformer
        expression="#[($.value in message.inboundAttachments.entrySet() if $.key ~= '.*\\.csv')]" />
    
于 2012-12-31T17:49:46.633 回答
3

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

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

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

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

Improvment

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)
于 2012-12-31T17:07:34.310 回答