Sample code for 30+ languages & platforms
Delphi DLL

SMTP XOAUTH2 Authentication

See more SMTP Examples

Demonstrates how to authenticate using an OAuth2 access token. This example assumes you have already obtained the access token and now wish to use it in an SMTP session.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, MailMan, Email;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
oauth2_access_token: PWideChar;
mailman: HCkMailMan;
email: HCkEmail;

begin
success := False;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

oauth2_access_token := 'some_previously_obtained_token';

mailman := CkMailMan_Create();

// Set the properties for the SMTP server, whatever they may be..
CkMailMan_putSmtpHost(mailman,'smtp.example.com');
CkMailMan_putSmtpPort(mailman,587);
CkMailMan_putStartTLS(mailman,True);

CkMailMan_putSmtpUsername(mailman,'myLogin');
CkMailMan_putOAuth2AccessToken(mailman,oauth2_access_token);

// Create a new email object
email := CkEmail_Create();

CkEmail_putSubject(email,'This is a test');
CkEmail_putBody(email,'This is a test');
CkEmail_putFrom(email,'Joe Example <joe@example.com>');
CkEmail_AddTo(email,'Chilkat Admin','admin@chilkatsoft.com');

// Connect to the server indicated by the SmtpHost property
success := CkMailMan_SmtpConnect(mailman);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

// Authenticate using XOAUTH2 (using the OAuth2AccessToken)
success := CkMailMan_SmtpAuthenticate(mailman);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

success := CkMailMan_SendEmail(mailman,email);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkMailMan__lastErrorText(mailman));
    Exit;
  end;

Memo1.Lines.Add('Email sent.');

CkMailMan_CloseSmtpConnection(mailman);

CkMailMan_Dispose(mailman);
CkEmail_Dispose(email);

end;