The following example shows how to expose a SOAP service that send a mail using the SMTP outbound and the Gmail connector.
GmailService-config.xml (mule-config.xml file)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:context="http://www.springframework.org/schema/context" xmlns:smtps="http://www.mulesoft.org/schema/mule/smtps" xmlns:smtp="http://www.mulesoft.org/schema/mule/smtp" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="CE-3.3.0" 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/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd http://www.mulesoft.org/schema/mule/smtps http://www.mulesoft.org/schema/mule/smtps/current/mule-smtps.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd "> <!-- The Gmail connector is part of connectors that Mule ESB offers out of the box. --> <smtp:gmail-connector name="emailConnector" doc:name="Gmail" /> <!-- The flows starts here --> <flow name="GMailService-configFlow1" doc:name="GMailService-configFlow1"> <!-- Configuring the HTTP inbound --> <http:inbound-endpoint exchange-pattern="request-response" address="http://localhost:9094/gmailService" doc:name="HTTP" /> <!-- In this block the SOAP CXF Service is configured. We need two java classes one is for set up the Simple Front end of the Web Service and the other one is for return the payload to the flow, in this case the mail that we want to send. --> <cxf:jaxws-service serviceClass="com.marco.tello.gmail.service.IGmailService" doc:name="IGmailService" enableMuleSoapHeaders="false"/> <component class="com.marco.tello.gmail.service.GmailService" doc:name="GMailService" /> <object-to-string-transformer doc:name="Object to String" /> <!-- We generate a response for the SOAP Service. --> <response> <set-payload value="Your Message has been sent." doc:name="Set Payload" /> </response> <logger message="******** #[payload]" level="INFO" doc:name="Logger" doc:description="Just for log the message to send"/> <echo-component doc:name="Echo" /> <!-- Setting the outbound Note that the user has to be set up with an "%40" instead of "@" otherwise the SMTP outbound wont work, and you'll have to do the same for the password all the @'s wont wont work. --> <smtps:outbound-endpoint host="smtp.gmail.com" user="your_address%40gmail.com" password="THE_PASSWORD" from="your_gmail_user@gmail.com" subject="Email from Mule ESB" doc:name="Send notification email" to="your_address@some_domain.com" /> </flow> </mule> |
In resume a simple CXF service is being exposed to send some text through Gmail.
The GmailService.java class is used to return the payload to Mule’s flow.
An Echo component is added to generate the response flow, and a SetPayload Transformer is placed to generate the response text.
The Logger component is useful to keep a track of the message that you want to send.
You have to consider the following things before you test the service:
- The “user” attribute uses the escape code of “%40” to replace the “@” symbol.
- As the “user attribute the “password” needs “%40” escape code too every time you want to place an “@”.
- Note that in “from” and “to” attributes the “@” symbol will work perfectly.
IGmailService.java
1 2 3 4 5 6 7 8 9 10 11 |
package com.marco.tello.gmail.service; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface IGmailService { @WebMethod(operationName = "sendMail") public String sendMail(String text); } |
This will expose a simple CXF service in Mule ESB with a method called “sendMail” that accept a String as input.
GmailService.java
1 2 3 4 5 6 7 8 9 10 |
package com.marco.tello.gmail.service; public class GmailService implements IGmailService { @Override public String sendMail(String text) { return text; } } |
This class returns the text that was passed through the Web Service to the Mule’s payload.
You can find the project in Github here.
Here is the source code of the project
7 comments: On Gmail Service in Mule ESB
Muchas Gracias Marco, for the tip on the %40 escape for the @. I forgot about that important detail when i was using the GMAIL SMTP server!
R U any related to the Senor Tello on the FC Barcelona squad ?
–Ciao
Mark Casagrande
Hola Mark,
Thanks for the reply, I’m glad to hear that it helped you.
BTW I’m not related to Tello but I play almost like him (I’m just kidding)
Adios 🙂
-Marco.
Good work Marco! Very well explained.
can you please explain how to expose a mule flow as a webservice…
we tried the application but no errors
gmail service started.we are not getting any mail.
Hi Lakshmi, I have a tutorial for creating a simple Web Service with Mule here http://marcotello.com/mule-esb/simple-web-service-in-mule-esb-3-3/
Hi Vamsi, do you have the log of the Mule’s flow?, try to start Mule up and call the Web Service, Do you see any exception?