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.

https://developers.google.com/gmail/oauth_overview?hl=en

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.

Example

[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

    Try
        Dim oServer As SmtpServer = New SmtpServer("smtp.gmail.com")
        
        ' 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("support@emailarchitect.net"))
        
        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 = "support@emailarchitect.net"; 

    // 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("smtp.gmail.com"); 

    // 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;   

    try 
    { 
        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:"); 
        Console.WriteLine(ep.Message); 
    } 
} 

[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();

    try
    {
        SmtpServer ^oServer = gcnew SmtpServer("smtp.gmail.com");
        
        //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("support@emailarchitect.net"));
        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 );           
    }
} 
    

Remarks

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