23.3 Mail Namespace Support

Spring Integration provides a namespace for mail-related configuration. To use it, configure the following schema locations.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mail="http://www.springframework.org/schema/integration/mail"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/integration/mail
            http://www.springframework.org/schema/integration/mail/spring-integration-mail-1.0.xsd">

To configure an outbound Channel Adapter, provide the channel to receive from, and the MailSender:

<mail:outbound-channel-adapter channel="outboundMail"
                              mail-sender="mailSender"/>

Alternatively, provide the host, username, and password:

<mail:outbound-channel-adapter channel="outboundMail"
                              host="somehost" username="someuser" password="somepassword"/>

[Note]Note
Keep in mind, as with any outbound Channel Adapter, if the referenced channel is a PollableChannel, a <poller> sub-element should be provided with either an interval-trigger or cron-trigger.

To configure an inbound Channel Adapter, you have the choice between polling or event-driven (assuming your mail server supports IMAP IDLE - if not, then polling is the only option). A polling Channel Adapter simply requires the store URI and the channel to send inbound Messages to. The URI may begin with "pop3" or "imap":

 <mail:inbound-channel-adapter channel="mailIn"
                               store-uri="imap://usr:[email protected]/INBOX">
     <poller max-messages-per-poll="3">
         <interval-trigger interval="30" time-unit="SECONDS"/>
     </poller>
 </mail:inbound-channel-adapter>

If you do have IMAP idle support, then you may want to configure the "imap-idle-channel-adapter" element instead. Since the "idle" command enables event-driven notifications, no poller is necessary for this adapter. It will send a Message to the specified channel as soon as it receives the notification that new mail is available:

 <mail:imap-idle-channel-adapter channel="mailIn"
                                 store-uri="imaps://usr:[email protected]:993/INBOX"/>

When using the namespace support, a header-enricher Message Transformer is also available. This simplifies the application of the headers mentioned above to any Message prior to sending to the Mail-sending Channel Adapter.

<mail:header-enricher subject="Example Mail"
                     to="[email protected]"
                     cc="[email protected]"
                     bcc="[email protected]"
                     from="[email protected]"
                     reply-to="[email protected]"
                     overwrite="false"/>