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 :
https://myaccount.google.com/lesssecureapps
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="http://www.mulesoft.org/schema/mule/smtp" 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:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.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/smtp http://www.mulesoft.org/schema/mule/smtp/current/mule-smtp.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8085" basePath="/api" doc:name="HTTP Listener Configuration"/>
<smtp:connector name="SMTP" contentType="text/plain" fromAddress="****@gmail.com" subject="Hello" validateConnections="true" doc:name="SMTP"/>
<smtp:gmail-connector name="Gmail" contentType="text/plain" fromAddress="****@gmail.com" 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="smtp.gmail.com" port="587" user="****@gmail.com" password="*****" connector-ref="Gmail" to="****@gmail.com" from="****@gmail.com" subject="Hello" responseTimeout="10000" mimeType="text/plain" doc:name="SMTP">
<property key="mail.smtp.starttls.enable" value="true"/>
</smtp:outbound-endpoint>
</flow>
</mule>