Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automate Gmail with GmailHelper
#1
Hey guys,

With Gmail-API-Helper,you can access a Gmail a account. You’ll be able to filter, read, open mails.
That can be helpfull to automate collection and treatement of user’s data.
You will need first Configurate the Google API side, but it is well documented.

After the configuration you should end up with a json file that we can call credential.json.

If not already done you’ll have to install Nugets GmailHelper, Google.Apis.Gmail.v1 and Google.Apis.Auth.

Here is a basic script to use it:
 
Code:
Copy      Help
/*/ nuget GmailHelper\GmailHelper; nuget GmailHelper\Google.Apis.Gmail.v1; nuget GmailHelper\Google.Apis.Auth; /*/


using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;

public class GmailHelper
{
    static readonly string[] Scopes = { GmailService.Scope.MailGoogleCom };
    static readonly string ApplicationName = "Gmail API Helper";

    public static void Main(string[] args)
    {
        try
        {
            UserCredential credential;

            using (var stream = new FileStream("path/to/your/credentials.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = "token.json"; // path to the tokens
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("The user’s credentials have been saved in : " + credPath);
            }

            // Create the Gmail API service.
            var service = new GmailService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            // List the 10 first emails of the Incoming.
            var request = service.Users.Messages.List("me");
            request.MaxResults = 10;

            var response = request.Execute();
            Console.WriteLine("Emails in the Incoming box:");
            if (response.Messages != null && response.Messages.Count > 0)
            {
                foreach (var message in response.Messages)
                {
                    var messageRequest = service.Users.Messages.Get("me", message.Id);
                    var messageContent = messageRequest.Execute();
                    Console.WriteLine($"- Subject: {messageContent.Snippet}"); // Show a snipet of the subject
                }
            }
            else
            {
                Console.WriteLine("Email not found in the Incoming box.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error has occured : " + ex.Message);
        }

        Console.ReadKey();
    }
}


1 - You’ll have to replace the : "path/to/your/credentials.json" by the real path to your credentials.json.
2 - Authenticate the user and stocking the tokens in token.json file.
3 - This script will authenticate the user,"me" is the authenticated user.
4 - Create an instance of the Gmail API service
5 - List the 10 last messages messages: request.MaxResults = 10;
6 - Grap a snipet of each message to show their topics.


Forum Jump:


Users browsing this thread: 1 Guest(s)