Email MuleSoft Integration

Gursimran Singh Ranhotra
Another Integration Blog
6 min readJan 4, 2024

Sending and receiving email notifications play an important role in the integration platform. At the end of this article, you will learn how to send, retrieve, count, delete email messages, and perform other related operations using different protocol servers and MuleSoft as an iPaaS tool.

Important Terminologies:
Let’s understand some basic terminologies that would help us understand this integration better:

SMTP

SMTP (Simple Mail Transfer Protocol) is a widely used TCP protocol for email sending. The SMTP protocol is mainly used by the clients to send emails to the servers or for email communications between servers.
When you click “send” on an email, the message travels from your device to an SMTP server. An SMTP protocol on the server finds the correct route. The server sends the email to the recipient using the route it selects. Works on TCP port number 25. It does not store mail on the server it just sends the mail.

IMAP

Internet Message Access Protocol (IMAP) is an inbound email protocol. IMAP keeps your emails on the server by default and stores only small reference copies of them on your devices. This feature synchronizes your emails within every folder across all the computers associated with the account, allowing you to access your messages via multiple devices.
Since IAMP allows access to emails from multiple locations simultaneously, it keeps the email on the server after being delivered. Also, it doesn’t download the entire email until the recipient opens it. Works on TCP port number 143. It stores all mail on the server and downloads when it gets request to download.

POP3

POP3, short for post office protocol, version 3, is an inbound email protocol. An inbound protocol allows a device to retrieve email messages from a server. For example, if your computer supports POP3, you can download email messages to your local machine from a server. This protocol stores the emails within your inbox folder on your device.

By default, POP3 deletes the messages from the server once the download is complete. POP3 transfers emails from the server to the client, allowing you to read them even if you are not connected to the internet. Works on TCP port number 110. It downloads all the mail when it is connected to the internet.

Note-
1. SMTP is a push protocol whereas IMAP and POP3 are pull protocols
2. SMTP works between the sender’s mail server to receiver’s mail server and the sender and sender’s mail server. IMAP and POP work between the receiver and the receiver’s mail server.

Steps-

  1. Configuring the Email Connector:

Navigate to studio and import module from Search in Exchange

2. In the Mule palette — The emailmodule would be displayed as below. Drag and drop the Send connector to Mule flow.

Setting up Gmail:

1. Go to the following link:
https://myaccount.google.com/apppasswords

2. Mention your email password and log inusing that email with which you want to connect with Mulesoft.

3. After logging in successfully, you will be redirected to the page like below-

4. There you can type your Appname and once you click on save, a password is generated.

5. Copy the mentioned password, and there you go!

1. Sending Emails:

1. Drag and drop the Send connector from the palette to the Mule flow.

2. Connector configuration:

You can keep the connection as SMTP or SMTPS (SMTP with TLS configuration)

Give the following details:
1. Host (Host name of the mail server: If the Hostname value is defined with a property name and the value is empty, then it defaults to the localhost value)
Example: While connecting to Gmail, it will be smtp.gmail.com

2. Port (The port number of the mail server; Default: 25)

3. User (Username used to connect with the mail server)

4. Password (Username password to connect with the mail server)

Once done, please click on OK and fill in the other details.

3. Click on Advanced and in the properties section, select the dropbox.
Now, add the following property Key: “mail.smtp.starttls.enable” with its Value as: “true”

4. Configure the From Address, To Addresses, subject, and content fields.
You can also send attachments and add Cc addresses, Bcc addresses, and other fields if needed.

There you go!

5. Run the flow, and on successful completion, we will receive the mail.

2. Retrieving Email-

As discussed earlier, you can use Pop3 or IMAP protocols to retrieve emails.

•IMAP-

Use connector list IMAP to retrieve all emails present in a particular email box-

  1. Give the following details:

1. Host (Example while connecting to Gmail: imap.gmail.com)
2. Port
3. User
4. Password

Give connections as IMAP or IMAPs depending upon your need.

2. Give the correct folder name from where you want to retrieve the email.

3. Here you go. Once the program is run, you will be able to retrieve all email details.

I’m adding all the details of the emails extracted through this connector in an Excel sheet through my MuleSoft flow.

• POP3-

  1. Use connector List- POP3 to retrieve all emails present in a particular email box-

Give the following details:
1. Host (Example while connecting to Gmail: pop.gmail.com)
2. Port
3. User
4. Password

2. Add details in it, including the mailbox folder (Folder from which you want to retrieve the emails)-

Here you go, all emails in particular lists are retrieved.

3. Count Emails:

Keep Count messages- IMAP or Count messages-POP3 to count emails present in a particular folder.
Do the respective connector configuration for IMAP or POP3 as defined previously.

Additionally, add the mailbox folder for which you want to count the
emails.

Note: Alternatively, you can also use the function sizeOf() to retrieve the email count, after using the List-IMAP/List-POP3 connector.

4. Delete Emails:

You can delete emails using the Delete connector.

Once the connector configuration is done, add the mailbox folder (from which you want to delete the email) and the email ID.

Note: The email ID field mentioned is the number of the required email to be deleted. (Generally retrieved from data through attributes.id)

Thanks for reading!
Please feel free to comment and follow me for more helpful articles and insights in the future.

--

--