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 SendMailWithXOAUTH2(userEmail As String, accessToken As String)

    Try
        ' set Gmail SMTP server address
        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 = userEmail
        ' use access token as password
        oServer.Password = accessToken

        Dim oMail As SmtpMail = New SmtpMail("TryIt")
        oMail.From = New MailAddress(userEmail)
        oMail.To.Add(New MailAddress("support@emailarchitect.net"))

        oMail.Subject = "test email sent from VB using Gmail OAUTH"
        oMail.TextBody = "test body"

        Dim oSmtp As SmtpClient = New SmtpClient
        oSmtp.SendMail(oServer, oMail)

        Console.WriteLine("The email has been submitted to server successfully!")

    Catch exp As Exception
        Console.WriteLine("Exception: {0}", exp.Message)
    End Try

End Sub
    

[C# - Send Email using Gmail OAUTH Authentication] using System; using EASendMail; void SendMailWithXOAUTH2(string userEmail, string accessToken) { try { // Gmail SMTP server address SmtpServer oServer = new SmtpServer("smtp.gmail.com"); // enable SSL connection oServer.ConnectType = SmtpConnectType.ConnectSSLAuto; // Using 587 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 = userEmail; // use access token as password oServer.Password = accessToken; SmtpMail oMail = new SmtpMail("TryIt"); // Your gmail email address oMail.From = userEmail; oMail.To = "support@emailarchitect.net"; oMail.Subject = "test email from gmail account with OAUTH 2"; oMail.TextBody = "this is a test email sent from c# project with gmail."; Console.WriteLine("start to send email using OAUTH 2.0 ..."); SmtpClient oSmtp = new SmtpClient(); oSmtp.SendMail(oServer, oMail); Console.WriteLine("The email has been submitted to server successfully!"); } catch (Exception ep) { Console.WriteLine("Exception: {0}", ep.Message); } }
[C++/CLI - using Gmail OAUTH Authentication] using namespace System; using namespace EASendMail; void SendMailWithXOAUTH2(String^ userEmail, String^ accessToken) { try { // Gmail SMTP server address 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 oServer->AuthType = SmtpAuthType::XOAUTH2; //set user authentication oServer->User = userEmail; // use access token as password oServer->Password = accessToken; SmtpMail ^oMail = gcnew SmtpMail("TryIt"); oMail->From = gcnew MailAddress(userEmail); oMail->To->Add(gcnew MailAddress("support@emailarchitect.net")); oMail->Subject = "test email sent from C++/CLI using Gmail OAUTH"; oMail->TextBody = "test body"; SmtpClient ^oSmtp = gcnew SmtpClient(); oSmtp->SendMail(oServer, oMail); Console::WriteLine("The email has been submitted to server successfully!"); } catch (System::Exception ^exp) { Console::WriteLine("Exception: {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