Spring Integration supports various e-mail protocols available today in the industry, allowing sending of e-mail based on incoming channel messages and the receiving of e-mails as channel messages. We will now discuss IMAP, POP3, and SMTP examples in detail in this section. Listing 22-61 shows the main dependency required for supporting Spring Integration email in your application.
Listing 22-61. Maven dependency required
1 2 3 4 5 6 7 8 9 |
<dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-mail</artifactId> <version>${spring-integration-version}</version> </dependency> |
Listing 22-62 below shows configuration of main Spring Integration namespace and mail related namespace in the Spring configuration file (mail-integration-context.xml).
Listing 22-62. Spring Integration Namespace declaration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" 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-3.1.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsd http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail-2.1.xsd"> … </beans> |
IMAP
The mail inbound channel adapter support both the IMAP and POP3 mail protocol. Listing 22-63 below shows configuring of mail inbound channel adapter to configure Gmail and to receive messages using IMAP protocol.
Listing 22-63. Configuring mail inbound channel adapter using IMAP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<beans …> … <mail:inbound-channel-adapter id="imapAdapter" store-uri="imaps://[username]:[password]@imap.gmail.com/INBOX" java-mail-properties="javaMailProperties" channel="recieveChannel" should-delete-messages="true" should-mark-messages-as-read="true" auto-startup="true"> <int:poller max-messages-per-poll="1" fixed-rate="5000"/> </mail:inbound-channel-adapter> <util:properties id="javaMailProperties"> …<!-- IMAP related configurations --> </util:properties> … </beans> |
POP3
Listing 22-64 below shows configuring of mail inbound channel adapter to configure Gmail and to receive messages using POP3 protocol.
Listing 22-64. Configuring mail inbound channel adapter using POP3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<beans …> … <mail:inbound-channel-adapter id="pop3Adapter" store-uri="pop3://[username]:[password]@pop.gmail.com/inbox" java-mail-properties="javaMailProperties" channel="recieveChannel" should-delete-messages="true" should-mark-messages-as-read="true" auto-startup="true"> <int:poller max-messages-per-poll="1" fixed-rate="5000"/> </mail:inbound-channel-adapter> <util:properties id="javaMailProperties"> …<!-- POP3 related mail configuration for Gmail --> </util:properties> … </beans> |
SMTP
The mail outbound channel adapter uses the SMTP protocol to send e-mail messages. According to the message mapping strategies, if the input message payload is an instance of the org.springframework.mail.MailMessage interface, it will be sent directly as text message and if the input message is a byte array it will be sent as an e-mail attachment. The mail outbound channel adapter can be configured in two ways as shown in Listing 22-65 and Listing 22-66 below.
Listing 22-65. Configuring mail outbound channel adapter using channel and mail sender
1 2 3 4 5 6 7 8 9 10 11 |
<beans …> … <mail:outbound-channel-adapter channel="outboundMail" mail-sender="mailSender"/> … </beans> |
Listing 22-66. Configuring mail outbound channel adapter using host, it’s username and password
1 2 3 4 5 6 7 8 9 10 11 |
<beans …> … <mail:outbound-channel-adapter channel="outboundMail" host="somehost" username="someuser" password="somepassword"/> … </beans> |
Page Visitors: 13052

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Hi,
Thanks for this blog and information sharing!
I have one question from Spring Integration. Just thought of checking with you.
I have SimplewebserviceOutBoundGateway. I need to send a document / attachment to the webservice. How will i achieve that?
This is my current gateway configuration:
Please let me know, if you can throw some light in this area.
Thanks in Advance!
Hi,
Thank you for your information sharing.
I have one doubt,could you please suggest me the correct frameworks to address that issue.
My requirement is I would like to get and process files using spring and camel.
Thanks,
Madhu