Learn To Send Mail In MuleSoft
Prerequisites:
- We need to have an active mail account. For this scenario, we are using Gmail. In Gmail, we need to set up the application password for further use in the connector to connect with Gmail.
- To do so we need to follow the following steps:
- We need to log in to the respective Gmail ID with which we want to establish the connection.
- There we need to click on the Manage your Google account.
- We will be redirected to the account page and there we need to go for the Security.
- On the security page we need to click on the 2-Step verification to create an app password.
- In the App password section we need to create an app by providing the app name to get the password that will be used in the SEND connector.
- Once we create the app we will receive a 16-digit password on the screen. We need to copy it and save it for further use.
- Now let’s configure our Mule application to use the SEND connector to send Emails.
Step 1:
- Open the Anypoint Studio create a Mule project and provide a name for the project.
Step 2:
- Now drag and drop a Flow and add an HTTP listener to it to create a source listener to get the mule event for the application.
Step 3:
- Now we need to configure the HTTP listener by providing the connector configuration and path.
- In connector configuration we need to provide the below details.
- Protocol: HTTP or HTTPS
- Host: All Interfaces[0.0.0.0](default)
- Port:8081
- Rest we can leave as default.
- Then hit OK.
- For path we can define any resource path following “/”.(Example: /Mail)
Step 4:
- Now drag and drop a Set payload component to create a payload we want to send as mail using the SEND connector.
Step 5:
- First we need to add an Email module to our mule palette. To do so we can search in the exchange about the Email module to add it to our anypoint studio. Once added we can see in the mule palette.
- Once we got the Email module we can see all the connectors to perform the required operation.
- We are using the SEND connector for the configuration for this particular scenario. We need to drag it to the mule flow.
- In the SEND connector configuration section we need to hit the plus button.
- As we are using Gmail we can use the SMTP connection option as Gmail is hosted over the SMTP server.
- We need to provide the values to the host, port, username, and password as follows:
- Host: smtp.gmail.com (Host of the Gmail id)
- Port: 587 (Dedicated port for SMTP)
- Username: The respective username of the mail ID (The Gmail ID)
- Password: The 16-digit password without the spaces
- In the advanced tab we need to provide the key-value pairs to have a secure transaction by the SEND connector.
- Key: mail.smtp.starttls.enable
- Value: true
- Key: mail.smtp.ssl.trust
- Value: smtp.gmail.com
- Hit on ok and the connector configuration is done.
Step 6:
- Then we need to provide the details below to complete the setup for the SEND connector.
- From Address: The mail id from where we want to send the message. (Same as username in connection)
- To Address: The mail id where we want to send the mail. (Edit inline and provide the email ID)
- Subject: The subject of the mail.
- Body :
- Content: The body of the mail. (e.g – payload)
- Content-Type: The type of the body. (e.g – text\plain)
- Encoding: UTF-8
- Content Transfer-Encoding: Base64
- In the attachment section we can provide the value if we want to send any attachment to the mail.
Step 7:
- Now hit on Save and Run the project.
- In the console we can see the “Status” as “Deployed” to ensure our app is deployed and ready to be tested.
- Once the Application is up and running we can Test it by triggering the Mule app from Postman.
Step 8:
- To trigger the Mule app we need to create a Request and we can hit the URL(http://localhost:8081/Mail) from Postman.
- Now we can see in the console that the mule app got executed and we can check the mail for the message.