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 6.0, VC++] To get the full samples of EASendMail, please refer to Samples section.

[VB6 - Send Email using Gmail OAUTH Authentication]
    
Private Sub SendEmail( email As String, access_token As String )
  Dim oSmtp As EASendMailObjLib.Mail
  Set oSmtp = New EASendMailObjLib.Mail
 
  oSmtp.LicenseCode = "TryIt"
  oSmtp.ServerAddr = "smtp.gmail.com"
  'Set server port, you can also use 465 port
  oSmtp.ServerPort = 587
  
  'Gmail OAUTH type
  oSmtp.AuthType = 5 
  oSmtp.UserName = email
  oSmtp.Password = access_token
  
  ' Enable SSL/TLS connection
  oSmtp.SSL_init
  
  oSmtp.FromAddr = email
  oSmtp.AddRecipient "Support Team", "support@emailarchitect.net", 0
 
  oSmtp.BodyText = "Hello, this is a test...."
  If oSmtp.SendMail() = 0 Then
    MsgBox "Message delivered!"
  Else
    MsgBox oSmtp.GetLastErrDescription()
  End If
End Sub

[Visual C++ - SMTP Gmail OAUTH User Authentication]
#include "stdafx.h"
#include <comdef.h>
#include <iostream>
 
#include "easendmailobj.tlh"
using namespace EASendMailObjLib;
using namespace std;
 
void SendEmail( const TCHAR* lpszEmail, const TCHAR* lpszAccessToken )
{
  ::CoInitialize( NULL );
  IMailPtr oSmtp = NULL;
  oSmtp.CreateInstance(__uuidof(EASendMailObjLib::Mail));
  oSmtp->LicenseCode = _T("TryIt");
	
  oSmtp->ServerAddr = _T( "smtp.gmail.com" );
  
   //Set server port, you can also use 465 port
  oSmtp->ServerPort = 587;
  
  //Gmail OAUTH type
  oSmtp->AuthType = 5; 
  oSmtp->UserName = lpszEmail;
  oSmtp->Password = lpszAccessToken;
  
  //Enable SSL/TLS connection
  oSmtp->SSL_init 
  
  oSmtp->FromAddr = lpszEmail;
  oSmtp->AddRecipient( _T("Support Team"), 
                          _T("support@emailarchitect.net"), 0 );
 
  
  oSmtp->BodyText = _T("Hello, this is a test....");
  if( oSmtp->SendMail() == 0 )
    cout << "Message delivered!" << endl;
  else
    cout << (const TCHAR*)(oSmtp->GetLastErrDescription()) << endl;
}

[Delphi - SMTP Gmail OAUTH User Authentication]

procedure TForm1.SendEmail(email: string, accesstoken: string);
var
  oSmtp : TMail;
begin
  oSmtp := TMail.Create(Application);
  oSmtp.LicenseCode := 'TryIt';

  // Set sender email address
  oSmtp.FromAddr := email;

  // Add recipient email address
  oSmtp.AddRecipientEx( 'support@emailarchitect.net', 0);

  // Set email subject
  oSmtp.Subject := 'simple email from Delphi project';

  // Set email body
  oSmtp.BodyText := 'this is a test email sent from Delphi project, do not reply';

  // Gmail SMTP server address
  oSmtp.ServerAddr := 'smtp.gmail.com';
  
   //Set server port, you can also use 465 port
  oSmtp.ServerPort := 587;
  
  // Gmail OAUTH type
  oSmtp.AuthType := 5; 
  oSmtp.UserName := email;
  oSmtp.Password := accesstoken;

  // Enable SSL/TLS connection
  oSmtp.SSL_init();
  
  ShowMessage( 'start to send email ...' );

  if oSmtp.SendMail() = 0 then
    ShowMessage( 'email was sent successfully!' )
  else
    ShowMessage( 'failed to send email with the following error: '
    + oSmtp.GetLastErrDescription());

end;

end.

    

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.

Online Examples

Send Email over SSL/TLS - VB6
Send Email over SSL/TLS - VC++
Send Email over SSL/TLS - Delphi

Online Tutorial

Send Email over SSL/TLS in VB6
Send Email using Gmail in VB6
Send Email using Yahoo in VB6
Send Email using Hotmail/Live in VB6

Send Email over SSL/TLS in VC++
Send Email using Gmail in VC++
Send Email using Yahoo in VC++
Send Email using Hotmail/Live in VC++

Send Email over SSL/TLS in Delphi
Send Email using Gmail in Delphi
Send Email using Yahoo in Delphi
Send Email using Hotmail/Live in Delphi

See Also

Using EASendMail ActiveX Object
Registration-free COM with Manifest File
From, ReplyTo, Sender and Return-Path
Digital Signature and Email Encryption - S/MIME
DomainKeys Signature and DKIM Signature
Send Email without SMTP server(DNS lookup)
Work with EASendMail Service(Mail Queuing)
Programming with Asynchronous Mode
Programming with FastSender
Mail vs. FastSender
Bulk Email Sender Guidelines
Process Bounced Email (Non-Delivery Report) and Email Tracking
Work with RTF and Word
EASendMail ActiveX Object References
EASendMail SMTP Component Samples