Unicode C
Unicode C
OAuth2 for a GMail using a P12 Service Account Key
See more GMail REST API Examples
Demonstrates how to use GMail with OAuth2 for aservice account within a Google Workspace Account where your email domain is custom (e.g., @yourcompany.com).
Note: This example does not work for Personal Google Accounts where the email domain is @gmail.com
Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkCertW.h>
#include <C_CkMailManW.h>
#include <C_CkEmailW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkCertW cert;
const wchar_t *iss;
const wchar_t *scope;
const wchar_t *oauth_sub;
int numSec;
const wchar_t *accessToken;
HCkMailManW mailman;
HCkEmailW email;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
// --------------------------------------------------------------------------------
// For a step-by-step guide for setting up your Google Workspace service account,
// see Setup Google Workspace Account for Sending SMTP GMail from a Service Account
// --------------------------------------------------------------------------------
// Begin by loading your Google service account key (.p12)
cert = CkCertW_Create();
success = CkCertW_LoadPfxFile(cert,L"c:/someDirectory/keys/chilkat25-cbd7b42afbd8.p12",L"notasecret");
if (success != TRUE) {
wprintf(L"%s\n",CkCertW_lastErrorText(cert));
CkHttpW_Dispose(http);
CkCertW_Dispose(cert);
return;
}
// The ISS is your service account email address ending in gserviceaccount.com.
iss = L"chilkatsvc@chilkat25.iam.gserviceaccount.com";
// The scope is always the following string:
scope = L"https://mail.google.com/";
// The sub is your company email address
oauth_sub = L"bob@yourcompany.com";
// The access token is valid for this number of seconds.
numSec = 3600;
accessToken = CkHttpW_g_SvcOauthAccessToken(http,iss,scope,oauth_sub,numSec,cert);
if (CkHttpW_getLastMethodSuccess(http) != TRUE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkCertW_Dispose(cert);
return;
}
else {
wprintf(L"access token: %s\n",accessToken);
}
// The access token allows us to send unlimited emails while it's valid. Once it expires, we must obtain and use a new one.
// -----------------------------------------------------------------------
mailman = CkMailManW_Create();
// Set the properties for the GMail SMTP server:
CkMailManW_putSmtpHost(mailman,L"smtp.gmail.com");
CkMailManW_putSmtpPort(mailman,587);
CkMailManW_putStartTLS(mailman,TRUE);
CkMailManW_putSmtpUsername(mailman,L"bob@yourcompany.com");
CkMailManW_putOAuth2AccessToken(mailman,accessToken);
// Create a new email object
email = CkEmailW_Create();
CkEmailW_putSubject(email,L"This is a test");
CkEmailW_putBody(email,L"This is a test");
CkEmailW_putFrom(email,L"Bob <bob@yourcompany.com>");
success = CkEmailW_AddTo(email,L"Recipient",L"recipient@example.com");
// To add more recipients, call AddTo, AddCC, or AddBcc once per recipient.
success = CkMailManW_SendEmail(mailman,email);
if (success != TRUE) {
wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
CkHttpW_Dispose(http);
CkCertW_Dispose(cert);
CkMailManW_Dispose(mailman);
CkEmailW_Dispose(email);
return;
}
success = CkMailManW_CloseSmtpConnection(mailman);
if (success != TRUE) {
wprintf(L"Connection to SMTP server not closed cleanly.\n");
}
wprintf(L"Successfully sent email using Gmail with a service account key.\n");
CkHttpW_Dispose(http);
CkCertW_Dispose(cert);
CkMailManW_Dispose(mailman);
CkEmailW_Dispose(email);
}