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
To use Gmail OAUTH in your application, you must create a project in Google Developers Console at first.
You should set detail information for your project at "OAuth consent screen" -> "Edit App".
Finally add "https://mail.google.com/" and "../auth/gmail.send" scopes at "OAuth consent screen" -> "Edit App" -> "Scopes for Google API".
If you use Gmail RESTFul API to send email, please only use "../auth/gmail.send" scope to avoid your app throttled.
Scope Remarks
Gmail supports SMTP + OAUTH, but the API (https://mail.google.com/) scope is restricted API which requests to have full access to the Gmail account. Restricted API is throttled before your project is authenticated in by Google.
Using less restricted API (https://www.googleapis.com/auth/gmail.send) scope to send email via Gmail server is recommended.
Using Gmail API + OAUTH to send email
You can use client id and client secret to get the user email address and access token like this:
You should create your client id and client secret, don't use the client id in the sample project, it is only for test purpose. If you got "This app isn't verified" information, please click "advanced" -> Go to ... for test.
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++, Delphi] To get the full samples of EASendMail, please refer to Samples section.
[VB6 - Send Email using Gmail OAUTH Authentication] Const ConnectNormal = 0 Const ConnectSSLAuto = 1 Const ConnectSTARTTLS = 2 Const ConnectDirectSSL = 3 Const ConnectTryTLS = 4 Const AuthAuto = -1 Const AuthLogin = 0 Const AuthNtlm = 1 Const AuthCramMd5 = 2 Const AuthPlain = 3 Const AuthMsn = 4 Const AuthXoauth2 = 5 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" oSmtp.ServerPort = 587 ' Enable SSL/TLS connection oSmtp.ConnectType = ConnectSSLAuto ' Gmail OAUTH/XOAUTH2 type oSmtp.AuthType = AuthXoauth2 oSmtp.UserName = email oSmtp.Password = access_token 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 <tchar.h> #include <Windows.h> #include "EASendMailObj.tlh" using namespace EASendMailObjLib; const int ConnectNormal = 0; const int ConnectSSLAuto = 1; const int ConnectSTARTTLS = 2; const int ConnectDirectSSL = 3; const int ConnectTryTLS = 4; const int AuthAuto = -1; const int AuthLogin = 0; const int AuthNtlm = 1; const int AuthCramMd5 = 2; const int AuthPlain = 3; const int AuthMsn = 4; const int AuthXoauth2 = 5; 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"); oSmtp->ServerPort = 587; // Enable SSL/TLS connection oSmtp->ConnectType = ConnectSSLAuto; // Gmail OAUTH type oSmtp->AuthType = AuthXoauth2; oSmtp->UserName = lpszEmail; oSmtp->Password = lpszAccessToken; 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) _tprintf(_T("Message delivered!")); else _tprintf((const TCHAR*)oSmtp->GetLastErrDescription()); }
[Delphi - SMTP Gmail OAUTH User Authentication] const ConnectNormal = 0; ConnectSSLAuto = 1; ConnectSTARTTLS = 2; ConnectDirectSSL = 3; ConnectTryTLS = 4; AuthAuto = -1; AuthLogin = 0; AuthNtlm = 1; AuthCramMd5 = 2; AuthPlain = 3; AuthMsn = 4; AuthXoauth2 = 5; procedure TForm1.SendEmail(email: string, accesstoken: string); var oSmtp : TMail; begin oSmtp := TMail.Create(Application); oSmtp.LicenseCode := 'TryIt'; // Gmail SMTP server address oSmtp.ServerAddr := 'smtp.gmail.com'; oSmtp.ServerPort := 587; // Enable SSL/TLS connection oSmtp.ConnectType := ConnectSSLAuto; // Gmail OAUTH type oSmtp.AuthType := AuthXoauth2; oSmtp.UserName := email; oSmtp.Password := accesstoken; // 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'; 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 your application is background service which doesn't support user interaction, please have a look at this topic: Use G Suite service account to do SMTP OAUTH.
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 Example
VB6 - Send Email using Google/Gmail
OAuth 2.0
Authentication
VB6 - Send Email using Gmail/G Suite
OAuth
2.0 in Background Service (Service Account)
VB6 - Send Email using Microsoft
OAuth
2.0
(Modern Authentication) from Hotmail/Outlook Account
VB6 - Send Email using Microsoft
OAuth
2.0
(Modern Authentication) + EWS Protocol from Office 365 Account
VB6 - Send Email using Microsoft
OAuth
2.0
(Modern Authentication) + EWS Protocol from Office 365 in Background Service
Delphi - Send Email using Google/Gmail
OAuth 2.0
Authentication
Delphi - Send Email using Gmail/G Suite
OAuth
2.0 in Background Service (Service Account)
Delphi - Send Email using Microsoft
OAuth 2.0
(Modern Authentication) from Hotmail/Outlook Account
Delphi - Send Email using Microsoft
OAuth 2.0
(Modern Authentication) + EWS Protocol from Office 365 Account
Delphi - Send Email using Microsoft
OAuth 2.0
(Modern Authentication) + EWS Protocol from Office 365 in Background Service
Visual C++ - Send Email using
Google/Gmail
OAuth 2.0
Authentication
Visual C++ - Send Email using Gmail/G
Suite OAuth
2.0 in Background Service (Service Account)
Visual C++ - Send Email using
Microsoft
OAuth 2.0
(Modern Authentication) from Hotmail/Outlook Account
Visual C++ - Send Email using
Microsoft
OAuth 2.0
(Modern Authentication) + EWS Protocol from Office 365 Account
Visual C++ - Send Email using
Microsoft
OAuth 2.0
(Modern Authentication) + EWS Protocol from Office 365 in Background Service
See Also
Using EASendMail ActiveX Object
Registration-free COM with Manifest File
User Authentication and SSL Connection
Enable TLS 1.2 on Windows XP/2003/2008/7/2008 R2
Using Gmail SMTP OAUTH
Using Gmail/GSuite Service Account + SMTP OAUTH
Authentication
Using Office365 EWS OAUTH
Using Office365 EWS OAUTH in Background Service
Using Hotmail SMTP OAUTH
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