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?
- Create a new project in the Google Cloud Platform -> https://console.cloud.google.com/home/dashboard
- Enable the Gmail API.
- Create a service account for the project -> https://console.cloud.google.com/iam-admin/serviceaccounts/
- Create and download a new key as JSON file.
- (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
// 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);
// 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();