Skip to content

Connect to SaaS or B2B Systems

In the previous tutorial, you learned how to route and transform messages, deploy, and test integrations in WSO2 Micro Integrator (MI). In this tutorial, you’ll learn how to create a loan review email notification flow that sends an email based on the client's loan status.

What you'll build

Let's consider a scenario where a client sends a loan request to the Bank API deployed in WSO2 Micro Integrator. Upon receiving the request, the API sends an email notification to the client indicating that the loan request has been received and is under review. This is done using a SaaS-based email service.

In this example, you will use Gmail's SMTP service as the email provider.

Create New Project

What you'll learn

  • How to integrate and send emails using the Email connector.

Prerequisites

  1. You need Visual Studio Code (VS Code) with the Micro Integrator for VS Code extension installed.

    Info

    See the Install Micro Integrator for VS Code documentation to learn how to install Micro Integrator for VS Code.

  2. You must have completed the Route and Transform messages tutorial under Build your first Integration before proceeding. Start the Route and Transform messages tutorial if you haven’t completed it yet.

Now, it's time to design the email notification flow. Follow the steps below to create the email notification integration.

Step 1 - Create a new API resource

To develop the above scenario, let's get started with creating a new API resource in the Bank API.

  1. Click on the Service Designer (inline expression editor) icon of the Bank API in the Micro Integrator Project Explorer to open the Service Designer.

    Create New Project

  2. In the Service Designer, click the + Resource button to add a new API resource.

    Create New Project

  3. In the Add API Resource pane, set /loan-review as the Resource Path and select the POST method.

    Create New Project

  4. Finally, click Create to add the new API resource.

Step 2 - Design the integration

  1. Open the Resource View of the newly created API resource by clicking the POST /loan-review resource under Available resources in the Service Designer.

    Create New Project

  2. After opening the Resource View, click on the Start node on the canvas to set an input payload for the integration flow.

    Note

    Setting an input payload for the integration flow is not mandatory. However, it is recommended, as it will be used to enable expression suggestions, which you will explore in later steps of this tutorial.

    Create New Project

  3. Click Add Request, provide the following JSON payload, then click Add. Finally, click Save to complete the input payload setup.

    {
        "customerId": "C567",
        "name": "Jane Smith",
        "email": "[email protected]",
        "amount": 25000
    }
    

    Create New Project

  4. Click on the + icon on the canvas to open the Mediator Palette.

    Create New Project

  5. Search for email in the Mediator Palette, then click the download (inline expression editor) icon to add the Email connector to the project. In the confirmation pane, select Yes to add the required dependencies.

    What is a connector?

    • Connectors in WSO2 Micro Integrator (MI) enable seamless integration with external systems, cloud platforms, and messaging services without the need for custom implementations. They provide a standardized way to send, receive, and process data from third-party applications like Salesforce, Kafka, and AWS services. To explore connectors in detail, see the Connector documentation.
    • In VS Code, you can view all available connectors by clicking Add Module under the Mediators tab in the Mediator Palette.

    Create New Project

  6. Once the connector is downloaded, select the Send operation from the Mediator Palette.

    Create New Project

  7. Click + Add new connection to create a new connection.

    Create New Project

  8. Select SMTPS and fill in the following details to create a connection to Gmail's SMTP service. Finally, click Add in the Add New Connection form to create the connection.

    Tip

    If two-factor authentication is enabled, you need to obtain an app password by following the instructions here.

    Property Value
    Connection Name GmailConnection
    Host smtp.gmail.com
    Port 465
    Username Your email address
    Password Your email password or app password

    Note

    It is recommended to use secure vault to store credentials. For more information, refer to Encrypting Secrets.

    Create New Project

  9. After creating the connection, enter your email address in the From field and set Loan Application Received as the Subject in the Add Send form.

    Create New Project

    In the next step, you will extract the recipient's email address from the incoming payload and assign it to the To field using an expression.

  10. Click on the expression (inline expression editor) icon to enable expression mode for the To field.

    Create New Project

  11. Once expressions are enabled, click on the expression editor (inline expression editor) icon to open the editor.

  12. Select Payloademail to extract the recipient's email address from the payload.

    Add inline expression

    In the next step, you will use a template with inline expressions to extract data from the incoming payload and construct the email body.

    Tip

    You can use the following pre-filled template in the Content field to skip the inline expression insertion steps and continue from Step 16.

    Dear ${payload.name},
    
    We have received your loan application for $ ${payload.amount}. Our team will review your application and contact you shortly.
    
    Thank you,
    O2 Bank
    
  13. In the Content field, enter the following template.

    Dear ,
    
    We have received your loan application for $ . Our team will review your application and contact you shortly.
    
    Thank you,
    O2 Bank
    
  14. In the template, place your cursor in the appropriate location (next to Dear), click on the inline expression editor (inline expression editor) icon, then select Payloadname, and click Add to insert the inline expression into the template.

  15. Repeat the same steps to insert the loan amount after the $ symbol in the template.

    Add inline expression

  16. Ensure that Overwrite Message Body is checked, then click Submit to add the email operation to the integration flow.

    Create New Project

  17. Click on the + icon located just after the Send operation to open the Mediator Palette to add a Respond Mediator to respond to the client request.

    Create New Project

  18. Select Respond mediator under Mediators, and click Add to insert it to the integration flow.

