How to Send Email Using Mule

Posted on the 13 November 2013 by Abhishek Somani @somaniabhi
Sending Email using mule is very easy by using SMTP Outbound End Point . Here is a sample mule application to send mail. To use gmail network to send mails , you have to use gmail smtp connector , otherwise you will get tls errors .
<?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" version="CE-3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
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://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 ">
 <smtp:gmail-connector name="gmail" />

    <flow name="mailTestFlow1" doc:name="mailTestFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP" 
        path="sendMail"/>
        <component doc:name="Java" class="Component1"/>
        <smtp:outbound-endpoint host="smtp.gmail.com" port="587" 
        user="yourEmailAddress%40gmail.com" password="pass" to="javaroots@gmail.com" 
        from="Abhishek Somani" subject="Testing mule" responseTimeout="10000" connector-ref="gmail" doc:name="Send notification email"/>
    </flow>
</mule>
use %40 instead of @ in user field ,otherwise you might get error like this :
ERROR 2013-10-10 15:02:20,702 [main] org.mule.module.launcher.application.DefaultMuleApplication: null
org.mule.api.endpoint.MalformedEndpointException: The endpoint "smtp://yourEmailAddress@gmail.com:pass@smtp.gmail.com:587" is malformed and cannot be parsed.  If this is the name of a global endpoint, check the name is correct, that the endpoint exists, and that you are using the correct configuration (eg the "ref" attribute).  Note that names on inbound and outbound endpoints cannot be used to send or receive messages; use a named global endpoint instead.
 at org.mule.endpoint.UserInfoEndpointURIBuilder.setEndpoint(UserInfoEndpointURIBuilder.java:34)
 at org.mule.endpoint.AbstractEndpointURIBuilder.build(AbstractEndpointURIBuilder.java:55)
 at org.mule.endpoint.MuleEndpointURI.initialise(MuleEndpointURI.java:223)
 at org.mule.endpoint.AbstractEndpointBuilder.doBuildOutboundEndpoint(AbstractEndpointBuilder.java:246)
 at org.mule.endpoint.AbstractEndpointBuilder.buildOutboundEndpoint(AbstractEndpointBuilder.java:122)
 at org.mule.endpoint.DefaultEndpointFactory.getOutboundEndpoint(DefaultEndpointFactory.java:89)
 at org.mule.config.spring.factories.OutboundEndpointFactoryBean.doGetObject(OutboundEndpointFactoryBean.java:50)
 at org.mule.config.spring.factories.AbstractEndpointFactoryBean.getObject(AbstractEndpointFactoryBean.java:43)

......
it is a known Bug . Here is the component class where you can set your email body .
import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;


public class Component1 implements Callable{


 @Override
 public Object onCall(MuleEventContext eventContext) throws Exception {
  eventContext.getMessage().setPayload("This is email Body ");
  return eventContext.getMessage();
 }

}



If you want to send html multimedia mails , use content type field in smtp or gmail connector like this :
<smtp:connector name="smtpConnector" contentType="text/html" />

Setting mimeType on smtp outbound endpoint will have no impact , unless you set contentType property on smtp connector.