Managed C++/CLI - Retrieve Email using Gmail/G Suite OAuth 2.0 + IMAP4 Protocol in Background Service (Service Account)

By default, you need to enable ” Allowing less secure apps” in Gmail/G Suite, then you can retrieve email with user/password IMAP4 authentication.

However Google will disable traditional user authentication in the future, switching to Google OAuth is strongly recommended now.


Before you can use the following sample codes, you should download the EAGetMail Installer and install it on your machine at first. Full sample projects are included in this installer.

Install from NuGet

You can also install the run-time assembly by NuGet. Run the following command in the NuGet Package Manager Console:

Install-Package EAGetMail


If you install it by NuGet, no sample projects are installed, only .NET assembly is installed.

Add Reference

To use EAGetMail POP3 & IMAP Component in your project, the first step is “Add reference of EAGetMail to your project”. Please create or open your project with Visual Studio, then go to menu -> Project -> Add Reference -> .NET -> Browse..., and select Installation path\Lib\[netversion]\EAGetMail.dll, click Open-> OK, the reference will be added to the project, you can start to use it to retrieve email and parse email in your project.

add reference in c#/ c++/cli

.NET Assembly

Because EAGetMail has separate builds for .Net Framework, please refer to the following table and choose the correct dll.

Separate builds of run-time assembly for .Net Framework 1.1, 2.0, 3.5, 4.0, 4.5, 4.61, .NET Core 2.1 and .Net Compact Framework 2.0, 3.5.

File .NET Framework Version
Lib\net11\EAGetMail.dll Built with .NET Framework 1.1
It requires .NET Framework 1.1, 2.0, 3.5 or later version.
Lib\net20\EAGetMail.dll Built with .NET Framework 2.0
It requires .NET Framework 2.0, 3.5 or later version.
Lib\net35\EAGetMail.dll Built with .NET Framework 3.5
It requires .NET Framework 3.5 or later version.
Lib\net40\EAGetMail.dll Built with .NET Framework 4.0
It requires .NET Framework 4.0 or later version.
Lib\net45\EAGetMail.dll Built with .NET Framework 4.5
It requires .NET Framework 4.5 or later version.
Lib\net461\EAGetMail.dll Built with .NET Framework 4.6.1
It requires .NET Framework 4.6.1 or later version.
Lib\netcoreapp2.1\EAGetMail.dll Built with .NET Core 2.1
It requires .NET Core 2.1 or later version.
Lib\net20-cf\EAGetMail.dll Built with .NET Compact Framework 2.0
It requires .NET Compact Framework 2.0, 3.5 or later version.
Lib\net35-cf\EAGetMail.dll Built with .NET Compact Framework 3.5
It requires .NET Compact Framework 3.5 or later version.

Google Service Account

Normal OAuth requires user input user/password in Web Browser. Obviously, it is not suitable for background service. In this case, you should use google service account to access G Suite email service without user interaction. Service account only works for G Suite user, it doesn’t work for personal Gmail account.

Create Project in Google Developers Console

To use “G Suite Service Account OAuth” in your application, you should create a project in Google Developers Console at first.


You can use any google user to create service account, it doesn’t require service account owner is a user in G Suite. But G Suite administrator must authorize service account in G Suite Admin Console to access user mailbox.

Create Service Account in Current Project

  • Click "Credentials" -> "Manage service accounts"

    manage service account in google developers console

    create service account in google developers console
  • Input a name for your service account, click "CREATE"

    create service account in google developers console step 1
  • In "Service account permissions", select "Project" -> "Owner" as role

    create service account in google developers console step 2
  • In "Grant users access to this service account", keep everything default and click "DONE"

    create service account in google developers console step 3

After service account is created, you should enable "Domain-wide delegation" and create service key pair to access G Suite user mailbox.

Enable “Domain-wide delegation” and Create Service Key

  • Go back to your service account, click "Edit" -> "SHOW DOMAIN-WIDE DELEGATION", check "Enable G Suite Domain-wide Delegation", input a name for product oauth consent, click "Save".

    Enable G Suite Domain-wide Delegation
  • Go back to your service account again, click "Create Key", you can select "p12" or "json" key type, both can work well, then you will get a file which contains private key, save the file to local disk.

    Now you have created service account with key pair successfully. You can use created private key in your codes to request "access token" impersonating a user in G Suite.

    create service key
  • To access user data in G Suite, you must get authorization from G Suite administrator. You should go to service accounts list, click "View Client ID" like this:

    google oauth client id
  • Then copy your “Client ID” and service account email address, forward it to G Suite administrator for authorization.

    google oauth serivce client id

Enable Gmail API

Enable Gmail API in "Library" -> Search "Gmail", then click "Gmail API" and enable it. If you use Gmail API protocol to send email, you should enable this API, if you use SMTP protocol, you don’t have to enable it.

enable Gmail API

Authorize Service Account by G Suite Administrator

To use service account to access user mailbox in G Suite, G Suite Administrator should authorize specified service account at first.