You have successfully updated the integration flow to send an email with the loan request status. For reference, you can review the configured API and Email connection.

Bank API

Info

You can view the source view by clicking on the Show Source (</>) icon located in the top right corner of the VS Code.

ai datamapping api

<?xml version="1.0" encoding="UTF-8"?>
<api context="/bankapi" name="BankAPI" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="GET" uri-template="/">
        <inSequence>
            <payloadFactory media-type="json" template-type="default">
                <format>{
                    "greetings":"Welcome to O2 Bank !!"
                    }
                </format>
            </payloadFactory>
            <respond/>
        </inSequence>
        <faultSequence>
        </faultSequence>
    </resource>
    <resource methods="POST" uri-template="/deposit">
        <inSequence>
            <http.post configKey="CurrencyConverter">
                <relativePath>/currency/rate</relativePath>
                <headers>[]</headers>
                <requestBodyType>JSON</requestBodyType>
                <requestBodyJson>{
                    &quot;fromCurrency&quot;: &quot; ${payload.currency} &quot;,
                    &quot;toCurrency&quot;: &quot;USD&quot;
                    }
                </requestBodyJson>
                <forceScAccepted>false</forceScAccepted>
                <disableChunking>false</disableChunking>
                <forceHttp10>false</forceHttp10>
                <noKeepAlive>false</noKeepAlive>
                <forcePostPutNobody>false</forcePostPutNobody>
                <responseVariable>http_post_1</responseVariable>
                <overwriteBody>false</overwriteBody>
            </http.post>
            <filter xpath="${vars.http_post_1.attributes.statusCode == 200}">
                <then>
                    <payloadFactory media-type="json" template-type="default">
                        <format>{ "status": "successful", "amountDeposited": ${payload.amount * vars.http_post_1.payload.rate} }</format>
                    </payloadFactory>
                </then>
                <else>
                    <payloadFactory media-type="json" template-type="default">
                        <format>{
                            "status": "unsuccessful",
                            "error": "${vars.http_post_1.payload.message}"
                            }
                        </format>
                    </payloadFactory>
                </else>
            </filter>
            <respond/>
        </inSequence>
        <faultSequence>
        </faultSequence>
    </resource>
    <resource methods="POST" uri-template="/loan-review">
        <inSequence>
            <email.send configKey="GmailConnection">
                <from>ENTER_YOUR_EMAIL_HERE</from>
                <personalName></personalName>
                <to>{${payload.email}}</to>
                <cc></cc>
                <bcc></bcc>
                <replyTo></replyTo>
                <subject>Loan Application Received</subject>
                <content>Dear ${payload.name},
                                We have received your loan application for $ ${payload.amount}. Our team will review your application and contact you shortly.
                                Thank you,
                                O2 Bank</content>
                <contentType>text/html</contentType>
                <encoding>UTF-8</encoding>
                <attachments></attachments>
                <inlineImages>[]</inlineImages>
                <contentTransferEncoding>Base64</contentTransferEncoding>
                <responseVariable>email_send_1</responseVariable>
                <overwriteBody>true</overwriteBody>
            </email.send>
            <respond />
        </inSequence>
        <faultSequence>
        </faultSequence>
    </resource>
</api>
Email Connection

Info

You can view the source view by clicking on the Show Source (</>) icon located in the top right corner of the VS Code.

http connection config

<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="GmailConnection" xmlns="http://ws.apache.org/ns/synapse">
    <email.init>
        <connectionType>SMTPS</connectionType>
        <host>smtp.gmail.com</host>
        <port>465</port>
        <username>REPLACE</username>
        <password>REPLACE</password>
        <name>GmailConnection</name>
    </email.init>
</localEntry>

Step 3 - Run the integration

Now that you have updated the integration, it's time to deploy the integration to the Micro Integrator server runtime.

Click the Build and Run icon located in the top right corner of VS Code.

Create New Project

Step 4 - Test the integration service

  1. Once the Runtime Services interface is open, select the BankAPI, and click the Try It button.

    Create New Project

  2. Select the /loan-review resource and click Try it Out to test the API.

    Create New Project

  3. Provide a JSON payload and click the Execute button to invoke the API. You may use the following sample payload to test the API. Make sure to update the name and email fields with the actual recipient's information.

    {
        "customerId": "C567",
        "name": "Jane Smith",
        "email": "[email protected]",
        "amount": 45000
    }
    

    Create New Project

  4. Check the response received from the server, and verify that the loan request status email has been successfully delivered by checking the recipient’s inbox.

Congratulations! You have now learned how to integrate with an email SaaS provider using connectors to send emails as part of your integration flow.

What's Next?

You have now completed this tutorial series, where you learned how to build a complete integration flow step by step. Throughout this journey, you have explored how to create Integration APIs, route and transform messages, connect to external SaaS and B2B systems.

Now, you can explore advanced integration scenarios, applying what you’ve learned to real-world use cases and expanding your expertise in WSO2 Micro Integrator.

Try more tutorials and examples.