Using Gmail SMTP OAUTH

The Gmail IMAP and SMTP servers have been extended to support authorization via the industry-standard OAuth 2.0 protocol. Using OAUTH protocol, user can do authentication by Gmail Web OAuth instead of inputting user and password directly in application. This way is more secure, but a little bit complex.

Create your project in Google Developers Console

To use Gmail OAUTH in your application, you must create a project in Google Developers Console at first.

Using Client ID and Client secret to get access token

Now you can use client id and client secret to get the user email address and access token. Basically, OAUTH uses HTTP protocol, first of all, user inputs user and password in Google Web Authentication, Google returns access token back to your application, finally your application uses access token to access Gmail SMTP server. You can find the example codes in EASendMail Installation Path\Samples_{Programming language/Developer Tool}\GmailOauth.* project.

You must apply for your client id and client secret, don't use the client id in the sample project, because it is limited now. If you got "This app isn't verified" information, please click "advanced" -> Go to ... for test.

How to use access token with Gmail SMTP Service.

After you get user email address and access token, you can use the following codes to send email using Gmail SASL XOAUTH2 mechanism.


[Visual Basic, C#, C++] To get the full samples of EASendMail, please refer to Samples section.

[VB - Send Email using Gmail OAUTH Authentication]
Imports EASendMail
Sub SendMail( email As String, access_token as String )
    Dim oMail As SmtpMail = New SmtpMail("TryIt")
    Dim oSmtp As SmtpClient = New SmtpClient

        Dim oServer As SmtpServer = New SmtpServer("")
        ' set SSL connection
        oServer.ConnectType = SmtpConnectType.ConnectSSLAuto
        ' set smtp server port, you can also use 465 port
        oServer.Port = 587

        ' use Gmail SMTP OAUTH 2.0 authentication
        oServer.AuthType = SmtpAuthType.XOAUTH2

        'set user authentication
        oServer.User = email 

	'use the access token as password
        oServer.Password = access_token 
        oMail.From = New MailAddress(email)
        oMail.To.Add(New MailAddress(""))
        oMail.Subject = "test email sent from VB using Gmail OAUTH"
        oMail.TextBody = "test body"
        oSmtp.SendMail( oServer, oMail )
        Console.WriteLine( "message was sent" )
    Catch exp As System.Exception
        Console.WriteLine("Exception: Common: {0}", exp.Message)
    End Try

End Sub
[C# - Send Email using Gmail OAUTH Authentication]
using System;
using EASendMail;
void Send_OAUTH(string email, string access_token ) 
    SmtpMail oMail = new SmtpMail("TryIt"); 
    SmtpClient oSmtp = new SmtpClient(); 

    // Your gmail email address
    oMail.From = email; 

    // Set recipient email address
    oMail.To = ""; 

    // Set email subject
    oMail.Subject = "test email from gmail account with OAUTH 2"; 

    // Set email body
    oMail.TextBody = "this is a test email sent from c# project with gmail."; 

    // Gmail SMTP server address
    SmtpServer oServer = new SmtpServer(""); 

    // Using 587 port, you can also use 465 port
    oServer.Port = 587;

    // enable SSL connection
    oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; 

    // use Gmail SMTP OAUTH 2.0 authentication
    oServer.AuthType = SmtpAuthType.XOAUTH2;
    oServer.User = email; 

    // use the access token as password
    oServer.Password = access_token;   

        Console.WriteLine("start to send email using OAUTH 2.0 ..."); 
        oSmtp.SendMail(oServer, oMail); 
        Console.WriteLine("email was sent successfully!"); 
    catch (Exception ep) 
        Console.WriteLine("failed to send email with the following error:"); 

[C++/CLI - using Gmail OAUTH Authentication]
using namespace System;
using namespace EASendMail;
void SendMail( String^ email, String^ access_token )
    SmtpMail ^oMail = gcnew SmtpMail("TryIt");
    SmtpClient ^oSmtp = gcnew SmtpClient();

        SmtpServer ^oServer = gcnew SmtpServer("");
        //set SSL connection
        oServer->ConnectType = SmtpConnectType::ConnectSSLAuto;
        //set smtp server port, you can also use 465
        oServer->Port = 587;
        // use Gmail SMTP OAUTH 2.0 authentication
        oSmtp->AuthType = SmtpAuthType::XOAUTH2;
        //set user authentication
        oServer->User = email;

        // use the access token as password
        oServer->Password = access_token;
        oMail->From = gcnew MailAddress(email);
        oMail->To->Add( gcnew MailAddress(""));
        oMail->Subject = "test email sent from C++/CLI using Gmail OAUTH";
        oMail->TextBody = "test body";
        oSmtp->SendMail( oServer, oMail );
        Console::WriteLine( "message was sent" );
    catch( System::Exception ^exp )
        Console::WriteLine( "Exception: Common: {0}", exp->Message );           


If you don't want to use OAUTH 2.0, Gmail also supports traditional ESMTP authentication, but you need to enable Allowing less secure apps or Sign in using App Passwords.

See Also

User Authentication and SSL Connection
Using EASendMail SMTP .NET Component
From, ReplyTo, Sender and Return-Path
DomainKeys and DKIM Signature
Send E-mail Directly (Simulating SMTP server)
Work with EASendMail Service (Email Queuing)
Bulk Email Sender Guidelines
Process Bounced Email (Non-Delivery Report) and Email Tracking
EASendMail .NET Namespace References
EASendMail SMTP Component Samples