Important Notice: You can use any google user to create service account, it doesn’t require service account owner is a user in G Suite. But G Suite administrator must authorize service account in G Suite Admin Console to access user mailbox.

  • G Suite Administrator should open, go to Admin Console, click "Security";

    Authorize Service Account by G Suite Administrator
  • Click "Advanced settings" -> "Manage API client access";

    manage api client access by G Suite Administrator
  • Input service account "Client ID" in Client Name, and input ",email,profile" in One or More API Scopes, click "Authorize".

    If you use Gmail API protocol instead of SMTP protocol, input: ",email,profile".

    manage api client access by G Suite Administrator 1

After G Suite administrator authorized service account, you can use it to access any users mailbox in G Suite domain.


You should create your client id and client secret, do not use the client id from example codes in production environment, it is used for test purpose.

Managed C++/CLI - Retrieve Email using Gmail/G Suite OAuth 2.0 from IMAP4 Server with Service Account - Example

// You can install Google.Apis.Auth.OAuth2 by NuGet
// Install-Package Google.Apis.Auth
#include "stdafx.h"

using namespace System;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Text;
using namespace System::Threading;
using namespace System::Threading::Tasks;
using namespace System::Runtime::InteropServices;
using namespace System::Security::Cryptography::X509Certificates;
using namespace System::Globalization;

using namespace Google::Apis::Auth::OAuth2;
using namespace EAGetMail;

// Generate an unqiue email file name based on date time
static String ^ _generateFileName(int sequence)
    DateTime currentDateTime = DateTime::Now;
    return String::Format("{0}-{1:000}-{2:000}.eml",
        currentDateTime.ToString("yyyyMMddHHmmss", gcnew CultureInfo("en-US")),

static void RetrieveWithGSuiteUser()
        // service account email address
        String^ serviceAccount = "";

        // import service account key p12 certificate.
        auto certificate = gcnew X509Certificate2("D:\\MyData\\myoauth-77dec4d192ec.p12",
            "notasecret", X509KeyStorageFlags::Exportable);

        // G Suite user email address
        String^ gsuiteUser = "";

        auto serviceAccountCredentialInitializer = gcnew ServiceAccountCredential::Initializer(serviceAccount);

            serviceAccountCredentialInitializer->User = gsuiteUser;
            serviceAccountCredentialInitializer->Scopes = gcnew array<String^>{ "" };


        // if service account key is in json format, copy the private key from json file:
        // "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"
        // and import it like this:
        // String^ privateKey = "-----BEGIN PRIVATE KEY-----\nMIIEv...revdd\n-----END PRIVATE KEY-----\n";

        // serviceAccountCredentialInitializer->FromPrivateKey(privateKey);

        // request access token
        auto credential = gcnew ServiceAccountCredential(serviceAccountCredentialInitializer);
        if (!credential->RequestAccessTokenAsync(CancellationToken::None)->Result)
            throw gcnew InvalidOperationException("Access token failed.");

        // Create a folder named "inbox" under current directory
        // to save the email retrieved.
        String ^localInbox = String::Format("{0}\\inbox", Directory::GetCurrentDirectory());

        // If the folder is not existed, create it.
        if (!Directory::Exists(localInbox))

        // Gmail IMAP4 Server
        MailServer ^oServer = gcnew MailServer("",
            credential->Token->AccessToken, // use access token as password

        // Use IMAP OAUTH 2.0
        oServer->AuthType = ServerAuthType::AuthXOAUTH2;
        // Enable SSL/TLS connection
        oServer->SSLConnection = true;
        // Set IMAP4 SSL port
        oServer->Port = 993;

        Console::WriteLine("Connecting {0}", oServer->Server);

        MailClient ^oClient = gcnew MailClient("TryIt");

        // Get new email only, if you want to get all emails, please remove this line
        oClient->GetMailInfosParam->GetMailInfosOptions = GetMailInfosOptionType::NewOnly;

        array<MailInfo^> ^infos = oClient->GetMailInfos();
        Console::WriteLine("Total {0} email(s)\r\n", infos->Length);

        for (int i = 0; i < infos->Length; i++)
            MailInfo ^info = infos[i];
            Console::WriteLine("Index: {0}; Size: {1}; UIDL: {2}",
                info->Index, info->Size, info->UIDL);

            // Generate an unqiue email file name based on date time
            String^ fileName = _generateFileName(i + 1);
            String^ fullPath = String::Format("{0}\\{1}", localInbox, fileName);

            // Receive email from IMAP server
            EAGetMail::Mail ^oMail = oClient->GetMail(info);
            Console::WriteLine("From: {0}", oMail->From->ToString());
            Console::WriteLine("Subject: {0}\r\n", oMail->Subject);

            // Save email to local disk
            oMail->SaveAs(fullPath, true);

            // Mark email as read to prevent retrieving this email again.
            oClient->MarkAsRead(info, true);

            // If you want to delete current email, please use Delete method instead of MarkAsRead
            // oClient->Delete(info);

        // Quit and expunge emails marked as deleted from IMAP server.

    catch (Exception^ ep)

TLS 1.2 Protocol

TLS is the successor of SSL, more and more SMTP servers require TLS 1.2 encryption now.

If your operating system is Windows XP/Vista/Windows 7/Windows 2003/2008/2008 R2/2012/2012 R2, you need to enable TLS 1.2 protocol in your operating system like this:

Enable TLS 1.2 on Windows XP/Vista/7/10/Windows 2008/2008 R2/2012



If you have any comments or questions about above example codes, please click here to add your comments.