Coder Social home page Coder Social logo

package-gmail's Introduction

Gmail Connector

Connects to Gmail from Ballerina.

The Gmail connector provides an optimized way to use the Gmail REST API from your Ballerina programs. It handles OAuth 2.0 and provides auto completion and type conversions.

Compatibility

Ballerina Language Version Gmail API Version
0.970.0-beta15 v1

Getting started

  1. To download and install Ballerina, see the Getting Started guide.

  2. Obtain your OAuth 2.0 credentials. To access a Gmail endpoint, you will need to provide the Client ID, Client Secret, and Refresh Token, or just the Access Token. For more information, see the Gmail OAuth 2.0 documentation.

  3. Create a new Ballerina project by executing the following command.

    $ ballerina init
  4. Import the Gmail package to your Ballerina program as follows.

    import ballerina/io;
    import wso2/gmail;
    
    //User credentials to access Gmail API
    string accessToken = "<access_token>";
    string clientId = "<client_id>";
    string clientSecret = "<client_secret>";
    string refreshToken = "<refresh_token>";
    //The user's email address. The special value "me" can be used to indicate the authenticated user.
    string userId = "me";
    
    function main(string... args) {
    
       endpoint gmail:Client gmailEP {
           clientConfig:{
               auth:{
                   accessToken:accessToken,
                   clientId:clientId,
                   clientSecret:clientSecret,
                   refreshToken:refreshToken
               }
           }
       };
    
       //Set headers, message body and etc to MessageRequest.
       gmail:MessageRequest messageRequest;
       messageRequest.recipient = "[email protected]";
       messageRequest.sender = "[email protected]";
       messageRequest.cc = "[email protected]";
       messageRequest.subject = "Email-Subject";
       messageRequest.messageBody = "Email Message Body Text";
       //Set the content type of the mail as TEXT_PLAIN or TEXT_HTML.
       messageRequest.contentType = gmail:TEXT_PLAIN;
    
       //Call the Gmail endpoint function sendMessage().
       var sendMessageResponse = gmailEP -> sendMessage(userId, messageRequest);
       match sendMessageResponse {
           (string, string) sendStatus => {
               //For a successful message request, returns message and thread id.
               string messageId;
               string threadId;
               (messageId, threadId) = sendStatus;
               io:println("Sent Message Id : " + messageId);
               io:println("Sent Thread Id : " + threadId);
           }
           gmail:GmailError e => io:println(e); //For unsuccessful attempts, returns Gmail Error.
       }
    }

package-gmail's People

Contributors

dushaniw avatar keerthu avatar ldclakmal avatar maheshika avatar muthulee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.