Skip to content

Email Connector Example

Email Connector can be used to perform operations using protocols SMTP, IMAP and POP3.

What you'll build

This example explains how to use Email Connector to send an email and retrieve the same email from Gmail. The user sends a payload with the recipients and content of the email. Then, by invoking another API resource, the content of the sent email will be retrieved.

The example consists of an API named as EmailConnector API with two resources send and retrieve.

  • /send: The user sends the request payload which includes the recipients, content and attachments of the email. This request is sent to WSO2 EI by invoking the EmailConnector API. It will send the email to the relevant recipients.

    Send function

  • /retrieve: The user sends the request payload, containing the filter to search the received email. This request is sent to WSO2 EI where the EmailConnector API resides. Once the API is invoked, it returns the filtered emails.

    Retrieve function

If you do not want to configure this yourself, you can simply get the project and run it.

Configure the connector in WSO2 Integration Studio

Follow these steps to set up the Integration Project and the Connector Exporter Project.

  1. Open WSO2 Integration Studio and create an Integration Project. Creating a new Integration Project

  2. Right click on the project that you created and click on Add or Remove Connector -> Add Connector. You will get directed to the WSO2 Connector Store.

  3. Search for the specific connector required for your integration scenario and download it to the workspace.
    Search Connector in the Connector Store

  4. Click Finish, and your Integration Project is ready. The downloaded connector is displayed on the side palette with its operations.

  5. You can drag and drop the operations to the design canvas and build your integration logic. Drag connector operations

