Send email attachment using SMTP in Mule ESB

What is Mule ESB ?

Mule ESB is a lightweight Java-based enterprise service bus (ESB) and integration platform that allows developers to connect applications together quickly and easily, enabling them to exchange data. Mule ESB enables easy integration of existing systems, regardless of the different technologies that the applications use, including JMS, Web Services, JDBC, HTTP, and more.

This example shows how we can send email attachment using SMTP connector to self or another email. We simply put a file in a source directory and Mule will read the file from the source directory. Using Mule ESB it’s very easy to read the file from a location. If we had to read the file using manual coding then we had to write many lines of code. But using Mule ESB we just simply put a file in a directory and let the Mule know the file path and Mule does the rest of the thing. You can put any kind of file to the source for reading. The file that we put into a source directory will be an attachment to the intended recipient’s mail address. So finally when we will see the file as an attachment in the mailbox.

What is Attachment transformer ?

Attachment transformer lets us attach a file or document which will be sent to the email address.

What is SMTP connector ?

The SMTP endpoint allows Mule messages to be sent as email or email attachments with the Simple Mail Transfer Protocol (SMTP) using Java mail API.

Prerequisites

Mule Studio 3.5.1
JDK 1.6
Maven 3
Mule Runtime 3.5.0 CE
Knowledge of XML

Step 1. Open Mule Studio and create a mule project. Go to File->New->Mule Project. Now enter the Project Name, select Runtime – Mule Server, check the check box for Use Maven. Now enter the following information

Group Id: your company(ex. roytuts.com) name – com.roytuts
Artifact Id: project name – file-attachment(automatically populated when project name is getting typed)
Version: Mule studio generates default

Step 2. Click Next and verify project Location and JRE/JDK are set correctly. Click Finish.

Step 3. Now drag and drop the elements from right side of the Mule studio as shown below in the picture.

Send Email Attachment using SMTP in Mule ESB

Send Email Attachment using SMTP in Mule ESB

File connector properties
email attachment using SMTP in Mule ESB

Browse and select the path from where a file will be read for an attachment.

Logger component properties
email attachment using SMTP in Mule ESB

Logger component will display content in console from each flow.

File to String transformer properties

File-to-Strong transformer just transforms the file object to human readable string format. We will not set any property for this element.

Attachment transformer properties
email attachment using SMTP in Mule ESB
Using attachment transformer we can set payload from previous flow as an attachment. We have to select Content Type, operation as Set Attachment, Name, Value.

SMTP connector properties
email attachment using SMTP in Mule ESB
For SMTP connector we have to input email host, user name, password, port under Basic Settings and To Address, From Address, Subject under Email Information.

The whole XML file of entire flow

Step 4. Now create a text file called file-attachment.txt and put it under selected path, i.e., as per the above XML the file path is D:AnypointWorkspacesource.

Step 5. Do right-click on file-attachment.xml and click on Run As -> Mule Application with Maven. Once the application up and running you will get the console output and email attachment in your mailbox where you have sent the email.

Output in console

Output in mailbox

Click on email. You will get the below content for the mail.

Output in mailbox  Click on email. You will get the below content for the mail.    Click on the attachment. You will see the below output in the browser.    That's all. Thanks for reading.

Click on the attachment. You will see the below output in the browser.

Send Email Attachment using SMTP in Mule ESB

That’s all. Thanks for reading.

 

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

One thought on “Send email attachment using SMTP in Mule ESB

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.