Wednesday, 13 November 2013

How to Send email using mule

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="" xmlns:http="" xmlns="" xmlns:doc="" xmlns:spring="" version="CE-3.3.1" xmlns:xsi="" xsi:schemaLocation=" ">
 <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" 
        <component doc:name="Java" class="Component1"/>
        <smtp:outbound-endpoint host="" port="587" 
        user="" password="pass" to="" 
        from="Abhishek Somani" subject="Testing mule" responseTimeout="10000" connector-ref="gmail" doc:name="Send notification email"/>
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://" 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(
 at org.mule.endpoint.MuleEndpointURI.initialise(
 at org.mule.endpoint.AbstractEndpointBuilder.doBuildOutboundEndpoint(
 at org.mule.endpoint.AbstractEndpointBuilder.buildOutboundEndpoint(
 at org.mule.endpoint.DefaultEndpointFactory.getOutboundEndpoint(
 at org.mule.config.spring.factories.OutboundEndpointFactoryBean.doGetObject(
 at org.mule.config.spring.factories.AbstractEndpointFactoryBean.getObject(

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{

 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.