Send secure emails via Email Connector or SMTP configuration
Sending emails securely from your integration flows can be a key requirement in many enterprise use cases whether it’s notifications, alerts, or data delivery. In this blog, you’ll learn how to send emails from WSO2 Micro Integrator 4.4.0 by integrating email functionality using two approaches: the Email Connector and the Transport Mail feature.- The Email Connector: A ready-to-use feature from WSO2 that integrates with popular email providers like Gmail, Outlook, and SMTP servers.- The Transport Mail Feature: A built-in capability that enables email communication using Axis2 configurations.
We’ll walk you through how to integrate email into your workflows with proper SMTP configuration, ensuring a secure email setup that supports modern standards. Whether you want to send email securely from your integration layer or build fully automated processes that include email integration, this guide has you covered.
By the end of this tutorial, you’ll understand the configuration steps, deployment methods, and best practices needed to send secure emails using both flexible and native options available in WSO2 Micro Integrator
Prerequisites
- VS Code with WSO2 MI extension
- JDK
- WSO2 Micro Integrator (MI) Runtime
If these are not installed on your local machine, the Micro Integrator for VS Code extension will automatically prompt you to download and configure them during the project creation step, depending on the project runtime version.
For more details on using WSO2 MI extension in Visual Studio Code, please refer to the link.
1. Sending Emails Using the Email Connector
The Email Connector is a pre-built connector available in WSO2’s Connector Store. It abstracts the complexities of setting up the transport and configuration, making it easy to integrate email functionalities into your integration flows.
Configuration
- First, we need to create a local entry to initialize the Email Connector with the necessary parameters for connecting to Outlook / Gmail using SMTP. Based on your requirement, you will need to adjust the host value in the below configuration.
The key “EMAIL_CONNECTION” allows this entry to be accessed across different integration components.
connectionType : Specifies SMTPS (Secure SMTP) for encrypted email transmission.
host : Defines the SMTP server, in this case, Office 365 (smtp.office365.com).
port : Uses port 587, which is the standard for SMTP with TLS.
username : Provide the username of the SMTP account.
password : Provide the password of the SMTP account.
requireTLS : If true, an appropriate trust store must be configured so that the client will trust the server’s certificate and also ensure that the connection uses TLS encryption for secure communication.
enableOAuth2 : false means OAuth2 authentication is not enabled (only username/password-based login is used)
Please note :
- If your password contains special characters (example: <, >, &), you will run into errors when running the server. To avoid errors, update the password parameter to escape special characters.
- Make sure to enable “Less secure app access” in your Gmail account or use an App Password if 2-Step Verification is enabled. This setup is necessary for SMTP to work with Gmail.
- It’s recommended to store credentials securely using environment variables or a secure vault instead of hardcoding them.
2. Next, we create an API that uses the initialized Email Connector to send an email. This API listens at /sendEmail/send and expects a POST request with a JSON payload containing details such as the recipient’s email, subject, and content or the email body from the request.
Figure shows the overall project structure in VS code
Build and Deploy
Use one of the following two options to build and run the project:
Option 1 :
Click on the Command Palette on the top of VS Code.
Type > to show the available commands.
Select MI: Build and Run.
Option 2 :
Click the Build and Run icon located on the top right corner of the VS Code.
Testing and validation
You can test the service using Postman or Runtime services provided by VS Code. Test it by sending an HTTP request to the API endpoint. The email should be delivered to the specified recipient.
2. Sending Emails Using Transport Mail Feature
Transport Mail in WSO2 MI allows you to send emails directly without requiring additional connectors. This method uses the underlying Apache Axis2 transport layer.
Configuration
- Configuring Email Settings in deployment.toml
Instead of hardcoding email credentials in the integration sequence, we store them in deployment.toml for better security and manageability. Navigate to the MI_HOME/conf/ directory and edit the deployment.toml file to include the following email configurations:
Please note : If your password contains special characters (example: <, >, &), you will run into errors when running the server. To avoid errors, update the password parameter as follows:
parameter.password = “<![CDATA[${password}]]>”
2. Create an API to Send Emails:
In this code, I extract the recipient email address and subject from the JSON request body to dynamically construct the mailto URL. The email body is currently set to a static message: “Hello, this is a test email sent using SMTP transport!”. If a dynamic message is required, this section should be modified to extract the content from the JSON request. Additionally, the OUT_ONLY property ensures that the request operates in a fire-and-forget manner no response is expected from the SMTP server.
Build and Deploy
Build and deploy the project using any of the methods stated earlier.
Testing and validation
You can test the service using Postman or Runtime services provided by VS Code. Test it by sending an HTTP request to the API endpoint. The email should be delivered to the specified recipient.
Conclusion
Now that you’ve explored both ways to integrate email functionality into your WSO2 Micro Integrator project via the Email Connector and the Transport Mail feature you can decide which method fits your use case.
The Email Connector offers a flexible and straightforward way to send emails securely, especially if you prefer a connector-based, configuration-friendly solution. On the other hand, the Transport Mail feature gives you more control through custom configuration, ideal for scenarios requiring specific handling or integration into existing flows.
Whichever method you choose, make sure your SMTP configuration is correct and credentials are handled securely to ensure a secure email setup.
As a next step, try adjusting these examples to match your own environment and test the email functionality in a real integration flow. With both options, you now have the tools to confidently add email integration to your projects.
Please contact our experts if you have any further questions regarding this blog.