Invoking a Web Service via Email

Version: 17.01

Supported Since: 17.01

Use Case Description

I have an integer multiplication web service exposed over HTTP, and want to make it accessible via email, where emails containing the input data sent to a predefined address are processed and replied to with the multiplication results.

Solution

UltraESB-X can be configured to proxy messages between the email service and the web service in order to enable the acceptance of data from and propagation of results to email senders.

Prerequisites

The following connectors and processing elements should be selected when creating an Ultra project for our requirement (or added to the project later via Tools → Ultra Studio → Component Registry):

  • Mail Connector (for both ingress (listener) and egress (sender))

  • NIO HTTP Connector (for sending HTTP requests to the web service)

  • Transport Header Manager (for some header manipulations)

Implementation

  1. Add the following Map for use in enabling SSL for IMAP (email polling) as well as SMTP (email sending), in project.xpml:

    Type

    Map

    Name

    mail-props

    mail.imap.ssl.enable

    true

    mail.smtp.starttls.enable

    true

  2. Add a Mail Ingress Connector to accept incoming multiplication request emails, with the following configuration (assuming a Gmail account):

    Basic

    Protocol

    imap

    Host

    imap.gmail.com

    Port

    993

    User

    your-email@gmail.com

    Password

    your-password

    Folder

    INBOX

    Other Settings

    mail-props (Map)

    Processing

    Preserve Headers (1)

    From

    Lazy-fetch Payload (2)

    Off

    Lazy-fetch Attachments (3)

    On

    Scheduling

    Polling Start Delay

    2000

    Polling Repeat Interval

    10000

    (1) avoids fetching of unnecessary email headers, as we would be submitting the message to an HTTP endpoint later on

    (2) we want the payload, so fetch it eagerly

    (3) we don’t expect attachments, and will not be accessing them even if they are present

  3. Add an Add Variable processing element to extract the value of the retained From header (sender’s email address) into a recipient variable:

    Variable Name

    recipient

    Extraction Type

    HEADER

    Value

    From

    Variable Type

    String

  4. Add a NIO HTTP Egress Connector to forward the received request to the multiplication web service:

    Basic

    Destination Address type

    URL

    Destination Host

    localhost

    Destination Port

    9000

    Service Path

    /service/JsonServlet

  5. Add an Add New Transport Header processing element to set the subject of the response email:

    Transport Header Name

    mail.subject

    Use Variable

    Off

    Value

    Multiplication Result

    Header Variable Type

    String

  6. Restore the previously saved sender’s email address from recipient variable into the mail.to transport header, using another Add New Transport Header processing element:

    Transport Header Name

    mail.to

    Use Variable

    On

    Value

    recipient

    Header Variable Type

    String

  7. Finally, add a Mail Egress Connector to send the response back to the sender (assuming a Gmail account):

    Basic

    Protocol

    smtp

    Host

    smtp.gmail.com

    Port

    25

    User

    your-email@gmail.com

    Password

    your-password

    Other Settings

    mail-props

The final flow would resemble the following:

mail ws

Testing

  • A multiplication service that accepts JSON payloads in the format {"v1":first_number,"v2":second_number} is available in the Jetty Server plugin shipped with Ultra Studio. Click View → Tool Windows → Jetty Server and click the green start button to start a Jetty server at port 9000. (If you have run a server instance previously, it will be listed in the Jetty Servers list, in which case you should start the corresponding (port 9000) server from the list.)

  • Create a new UltraESB Server runtime configuration, and start the project.

  • Send an email to your-email@gmail.com containing a multiplication request in the message body, such as the following: {"v1":7,"v2":6}

  • A response email will be delivered to the account from which you sent the multiplication request, resembling: {"result":42,"v1":7,"v2":6}

Property Configuration

When running the sample in the UltraESB-X distribution, you need to override the following properties in-order for the sample to work.

Refer to Managing Project Properties documentation on how to override properties.

MailIngressConnector.email-listener.user

Email account of the user who accepts input data

MailIngressConnector.email-listener.password

Email password of the user who accepts input data

MailEgressConnector.email-sender.user

Email account of the user who sends back computed data

MailEgressConnector.email-sender.password

Email password of the user who sends back computed data

Depnding on the email provider which hosts the emails that you have mentioned above, you may have to enable insecure application access in account settings
In this topic
In this topic
Contact Us