Ballerina connector for Microsoft Outlook mail provides access to the Microsoft Outlook mail service in Microsoft Graph v1.0 via the Ballerina language. It provides the capability to perform more useful functionalities provided in Microsoft outlook mail such as sending messages, listing messages, creating drafts, mail folders, deleting messages, updating messages, etc.

This module supports Microsoft Graph (Mail) API v1.0.


Before using this connector in your Ballerina application, complete the following:

  1. Follow this link and obtain the client ID, client secret, and refresh token.


To use the Outlook mail connector in your Ballerina application, update the .bal file as follows:

Step 1 - Import connector

Import the ballerinax/microsoft.outlook.mail module into the Ballerina project.

import ballerinax/microsoft.outlook.mail;

Step 2: - Create a new connector instance

You can now make the connection configuration using the OAuth2 refresh token grant config.

mail:ConnectionConfig configuration = {
    auth: {
        refreshUrl: <REFRESH_URL>,
        refreshToken : <REFRESH_TOKEN>,
        clientId : <CLIENT_ID>,
        clientSecret : <CLIENT_SECRET>

mail:Client outlookClient = check new(configuration);

Step 3: Invoke connector operation

  1. Send a new message using the sendMessage remote operation
public function main() returns error? {
    mail:MessageContent messageContent = {
        message: {
            subject: "Ballerina Test Email",
            importance: "Low",
            body: {
                "contentType": "HTML",
                "content": "This is sent by sendMessage operation <b>Test</b>!"
            toRecipients: [
                    emailAddress: {
                        address: "<email address>",
                        name: "<name>"
        saveToSentItems: true
    http:Response response = check oneDriveClient->sendMessage(messageContent);
  1. Use bal run command to compile and run the Ballerina program.

You can find more samples here

Other versions

See more...