Attachment Transformer in Mule | Mail Transfer through SMTP

Attachment Transformer in Mule | Mail Transfer through SMTP

An attachment transformer is used to set, remove, or copy attachments on the outbound scope of a message.
Each Mule message consists of two parts. The header contains metadata pertaining to the message, such as the message type and the encoding protocol used to format the payload. The payload holds the data that typically gets processed by the Mule application flow, then dispatched to one or more external parties. This payload may also include a sub-section that holds any attachments (such as text, images, or applications) that travel with the message.

When an attachment is received into a flow along with its associated message, it is considered an inbound property, and it remains active (that is, affixed to its parent message) as long as that message remains within the same flow. Note that the attachment does not automatically become part of the processed message that gets sent out of the flow by the outbound connector. For that to happen, you must explicitly make it an outbound property by reattaching it to the message with the attachment transformer. Alternatively, you can designate a different attachment to leave the flow along with the processed message.

The attachment transformer can use expressions that evaluate the content of the current message as well as the current state of the Mule environment in order to make a runtime determination as to which specific attachment leaves the flow with the current message. For example, the Attachment transformer can retrieve a photo that matches some content in the message payload and affix it to the message.

flow of Attachment Transformer :

Attachment configuration :

SMTP connector settings :

go to connector configuration and select Gmail then fill necessary fields mentioned below

Go to advance tab and put key-value mentioned below :

URL : http://localhost:8085/api/attachment
Method : POST

Input : 

Output :

logger output :

One important thing is you might get critical security alert mail from google for sign in by some different device. for this you have to turn on less secure app access by link :

then go to critical security alert mail sent by google and choose option Yes it was me

Now its all set !!!....Hit the URL and you will get the mail in your inbox.

XML project code :

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:smtp="" xmlns:http="" xmlns="" xmlns:doc=""
    <http:listener-config name="HTTP_Listener_Configuration" host="" port="8085" basePath="/api" doc:name="HTTP Listener Configuration"/>
    <smtp:connector name="SMTP" contentType="text/plain" fromAddress="****" subject="Hello" validateConnections="true" doc:name="SMTP"/>
    <smtp:gmail-connector name="Gmail" contentType="text/plain" fromAddress="****" subject="Hello" validateConnections="false" doc:name="Gmail"/>
    <flow name="test_attachmentFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/attachment" doc:name="HTTP"/>
        <set-attachment attachmentName="doc" value="#[payload]" contentType="text/plain" doc:name="Attachment"/>
        <object-to-string-transformer doc:name="Object to String"/>
        <smtp:outbound-endpoint host="" port="587" user="****" password="*****" connector-ref="Gmail" to="****" from="****" subject="Hello" responseTimeout="10000" mimeType="text/plain" doc:name="SMTP">
            <property key="mail.smtp.starttls.enable" value="true"/>