Creating the Integration Logic

  1. Right click on the created Integration Project and select, -> New -> Rest API to create the REST API. Adding a Rest API

  2. Provide the API name as Email Connector and the API context as /emailconnector.

  3. First we will create the /send resource. This API resource will retrieve information from the incoming HTTP post request such as recipients and content and construct the email and send to the mentioned recipients.
    Right click on the API Resource and go to Properties view. We use a URL template called /send as we have two API resources inside single API. The method will be Post. Adding the API resource.

  4. In this operation we are going to receive following inputs from the user.

    • from - Sender of the email.
    • to - Recipient of the email.
    • subject - Subject of the email.
    • content - Content to be included in the email.
    • contentType - Content Type of the email
  5. Drag and drop the 'send' operation of the Email Connector to the Design View as shown below. Adding the send operation.

  6. Create a connection from the properties window by clicking on the '+' icon as shown below. Adding the connection.

    In the pop up window, following parameters must be provided.

    • Connection Name - Unique name to identify the connection by.
    • Connection Type - Type of the connection which specifies the protocol to be used.
    • Host - Host name of the mail server.
    • Port - The port number of the mail server.
    • Username - Username used to connect with the mail server.
    • Password - Password to connect with the mail server.

    Following values can be provided to connect to Gmail.

    • Connection Name - smtpsconnection
    • Connection Type - SMTP Secured Connection
    • Host - smtp.gmail.com
    • Port - 465
    • Username - <your_email_address>
    • Password - <your_email_password>

      NOTE: If you have enabled 2-factor authentication, an app password should be obtained as instructed here.

    Connection parameters.

  7. After the connection is successfully created, select the created connection as 'Connection' from the drop down in the properties window. Selecting the connection.

  8. Next, provide the expressions as below to the following properties in the properties window to obtain respective values from the JSON request payload.

    • to - json-eval($.to)
    • from - json-eval($.from)
    • subject - json-eval($.subject)
    • content - json-eval($.content)
    • contentType - json-eval($.contentType)
  9. Drag and drop the Respond Mediator to respond the response from sending the email as shown below. Adding the respond mediator.

  10. Create the next API resource, which is /retrieve by dragging and dropping another API resource to the design view. This API resource will retrieve filters from the incoming HTTP post request from which to filter the email messages such as the subject, retrieve the emails, retrieve email body and respond back. This will be used to retrieve the email we just sent. This will also be a POST request. Adding new resource.

  11. Drag and drop the 'list' operation of the Email Connector to the Design View as shown below. Adding list operation.

  12. Next, we will create a IMAP connection to list emails similar to step 6. Following are the values to be provided when creating the connection.

    • Connection Name - imapsconnection
    • Connection Type - IMAP Secured Connection
    • Host - imap.gmail.com
    • Port - 993
    • Username - <your_email_address>
    • Password - <your_email_password>
  13. In this operation we are going to receive following inputs from the user.

    • subjectRegex - Subject Regex to filter the email from.

    Therefore, provide the expressions as below to the following properties in the properties window to obtain respective values from the JSON request payload.

    • Subject Regex: json-eval($.subjectRegex)
  14. We will next iterate the response received and obtain the email content of each email using the getEmailBody operation. In order to do this, drag and drop the Foreach Mediator as shown below and enter //emails/email as the Foreach Expression in the properties window. Adding foreach mediator.

  15. Inside the Foreach Mediator, drag and drop the getEmailBody operation as shown below and provide the //email/index/text() expression as the Email Index. Adding getEmailBody operation.

    NOTE: Further, you can use getAttachment operation to retrieve attachment content if there are any. Refer Reference Documentation to learn more.

  16. Next, we will use a Payload Factory Mediator, to add the email content to the same response we received from list operation and configure the Payload mediator as shown below. Adding payload facotry mediator.

    Enter following as the payload:

    <email>
        <emailID>$1</emailID>
        <to>$2</to>
        <from>$3</from>
        <subject>$4</subject>
        <textContent>$5</textContent>
    </email>

    Here, you may observe that we are obtaining TEXT_CONTENT property which is being set when getEmailBody is invoked to retrieve the email content. You can find the list of similar properties set in this operation here.

  17. Drag and drop a Property Mediator and set the Property name as 'messageType' and the value as application/json. This is added so that the response will be in json. Adding property mediator.

  18. Finally, drag and drop the Respond Mediator after the 'foreach' mediator to respond the response of retrieved emails. Adding property mediator.

  19. You can find the complete API XML configuration below. You can go to the source view and copy paste the following config.

    <?xml version="1.0" encoding="UTF-8"?>
    <api context="/emailconnector" name="EmailConnector" xmlns="http://ws.apache.org/ns/synapse">
        <resource methods="POST" uri-template="/send">
            <inSequence>
                <email.send configKey="smtpsconnection">
                    <from>{json-eval($.from)}</from>
                    <to>{json-eval($.to)}</to>
                    <subject>{json-eval($.subject)}</subject>
                    <content>{json-eval($.content)}</content>
                    <contentType>{json-eval($.contentType)}</contentType>
                </email.send>
                <respond/>
            </inSequence>
            <outSequence/>
            <faultSequence/>
        </resource>
        <resource methods="POST" uri-template="/retrieve">
            <inSequence>
                <email.list configKey="imapsconnection">
                    <subjectRegex>{json-eval($.subjectRegex)}</subjectRegex>
                </email.list>
                <foreach expression="//emails/email">
                    <sequence>
                        <email.getEmailBody>
                            <emailIndex>{//email/index/text()}</emailIndex>
                        </email.getEmailBody>
                        <payloadFactory media-type="xml">
                            <format>
                                <email xmlns="">
                                    <emailID>$1</emailID>
                                    <to>$2</to>
                                    <from>$3</from>
                                    <subject>$4</subject>
                                    <textContent>$5</textContent>
                                </email>
                            </format>
                            <args>
                                <arg evaluator="xml" expression="//email/emailID"/>
                                <arg evaluator="xml" expression="//email/to"/>
                                <arg evaluator="xml" expression="//email/from"/>
                                <arg evaluator="xml" expression="//email/subject"/>
                                <arg evaluator="xml" expression="$ctx:TEXT_CONTENT"/>
                            </args>
                        </payloadFactory>
                    </sequence>
                </foreach>
                <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
                <respond/>
            </inSequence>
            <outSequence/>
            <faultSequence/e>
        </resource>
    </api>

Exporting Integration Logic as a CApp

CApp (Carbon Application) is the deployable artifact on the Enterprise Integrator runtime. Let us see how we can export integration logic we developed into a CApp along with the connector.

Creating Connector Exporter Project

In order to bundle Connector into a CApp a Connector Exporter Project is needed.

  1. Navigate to File -> New -> Other -> WSO2 -> Extensions -> Project Types -> Connector Exporter Project.
    Add Connector Exporter Project

  2. Enter a name for the Connector Exporter Project.

  3. In the next screen select, Specify the parent from workspace and select the specific Integration Project you created from the dropdown. Naming Connector Exporter Project

  4. Now you need to add the Connector to Connector Exporter Project that you just created. Right click on the Connector Exporter Project and select, New -> Add Remove Connectors -> Add Connector -> Add from Workspace -> Connector

  5. Once you are directed to the workspace, it displays all the connectors that exist in the workspace. You can select the relevant connector and click Ok. Selecting Connector from Workspace

Creating a Composite Application Project

To export the Solution Project as a CApp, a Composite Application Project needs to be created. Usually, when a solution project is created, this project is automatically created by Integration Studio. If not, you can specifically create it by navigating to File -> New -> Other -> WSO2 -> Distribution -> Composite Application Project.

Exporting the Composite Application Project

  1. Right click on Composite Application Project and click on Export Composite Application Project.
    Export as a Carbon Application

  2. Select an Export Destination where you want to save the .car file.

  3. In the next Create a deployable CAR file screen, select both the created Integration Project and the Connector Exporter Project to save and click Finish. The CApp will get created at the specified location provided at the previous step. Create a deployable CAR file

Get the project

You can download the ZIP file and extract the contents to get the project code.

Download ZIP

Deployment

Follow these steps to deploy the exported CApp in the Enterprise Integrator Runtime.

Deploying on WSO2 Enterprise Integrator 7

You can copy the composite application to the /repository/deployment/server/carbonapps folder and start the server. Micro Integrator will be started and the composite application will be deployed. You can further refer to the application deployed through the CLI tool. You can download the CLI tool from here from the Other Resources section. Make sure you first export the PATH as below.

  $ export PATH=/path/to/mi/cli/directory/bin:$PATH
  1. Log in to Micro Integrator using the following command.

    ./mi remote login
  2. Provide default credentials admin for both username and password.

  3. In order to view the APIs deployed, execute the following command.

    ./mi api show
Click here for instructions on deploying on WSO2 Enterprise Integrator 6
  1. You can copy the composite application to the /repository/deployment/server/carbonapps folder and start the server.

  2. WSO2 EI server starts and you can login to the Management Console https://localhost:9443/carbon/ URL. Provide login credentials. The default credentials will be admin/admin.

  3. You can see that the API is deployed under the API section.

Testing

Email Send Operation

  1. Create a file called data.json with the following payload. We will send the email to ourself so that we can retrieve it later.
    {
        "from": "<your-email>@gmail.com",
        "to": "<your-email>@gmail.com",
        "subject": "Sample email",
        "content": "This is the body of the sample email",
        "contentType":"text/plain"
    }
  2. Invoke the API as shown below using the curl command. Curl Application can be downloaded from [here] (https://curl.haxx.se/download.html).
    curl -H "Content-Type: application/json" --request POST --data @body.json http://localhost:8290/emailconnector/send
    Expected Response: You should get a 'Success' response as below and you will receive the email.
    {
        "result": {
            "success": true
        }
    }

Email List Operation

  1. Create a file called data.json with the following payload.
    {
        "subjectRegex":"Sample email"
    }
  2. Invoke the API as shown below using the curl command. Curl Application can be downloaded from [here] (https://curl.haxx.se/download.html).
    curl -H "Content-Type: application/json" --request POST --data @body.json http://localhost:8290/emailconnector/retrieve
    Expected Response: You should get a response like below.
    {
        "emails": {
            "email": [
                {
                    "index": 0,
                    "emailID": "<1623446944.0.152334336343@localhost>",
                    "to": "<your-email>@gmail.com",
                    "from": "<your-email>@gmail.com",
                    "subject": "Sample email",
                    "textContent": "This is the body of the sample email"
                }
            ]
        }
    }

What's Next

Top