Skip to content

krugerable/KP.GmailClient

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Build status

KP.GmailClient

This is an alternative client for the auto generated Google.Apis.Gmail.v1 Client Library.

  • It's easy to use
  • Has added extension methods to make common tasks even more easier
  • Supports Async
  • Did I mention easy?

Prerequisites

  1. Create a new project in the Google Cloud Platform -> https://console.cloud.google.com/home/dashboard
  2. Enable the Gmail API.
  3. Create a service account for the project -> https://console.cloud.google.com/iam-admin/serviceaccounts/
  4. Create and download a new key as JSON file.
  5. (only for G Suite users) Go to the G Suite Admin console and select the scopes, more on that here https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority

Setup

// Either use from config
const GmailScopes scopes = GmailScopes.Readonly | GmailScopes.Compose;
string privateKey = SettingsManager.GetPrivateKey();
string tokenUri = SettingsManager.GetTokenUri();
string clientEmail = SettingsManager.GetClientEmail();
string emailAddress = SettingsManager.GetEmailAddress();
ServiceAccountCredential accountCredential = new ServiceAccountCredential
{
    PrivateKey = privateKey,
    TokenUri = tokenUri,
    ClientEmail = clientEmail
};
var client = new GmailClient(accountCredential, emailAddress, scopes);

// Or use from downloaded JSON file directly
const string path = "C:\\Users\\Me\\Documents\\Gmail-Project.json";
var initializer = GmailClientInitializer.Initialize(path, scopes);
client = new GmailClient(initializer, emailAddress);

Usage examples

// Send a plain text email
Message sentMessage = await client.Messages.SendAsync(emailAddress, "The subject", "Plain text body");

// Send a HTML email
sentMessage = await client.Messages.SendAsync(emailAddress, "The subject", "<h1>HTML body</h1>", isBodyHtml: true);

// Get the users profile
Profile profile = await client.GetProfileAsync();

// Get inbox messages
IList<Message> messages = await client.Messages.ListAsync();

// Get starred messages
IList<Message> starredMessages = await client.Messages.ListByLabelAsync(Label.Starred);

// List all labels
IList<Label> labels = await client.Labels.ListAsync();

// List all drafts
IList<Draft> drafts = await client.Drafts.ListAsync();

Releases

No releases published

Packages

No packages published

Languages

  • C# 99.4%
  • PowerShell 0.6%