MuleSoft Email Connector – Part 1

What is the email link?

MuleSoft Connector for Email sends and retrieves emails via standard email protocols such as SMTPS/SMTP, IMAP, and POP3.

Some use cases for email connectors include:

  • Retrieve emails from POP3 mailboxes
  • Recover, delete and store emails from IMAP mailboxes
  • Sending emails via SMTP

A prerequisite to get started with these how-tos is to add email and file connectors in Anypoint Studio. You can add these connectors from Add units Selection. Also, you must have a Google account because we will be using Gmail to send an email. Also, you need to turn on Gmail Less secure apps Other settings You will not be able to send mail from MuleSoft or any external application.

Let’s first create an SMTP configuration item for global email. To create a generic item, you can go to File Global Elements Click Create and search for Email SMTP.

In the config we have to define a connection as SMTPS connection And check the Unsafe checkbox. Provide details below:

  • host = smtp.gmail.com
  • port = 465
  • User = Your Gmail ID
  • Password = Your Gmail password

SMTP Email Configuration Item Overview

Let’s configure the email sending process to send an email. In the to the titles In the field we have to pass a set of emails to which we want to send an email. in a Content In the body section, we pass the HTML content that we obtained as a result of the template parsing process. In the parsing form, we just associated the blood donation form-register-mail-template.html specified under src/main/resources with an input request.

In the attachments In the Attachments section, we have passed the expression “Blood-Donation.jpg”: vars.image where Blood-Donation.jpg is the name of the attachment and vars.image is the content of the attached file. that image variable Holds the output file read operation. If we want to send multiple attachments, we can define JSON objects.

You can refer to the screenshot below to configure the sending process.

Send process configuration

Our flow will look like this once it’s complete. I have provided the mule flow code at the end.

Complete the Mule flow to send an email with HTML text with an attachment.

Let’s run the test with the postman.

Postman shot


Email Snapshot

Mule flow code for reference:

“++item.name++” “++item.age++” “++item.bloodType++”

“) JoinBy” “]” doc: name = “create-email-table-content” doc: id = “c741d5e6-9192-42bc-bdde-c444e58db093” />




“data-lang=”application/xml”>

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:tls="http://www.mulesoft.org/schema/mule/tls" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:email="http://www.mulesoft.org/schema/mule/email" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/tls http://www.mulesoft.org/schema/mule/tls/current/mule-tls.xsd">
	<email:smtp-config name="Email_SMTP" doc:name="Email SMTP" doc:id="60188ae8-00d9-4805-a8d0-ca588189fd57" from='test@gmail.com'>
		<email:smtps-connection host="smtp.gmail.com" user="test@gmail.com" password="********">
			<tls:context>
				<tls:trust-store insecure="true" type="jceks"></tls:trust>
				<tls:key-store type="pkcs12"></tls:key>
			</tls:context>
		</email:smtps-connection>
	</email:smtp-config>
	<file:config name="File_Config2" doc:name="File Config" doc:id="26d4aa0f-e0ee-4dc6-ade6-9eb0f179010f">
		<file:connection workingDir="C:UsersluffyDownloads"></file:connection>
	</file:config>
	<flow name="send-email" doc:id="d8cba714-c4d5-4c15-a68b-cba4cccc807a">
		<http:listener doc:name="listener" doc:id="af9b9af3-1717-4ea7-8bc5-fe04597c5036" config-ref="HTTP_Listener_config" path="/email-send"></http:listener>
		<file:read doc:name="read-attachment-file" doc:id="18d301eb-2bbc-4f6f-8c6e-7d8d2f48266b" config-ref="File_Config2" path="Blood-Donation.jpg" target="image"></file:read>
		<set-payload value="#[%dw 2.0

output text/plain

---

payload map ((item, index) -> 

  "<tr><td>" ++ item.name ++ "</td><td>" ++ item.age ++ "</td><td>" ++ item.bloodType ++ "</td></tr>") joinBy " "]" doc:name="create-email-table-content" doc:id="c741d5e6-9192-42bc-bdde-c444e58db093"/>
		<parse-template doc:name="parse-email- template" doc:id="2470fbc9-67a8-46d1-bd3b-824f6ea77915" location="blood-donation-registration-mail-template.html"></parse>
		<email:send doc:name="send-mail" doc:id="5e906be9-cfd2-4774-99ae-55b9191b9ed8" config-ref="Email_SMTP" fromAddress="test@gmail.com" subject="#["Blood donation registration list"]" toAddresses="#[["test@gmail.com"]]">
			<email:body contentType="text/html">
			</email:body>
			<email:attachments><![CDATA[#["Blood-Donation.jpg": vars.image]]]></email:attachments>
		</email:send>
		<set-payload value="#[%dw 2.0

output application/json 

---



{

	"message": "An email has been sent successfully."

}]" doc:name="response" doc:id="8ece226a-7110-48c8-901c-8d9d3a20ea97"></set>
	</flow>
</mule>

Content of blood donation-register-mail-template.html file for reference:

<!DOCTYPE html>
<html>
   <head>
      <style>
         table {
         font-family: arial, sans-serif;
         border-collapse: collapse;
         width: 100%;
         }
         td, th {
         border: 1px solid #dddddd;
         text-align: left;
         padding: 8px;
         }
         tr:nth-child(even) {
         background-color: #dddddd;
         }
      </style>
   </head>
   <body>
      <h4>Please find below a list of people who have registered for blood donation.</h4>
      <table>
         <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Blood Type</th>
         </tr>
         #[payload] 
      </table>
   </body>
</html>

This is. I hope you learned to use the MuleSoft Email Connector to send an HTML email with an attachment.

.

Leave a Comment