Mule ESB: how to filter emails based on subject or sender?

99封情书 提交于 2019-12-03 20:14:56

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.

  • 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" />
    
  • 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;
        }
    }
    

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)

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')]" />
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!