Sample 256: Proxy Services with the Mail Transport

<definitions xmlns="http://ws.apache.org/ns/synapse"> <proxy name="StockQuoteProxy" transports="mailto"> <target> <inSequence> <property name="senderAddress" expression="get-property('transport', 'From')"/> <log level="full"> <property name="Sender Address" expression="get-property('senderAddress')"/> </log> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </inSequence> <outSequence> <property name="Subject" value="Custom Subject for Response" scope="transport"/> <header name="To" expression="fn:concat('mailto:', get-property('senderAddress'))"/> <log level="full"> <property name="message" value="Response message"/> <property name="Sender Address" expression="get-property('senderAddress')"/> </log> <send/> </outSequence> </target> <publishWSDL uri="file:repository/conf/sample/resources/proxy/sample_proxy_1.wsdl"/> <parameter name="transport.mail.Address">synapse.demo.mail1@gmail.com</parameter> <parameter name="transport.mail.Protocol">pop3</parameter> <parameter name="transport.PollInterval">5</parameter> <parameter name="mail.pop3.host">pop.gmail.com</parameter> <parameter name="mail.pop3.port">995</parameter> <parameter name="mail.pop3.user">synapse.demo.mail1</parameter> <parameter name="mail.pop3.password">mailpassword</parameter> <parameter name="mail.pop3.socketFactory.class">javax.net.ssl.SSLSocketFactory</parameter> <parameter name="mail.pop3.socketFactory.fallback">false</parameter> <parameter name="mail.pop3.socketFactory.port">995</parameter> <parameter name="transport.mail.ContentType">application/xml</parameter> </proxy> </definitions>

Objective

This sample show cases the mail transport of Synapse. The mail transport allows Synapse to receive and send e-mails using common protocols like POP, IMAP and SMTP.

Pre-requisites

  • You need access to an e-mail account to try out this sample
  • Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
  • Enable the mail transport listener and mail transport sender for Synapse (refer Mail transport setup guide for more details)
  • Start Synapse using the configuration numbered 256 (repository/conf/sample/synapse_sample_256.xml)
    Unix/Linux: sh synapse.sh -sample 256
    Windows: synapse.bat -sample 256

Executing the Client

Send an e-mail to synapse.demo.mail1@gmail.com with the following payload.

<getQuote xmlns="http://services.samples"> <request> <symbol>IBM</symbol> </request> </getQuote>

Synapse will be polling on the above e-mail account for any incoming requests. When your mail arrives at this account, Synapse will pick it up and send the payload to Axis2 over HTTP. The response will be mailed back to your e-mail account. Synapse retrieves the sender information from the original request to determine the recipient of the response mail.

Note that in this sample we used the transport.mail.ContentType property to make sure that the transport parses the request message as POX. If you remove this property, you may still be able to send requests using a standard mail client if instead of writing the XML in the body of the message, you add it as an attachment. In that case, you should use .xml as a suffix for the attachment and format the request as a SOAP 1.1 message. Indeed, for a file with suffix .xml the mail client will most likely use text/xml as the content type, exactly as required for SOAP 1.1. Sending a POX message using this approach will be a lot trickier, because most standard mail clients don't allow the user to explicitly set the content type.

Back to Catalog