{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


Author:       François PIETTE
Object:       How to use TSslSmtpCli component
Creation:     09 october 1997
Version:      V9.8
EMail:        francois.piette@overbyte.be  http://www.overbyte.be
Support:      https://en.delphipraxis.net/forum/37-ics-internet-component-suite/
Legal issues: Copyright (C) 1997-2026 by François PIETTE
              Rue de Grady 24, 4053 Embourg, Belgium.
              SSL implementation includes code written by Arno Garrels,
              Berlin, Germany

              This software is provided 'as-is', without any express or
              implied warranty.  In no event will the author be held liable
              for any  damages arising from the use of this software.

              Permission is granted to anyone to use this software for any
              purpose, including commercial applications, and to alter it
              and redistribute it freely, subject to the following
              restrictions:

              1. The origin of this software must not be misrepresented,
                 you must not claim that you wrote the original software.
                 If you use this software in a product, an acknowledgment
                 in the product documentation would be appreciated but is
                 not required.

              2. Altered source versions must be plainly marked as such, and
                 must not be misrepresented as being the original software.

              3. This notice may not be removed or altered from any source
                 distribution.

              4. You must register this software by sending a picture postcard
                 to the author. Use a nice stamp and mention your name, street
                 address, EMail address and any comment you like to say.

Updates:
Aug 20, 2003 SSL support added by Arno Garrels <arno.garrels@gmx.de>.
Dec 29, 2007 A. Garrels reworked SSL, simplified verification code.
Jul 23, 2008 A. Garrels changed code in OnGetDate event handler to prepare
             code for Unicode
Aug 03, 2008 A. Garrels changed code in OnGetDate event handler to prepare
             code for Unicode again.
May 17, 2009 A.Garrels added correct casts to PAnsiChar in SslSmtpClientHeaderLine.
Apr 15, 2017 V2.11 FPiette removed compiler warnings for D10.2
Nov 25, 2020 V8.65 - Added XOAuth2 and OAuthBearer authentication support using
               TIcsRestEmail component for OAuth2, allows to access GMail account
               with security enabled and Microsoft Outlook mail.
               See comments at top of OverbyteIcsSslHttpRest.pas on how to
               set-up a Google/Microsoft OAuth2 application account.
               Note you have to specify ClientId and ClientPassword later
               before OAuth2 will work.
             Corrected SslContext so it works with TLS/1.2 and later.
             Changed defaults for implicit SSL connection which is standard now.
Mar 23, 2022 V8.69 - Added unit OverbyteIcsSslHttpOAuth for TIcsRestEmail
                previously in HttpRest.
Aug 5, 2022  V8.70 - Added header character set and body line wrapping and
                encoding from non-SSL sample.
             Note the UseMailMessageCheckBox means you pass the entire
                body in MailMessage property instead of using the OnGetData
                event to send one line at a time, this will also word wrap
                long lines and encode body content.
Dec 19, 2022 V8.71 Support OAuth authentication OAuthTypeEmbed (embedded browser)
                using new TOAuthBrowser component and TOAuthLoginForm window, that
                uses TEdgeBrowser or TWebBrowser to display web page without
                needing external browser or web server, traps redirect request
                to get authentication code, Edge is Delphi 10.4 or later only.
             Added Rest Email Microsoft User Authority property to access
               different user authorities, defaults to consumers but can be
               changed to common or an Azure Active Directory tenant GUID for
               corporate accounts.
             Added OAuth2 External Browser tick box instead of embedded browser.
             Set IcsRestEmail account hint to login name for browser hint.
Aug 08, 2023 V9.0  Updated version to major release 9.
Sep 24, 2024 V9.3  Using OverbyteIcsTypes for consolidated types and constants, allowing
                     other import units to be removed.
                   Using Segoe UI font which scale properly.
                   Cleaned up form, display more lines in log.
Jan 29, 2026 V9.6  Using new onDebugDisplay event to display messages from
                      TWSocket, controlled by TWSDebugOptions. Now reports
                      SSL certificate chain.
                   Removed SSL cert logging now done in WSocket.
                   Don't set client SSL certificate unless the files actually
                     exist, very rarely needed!!!
Aug 21, 2026 V9.8  Commonise sample caption version and date, from Types.
                   Using OverbyteIcsOAuthFormVclE unit for Delphi 11 and later,
                    which is more compatible with new Delphi versions.



 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit OverbyteIcsSslMailSnd1;

{$I Include\OverbyteIcsDefs.inc}
{$IFDEF DELPHI25_UP}
   {$WARN SYMBOL_DEPRECATED OFF}
{$ENDIF}
{$IFNDEF USE_SSL}
  {$MESSAGE FATAL 'Define conditional define "USE_SSL" in the project options'};
{$ENDIF}
{$IF CompilerVersion < 15}
  {$MESSAGE FATAL 'This demo requires at least Delphi 7 or better'};
{$IFEND}

{$B-}           { Enable partial boolean evaluation   }
{$T-}           { Untyped pointers                    }
{$X+}           { Enable extended syntax              }

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,
  OverbyteIcsIniFiles,
  OverbyteIcsWndControl,
  OverbyteIcsSmtpProt,
  OverbyteIcsSslHttpOAuth,        { V8.69 }
  OverbyteIcsCharsetComboBox,
//{$IFDEF COMPILER27_UP}
  OverbyteIcsOAuthFormVclE,  { V9.8 used for D10.4 and later only }
//{$ELSE}
//  OverbyteIcsOAuthFormVcl,  { V8.71 used for D10.3 and earlier }
//{$ENDIF}
  OverbyteIcsSslBase,      { V8.70 }
  OverbyteIcsTypes;        { V9.3 consolidated types and constants }

const
  SslSmtpTestVersion    = IcsVerLitNum;                      { V9.8 }
  CopyRight : String    = ' SslMailSnd' + IcsVerLitCopyright;         { V9.8 }
  WM_SSL_RECONNECT      = WM_USER + 1;

type
  TSslSmtpTestForm = class(TForm)
    MsgMemo: TMemo;
    DisplayMemo: TMemo;
    ToolsPanel: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Subject: TLabel;
    Label4: TLabel;
    HostEdit: TEdit;
    FromEdit: TEdit;
    ToEdit: TEdit;
    SubjectEdit: TEdit;
    SignOnEdit: TEdit;
    PortEdit: TEdit;
    Label5: TLabel;
    AttachPanel: TPanel;
    Label6: TLabel;
    FileAttachMemo: TMemo;
    InfoPanel: TPanel;
    Label7: TLabel;
    ClearDisplayButton: TButton;
    ConnectButton: TButton;
    HeloButton: TButton;
    MailFromButton: TButton;
    RcptToButton: TButton;
    DataButton: TButton;
    AbortButton: TButton;
    QuitButton: TButton;
    MailButton: TButton;
    OpenButton: TButton;
    Label8: TLabel;
    SslSmtpClient: TSslSmtpCli;
    Label9: TLabel;
    Label10: TLabel;
    UsernameEdit: TEdit;
    PasswordEdit: TEdit;
    AuthComboBox: TComboBox;
    PriorityComboBox: TComboBox;
    Label11: TLabel;
    EhloButton: TButton;
    AuthButton: TButton;
    Label12: TLabel;
    CcEdit: TEdit;
    Label13: TLabel;
    BccEdit: TEdit;
    AllInOneButton: TButton;
    TlsButton: TButton;
    VerifyPeerCheckBox: TCheckBox;
    SmtpSslModeCombobox: TComboBox;
    Label15: TLabel;
    KeyEdit: TEdit;
    CertEdit: TEdit;
    Label16: TLabel;
    Label17: TLabel;
    CaFileEdit: TEdit;
    CaPathEdit: TEdit;
    Label18: TLabel;
    Label19: TLabel;
    PassPhraseEdit: TEdit;
    Label21: TLabel;
    SslContext1: TSslContext;
    IcsRestEmail1: TIcsRestEmail;
    IcsCharsetComboBox1: TIcsCharsetComboBox;
    ConvertToCharsetCheckBox: TCheckBox;
    Allow8BitCheckBox: TCheckBox;
    FoldHeadersCheckBox: TCheckBox;
    DefEnc: TLabel;
    DefEncodingComboBox: TComboBox;
    WrapTextCheckBox: TCheckBox;
    Label20: TLabel;
    WrapAtEdit: TEdit;
    UseMailMessageCheckBox: TCheckBox;
    OAuth2ExtBrow: TCheckBox;
    Label22: TLabel;
    OAuth2UserAuth: TComboBox;
    OAuthBrowser1: TOAuthBrowser;
    procedure FormCreate(Sender: TObject);
    procedure ClearDisplayButtonClick(Sender: TObject);
    procedure ConnectButtonClick(Sender: TObject);
    procedure SslSmtpClientRequestDone(Sender: TObject; RqType: TSmtpRequest;  Error: Word);
    procedure HeloButtonClick(Sender: TObject);
    procedure MailFromButtonClick(Sender: TObject);
    procedure RcptToButtonClick(Sender: TObject);
    procedure DataButtonClick(Sender: TObject);
    procedure AbortButtonClick(Sender: TObject);
    procedure QuitButtonClick(Sender: TObject);
    procedure MailButtonClick(Sender: TObject);
    procedure OpenButtonClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure EhloButtonClick(Sender: TObject);
    procedure AuthButtonClick(Sender: TObject);
    procedure AllInOneButtonClick(Sender: TObject);
    procedure SmtpClientDisplay(Sender: TObject; Msg: String);
    procedure SslSmtpClientGetData(Sender: TObject; LineNum: Integer;  MsgLine: Pointer; MaxLen: Integer; var More: Boolean);
    procedure SslSmtpClientHeaderLine(Sender: TObject; Msg: Pointer; Size: Integer);
    procedure SslSmtpClientBeforeFileOpen(Sender: TObject; Idx: Integer;  FileName: String; var Action: TSmtpBeforeOpenFileAction);
    procedure TlsButtonClick(Sender: TObject);
    procedure SslSmtpClientSslVerifyPeer(Sender: TObject; var Ok: Integer; Cert: TX509Base);
    procedure SslSmtpClientSslHandshakeDone(Sender: TObject; ErrCode: Word; PeerCert: TX509Base; var Disconnect: Boolean);
    procedure IcsRestEmail1EmailNewToken(Sender: TObject);
    procedure IcsRestEmail1EmailProg(Sender: TObject; LogOption: TLogOption;  const Msg: string);
    procedure SslSmtpClientGetNewToken(Sender: TObject);
    procedure AuthComboBoxChange(Sender: TObject);
    procedure IcsRestEmail1OAEmbed(Sender: TObject; const URL: string; var Success: Boolean);
  private
    FIniFileName  : String;
    FInitialized  : Boolean;
    FAllInOneFlag : Boolean;
    FEhloCount    : Integer;
    procedure WMSslReconnect(var Msg: TMessage); message WM_SSL_RECONNECT;
    procedure Display(const Msg : String);
    procedure ExceptionHandler(Sender: TObject; E: Exception);
    procedure SetupOAuth2;                                                      { V8.65 }
  end;

var
  SslSmtpTestForm: TSslSmtpTestForm;

implementation

{$R *.DFM}
const
    SectionData         = 'Data';
    KeyHost             = 'HostName';
    KeyPort             = 'Port';
    KeyFrom             = 'From';
    KeyTo               = 'To';
    KeyCc               = 'Cc';
    KeyBcc              = 'Bcc';
    KeySubject          = 'Subject';
    KeySignOn           = 'SignOn';
    KeyUser             = 'UserName';
    KeyPass             = 'Password';
    KeyAuth             = 'Authentification';
    KeyPriority         = 'Priority';
    SectionWindow       = 'Window';
    KeyTop              = 'Top';
    KeyLeft             = 'Left';
    KeyWidth            = 'Width';
    KeyHeight           = 'Height';
    SectionFileAttach   = 'Files';
    KeyFileAttach       = 'File';
    SectionMsgMemo      = 'Message';
    KeyMsgMemo          = 'Msg';
    KeySmtpSslMode      = 'SmtpSslMode';
    KeySslVerifyPeer    = 'SslVerifyPeer';
    KeySslKey           = 'SslKey';
    KeySslCert          = 'SslCert';
    KeySslCAFile        = 'SslCaFile';
    KeySslCAPath        = 'SslCaPath';
    KeySslHostsAccepted = 'SslHostsAccepted';
    KeySslPassPhrase    = 'SslPassPhrase';
    KeyConvertToCharset = 'ConvertToCharset';
    KeyUseOnGetData     = 'UseOnGetData';
    KeyAllow8Bit        = 'Allow8bit';
    KeyFoldHeaders      = 'FoldHeaders';
    KeyWrapText         = 'WrapText';
    KeyWrapAt           = 'WrapAt';
    KeyCharSet          = 'Charset';
    KeyDefTransEnc      = 'DefaultTransferEncoding';
    KeyOAuth2ExtBrow    = 'OAuth2ExtBrow';
    KeyOAuth2UserAuth   = 'OAuth2UserAuth';

{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure SaveStringsToIniFile(
    IniFile           : TIcsIniFile;
    const IniSection  : String;
    const IniKey      : String;
    Strings           : TStrings);
var
    nItem   : Integer;
begin
    if (IniSection = '') or (IniKey = '') or (not Assigned(Strings)) then
        Exit;
    IniFile.EraseSection(IniSection);
    if Strings.Count <= 0 then
        IniFile.WriteString(IniSection, IniKey + 'EmptyFlag', 'Empty')
    else
        for nItem := 0 to Strings.Count - 1 do
            IniFile.WriteString(IniSection,
                                IniKey + IntToStr(nItem),
                                Strings.Strings[nItem]);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Return FALSE if non existant in IniFile                                   }
function LoadStringsFromIniFile(
    IniFile           : TIcsIniFile;
    const IniSection  : String;
    const IniKey      : String;
    Strings           : TStrings) : Boolean;
var
    nItem   : Integer;
    I       : Integer;
    Buf     : String;
begin
    Result := TRUE;
    if (IniSection = '') or (IniKey = '') or  (not Assigned(Strings)) then
        Exit;
    Strings.Clear;
    if IniFile.ReadString(IniSection, IniKey + 'EmptyFlag', '') <> '' then
        Exit;
    IniFile.ReadSectionValues(IniSection, Strings);
    nItem := Strings.Count - 1;
    while nItem >= 0 do begin
        Buf := Strings.Strings[nItem];
        if CompareText(IniKey, Copy(Buf, 1, Length(IniKey))) <> 0 then
            Strings.Delete(nItem)
        else begin
            if not (Word(Buf[Length(IniKey) + 1]) in [Ord('0')..Ord('9')]) then
                Strings.Delete(nItem)
            else begin
                I := Pos('=', Buf);
                Strings.Strings[nItem] := Copy(Buf, I + 1, Length(Buf));
            end;
        end;
        Dec(nItem);
    end;
    Result := (Strings.Count <> 0);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Display a message in display memo box, making sure we don't overflow it.  }
procedure TSslSmtpTestForm.Display(const Msg : String);
begin
    DisplayMemo.Lines.BeginUpdate;
    try
        if DisplayMemo.Lines.Count > 10000 then begin   { V9.3 was 200 lines }
            { We preserve only 200 lines }
            while DisplayMemo.Lines.Count > 10000 do
                DisplayMemo.Lines.Delete(0);
        end;
        DisplayMemo.Lines.Add(Msg);
    finally
        DisplayMemo.Lines.EndUpdate;
        { Makes last line visible }
        {$IFNDEF VER80}
        SendMessage(DisplayMemo.Handle, EM_SCROLLCARET, 0, 0);
        {$ENDIF}
    end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.FormCreate(Sender: TObject);
begin
    Caption := Caption + IcsVerLitSample;   { V9.8 commonise sample caption version and date }
{$IFDEF DELPHI10_UP}
    // BDS2006 has built-in memory leak detection and display
    ReportMemoryLeaksOnShutdown := (DebugHook <> 0);
{$ENDIF}
    //IsConsole := AllocConsole;
    Application.OnException := ExceptionHandler;
    DisplayMemo.Clear;
    FIniFileName := GetIcsIniFileName;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.FormShow(Sender: TObject);
var
    IniFile : TIcsIniFile;
begin
    if not FInitialized then begin
        FInitialized := TRUE;
        IniFile := TIcsIniFile.Create(FIniFileName);
        HostEdit.Text    := IniFile.ReadString(SectionData, KeyHost,
                                               'localhost');
        PortEdit.Text    := IniFile.ReadString(SectionData, KeyPort,
                                               '465');
        FromEdit.Text    := IniFile.ReadString(SectionData, KeyFrom,
                                               'first.last@company.com');
        ToEdit.Text      := IniFile.ReadString(SectionData, KeyTo,
                                               'john.doe@acme;tartempion@brol.fr');
        CcEdit.Text      := IniFile.ReadString(SectionData, KeyCc,
                                               '');
        BccEdit.Text     := IniFile.ReadString(SectionData, KeyBcc,
                                               'francois.piette@swing.be');
        SubjectEdit.Text := IniFile.ReadString(SectionData, KeySubject,
                                               'This is the message subject');
        SignOnEdit.Text  := IniFile.ReadString(SectionData, KeySignOn,
                                               'your name');
        UsernameEdit.Text :=  IniFile.ReadString(SectionData, KeyUser,
                                               'account name');
        PasswordEdit.Text :=  IniFile.ReadString(SectionData, KeyPass,
                                               'account password');
        AuthComboBox.ItemIndex :=  IniFile.ReadInteger(SectionData,
                                            KeyAuth, Ord(smtpAuthLogin));       { V8.65 SSL defaults }
        SmtpSslModeCombobox.ItemIndex := IniFile.ReadInteger(SectionData,
                                               KeySmtpSslMode, Ord(smtpTlsImplicit));
        VerifyPeerCheckBox.Checked := IniFile.ReadBool(SectionData, KeySslVerifyPeer, False);
        OAuth2UserAuth.Text :=  IniFile.ReadString(SectionData, KeyOAuth2UserAuth,  '');   { V8.71 }
        OAuth2ExtBrow.Checked := IniFile.ReadBool(SectionData, KeyOAuth2ExtBrow, False);   { V8.71 }
        KeyEdit.Text :=  IniFile.ReadString(SectionData, KeySslKey,
                                               '01key.pem');
        PassPhraseEdit.Text := IniFile.ReadString(SectionData, KeySslPassPhrase,
                                               'password');
        CertEdit.Text :=  IniFile.ReadString(SectionData, KeySslCert,
                                               '01cert.pem');
        CAFileEdit.Text :=  IniFile.ReadString(SectionData, KeySslCaFile,
                                               'TrustedCABundle.pem');
        CAPathEdit.Text :=  IniFile.ReadString(SectionData, KeySslCaPath,
                                               'TrustedCaStore');
        PriorityComboBox.ItemIndex := IniFile.ReadInteger(SectionData, KeyPriority, 2);

        if not LoadStringsFromIniFile(IniFile, SectionFileAttach,
                                      KeyFileAttach, FileAttachMemo.Lines) then
        FileAttachMemo.Text := 'ics_logo.gif' + #13#10 + 'fp_small.gif';
        if not LoadStringsFromIniFile(IniFile, SectionMsgMemo,
                                      KeyMsgMemo, MsgMemo.Lines) then
            MsgMemo.Text :=
            'This is the first line' + #13#10 +
            'Then the second one' + #13#10 +
            'The next one is empty' + #13#10 +
            '' + #13#10 +
            'The next one has only a single dot' + #13#10 +
            '.' + #13#10 +
            'Finally the last one' + #13#10;

      { V8.70 character set stuff copied from non-SSL sample }
        ConvertToCharsetCheckBox.Checked := IniFile.ReadBool(SectionData, KeyConvertToCharset, False);
        {$IFDEF UNICODE}
            { In Delphi 2009 we have to convert from UTF-16 always, property }
            { ConvertToCharset is ignored in 2009 and better.                }
            ConvertToCharsetCheckBox.Visible := FALSE;
        {$ENDIF}
        UseMailMessageCheckBox.Checked := IniFile.ReadBool(SectionData, KeyUseOnGetData, False);
        Allow8BitCheckBox.Checked := IniFile.ReadBool(SectionData, KeyAllow8bit, False);
        FoldHeadersCheckBox.Checked := IniFile.ReadBool(SectionData, KeyFoldHeaders, True);
        WrapTextCheckBox.Checked := IniFile.ReadBool(SectionData, KeyWrapText, False);
        WrapAtEdit.Text := IniFile.ReadString(SectionData, KeyWrapAt, '76');
        IcsCharsetComboBox1.Charset := IniFile.ReadString(SectionData, KeyCharset, SslSmtpClient.CharSet);
        DefEncodingComboBox.ItemIndex := IniFile.ReadInteger(SectionData, KeyDefTransEnc, 0);
        if OAuth2UserAuth.Text = '' then
            OAuth2UserAuth.Text := OAuthMsUserAuthDef;                                   { V8.71 }

        Top    := IniFile.ReadInteger(SectionWindow, KeyTop,    (Screen.Height - Height) div 2);
        Left   := IniFile.ReadInteger(SectionWindow, KeyLeft,   (Screen.Width - Width) div 2);
        Width  := IniFile.ReadInteger(SectionWindow, KeyWidth,  Width);
        Height := IniFile.ReadInteger(SectionWindow, KeyHeight, Height);

        IniFile.Free;
    end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.FormClose(Sender: TObject;
  var Action: TCloseAction);
var
    IniFile : TIcsIniFile;
begin
    IniFile := TIcsIniFile.Create(FIniFileName);
    IniFile.WriteString(SectionData, KeyHost,             HostEdit.Text);
    IniFile.WriteString(SectionData, KeyPort,             PortEdit.Text);
    IniFile.WriteString(SectionData, KeyFrom,             FromEdit.Text);
    IniFile.WriteString(SectionData, KeyTo,               ToEdit.Text);
    IniFile.WriteString(SectionData, KeyCc,               CcEdit.Text);
    IniFile.WriteString(SectionData, KeyBcc,              BccEdit.Text);
    IniFile.WriteString(SectionData, KeySubject,          SubjectEdit.Text);
    IniFile.WriteString(SectionData, KeySignOn,           SignOnEdit.Text);
    IniFile.WriteString(SectionData, KeyUser,             UsernameEdit.Text);
    IniFile.WriteString(SectionData, KeyPass,             PasswordEdit.Text);
    IniFile.WriteInteger(SectionData, KeyAuth,            AuthComboBox.ItemIndex);
    IniFile.WriteInteger(SectionData, KeySmtpSslMode,     SmtpSslModeCombobox.ItemIndex);
    IniFile.WriteBool(SectionData, KeySslVerifyPeer,      VerifyPeerCheckBox.Checked);
    IniFile.WriteString(SectionData, KeyOAuth2UserAuth,   OAuth2UserAuth.Text);        { V8.71 }
    IniFile.WriteBool(SectionData, KeyOAuth2ExtBrow,      OAuth2ExtBrow.Checked);      { V8.71 }
    IniFile.WriteString(SectionData, KeySslKey,           KeyEdit.Text);
    IniFile.WriteString(SectionData, KeySslPassPhrase,    PassPhraseEdit.Text);
    IniFile.WriteString(SectionData, KeySslCert,          CertEdit.Text);
    IniFile.WriteString(SectionData, KeySslCaFile,        CAFileEdit.Text);
    IniFile.WriteString(SectionData, KeySslCaPath,        CAPathEdit.Text);
    IniFile.WriteInteger(SectionData, KeyPriority,        PriorityComboBox.ItemIndex);
    SaveStringsToIniFile(IniFile, SectionFileAttach,      KeyFileAttach, FileAttachMemo.Lines);
    SaveStringsToIniFile(IniFile, SectionMsgMemo,         KeyMsgMemo, MsgMemo.Lines);

 { V8.70 character set stuff copied from non-SSL sample }
    IniFile.WriteBool(SectionData, KeyConvertToCharset, ConvertToCharsetCheckBox.Checked);
    IniFile.WriteBool(SectionData, KeyUseOnGetData, UseMailMessageCheckBox.Checked);
    IniFile.WriteBool(SectionData, KeyAllow8bit, Allow8BitCheckBox.Checked);
    IniFile.WriteBool(SectionData, KeyFoldHeaders, FoldHeadersCheckBox.Checked);
    IniFile.WriteBool(SectionData, KeyWrapText, WrapTextCheckBox.Checked);
    IniFile.WriteString(SectionData, KeyWrapAt, WrapAtEdit.Text);
    IniFile.WriteString(SectionData, KeyCharset, IcsCharsetComboBox1.CharSet);
    IniFile.WriteInteger(SectionData, KeyDefTransEnc, DefEncodingComboBox.ItemIndex);

    IniFile.WriteInteger(SectionWindow, KeyTop,    Top);
    IniFile.WriteInteger(SectionWindow, KeyLeft,   Left);
    IniFile.WriteInteger(SectionWindow, KeyWidth,  Width);
    IniFile.WriteInteger(SectionWindow, KeyHeight, Height);
    IniFile.UpdateFile;
    IniFile.Free;
end;



{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.SmtpClientDisplay(Sender: TObject; Msg: String);
begin
    Display(Msg);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.SslSmtpClientGetData(
    Sender  : TObject;
    LineNum : Integer;
    MsgLine : Pointer;
    MaxLen  : Integer;
    var More: Boolean);
begin
    if LineNum > MsgMemo.Lines.count then
        More := FALSE
    else
        { Truncate the line if too long (should wrap to next line) }
        StrPLCopy(PAnsiChar(MsgLine), AnsiString(MsgMemo.Lines[LineNum - 1]), MaxLen - 1);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.SslSmtpClientHeaderLine(
    Sender : TObject;
    Msg    : Pointer;
    Size   : Integer);
begin
    { This demonstrates how to add a line to the message header              }
    { Just detect one of the header lines and add text at the end of this   }
    { line. Use #13#10 to form a new line                                   }
    { Here we check for the From: header line and add a Comments: line      }
    { Cast properly in order to call the right overload in D2009            }
    if (StrLen(PAnsiChar(Msg)) > 0) and
       (StrLIComp(PAnsiChar(Msg), PAnsiChar('From:'), 5) = 0) then
        StrCat(PAnsiChar(Msg), PAnsiChar(#13#10'Comments: This is a test'));
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.ClearDisplayButtonClick(Sender: TObject);
begin
    DisplayMemo.Clear;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.ExceptionHandler(Sender: TObject; E: Exception);
begin
    Application.ShowException(E);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.SetupOAuth2;                                         { V8.65 }
begin
  // hardcode your Google or Microsoft client application account details
    IcsRestEmail1.RestEmailType := RestEmailMSSmtp;
    IcsRestEmail1.ClientId := '';
    IcsRestEmail1.ClientSecret := '';

// TEMP STUFF !!!
//    IcsRestEmail1.RestEmailType := RestEmailMSSmtp;
//    IcsRestEmail1.ClientId := 'xxxxxxxxx';
//    IcsRestEmail1.ClientSecret := 'xxxxxxxxxxx';

    if IcsRestEmail1.ClientId = '' then begin
        Display('OAuth2 authentication requires an client application account');
        Exit;
    end;
    IcsRestEmail1.Clear;
     if Length(PasswordEdit.Text) > 32 then
        IcsRestEmail1.RefrToken := PasswordEdit.Text;  // ideally we'd save it separately

    IcsRestEmail1.AccountHint := UsernameEdit.Text;        { V8.71 }
    IcsRestEmail1.MsUserAuth := OAuth2UserAuth.Text;       { V8.71 }
    if OAuth2ExtBrow.Checked then                          { V8.71 }
        IcsRestEmail1.OAAuthType := OAuthTypeWeb
    else
        IcsRestEmail1.OAAuthType := OAuthTypeEmbed;

    if Pos('gmail', SslSmtpClient.Host) > 0 then
        IcsRestEmail1.RestEmailType := RestEmailGoogle
    else if (Pos('office365', SslSmtpClient.Host) > 0) or
                (Pos('outlook', SslSmtpClient.Host) > 0) then
        IcsRestEmail1.RestEmailType := RestEmailMSSmtp
    else
        Display('Can not use OAuth2 Authentication with this SMTP Server');
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Connect to the mail server }
procedure TSslSmtpTestForm.ConnectButtonClick(Sender: TObject);
begin
    FAllInOneFlag              := False;
    SslSmtpClient.Host         := HostEdit.Text;
    SslSmtpClient.Port         := PortEdit.Text;
    SslSmtpClient.HdrPriority  := TSmtpPriority(PriorityComboBox.ItemIndex);
    SslSmtpClient.AuthType     := TSmtpAuthType(AuthComboBox.ItemIndex);        { V8.65 }
    SslSmtpClient.Username     := UsernameEdit.Text;                            { V8.65 }
    SslSmtpClient.Password     := PasswordEdit.Text;                            { V8.65 }
    if SslSmtpClient.AuthType in [smtpAuthXOAuth2, smtpAuthOAuthBearer] then
        SetupOAuth2;                                                            { V8.65 }
    SslSmtpClient.WSDebugOptions := DefWSDebugClient;                           { V9.6 what we want to log }
    //  [wsdlogConnWS, wsdlogSsl, wsdlogHdr, wsdlogCerts, wsdlogErrs];
    SslSmtpClient.SslType      := TSmtpSslType(SmtpSslModeComboBox.ItemIndex);
    if SslSmtpClient.SslType <> smtpTlsNone then begin
        if (KeyEdit.Text <> '') or (CertEdit.Text <> '') then begin
            if (NOT (FileExists (KeyEdit.Text) and FileExists (CertEdit.Text))) then begin  { V9.6 sanity check }
                Display('Client SSL certificate and key must exist, if specified and required');
                Exit;
            end;
            SslContext1.SslPrivKeyFile := KeyEdit.Text;
            SslContext1.SslPassPhrase  := PassPhraseEdit.Text;
            SslContext1.SslCertFile    := CertEdit.Text;
        end;
        SslContext1.UseSharedCAStore := True;  { V9.6 }
        SslContext1.SslCAFile      := CAFileEdit.Text; // no longer needed
        SslContext1.SslCAPath      := CAPathEdit.Text;
        SslContext1.SslVerifyPeer  := VerifyPeerCheckBox.Checked;
        if SslContext1.SslVerifyPeer then begin
            SslSmtpClient.SslCertVerMethod := CertVerBundle;   { V9.6 }
            SslSmtpClient.OnSslVerifyPeer := SslSmtpClientSslVerifyPeer;
            SslSmtpClient.OnSslHandshakeDone := SslSmtpClientSslHandshakeDone;
        end
        else begin
            SslSmtpClient.OnSslVerifyPeer := nil;
            SslSmtpClient.OnSslHandshakeDone := nil;
        end;
    end;
    SslSmtpClient.Connect;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Send HELO command with our local identification }
procedure TSslSmtpTestForm.HeloButtonClick(Sender: TObject);
begin
    FAllInOneFlag                 := FALSE;
    SslSmtpClient.SignOn          := SignOnEdit.Text;
    SslSmtpClient.Helo;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.EhloButtonClick(Sender: TObject);
begin
    FAllInOneFlag                 := FALSE;
    SslSmtpClient.SignOn          := SignOnEdit.Text;
    SslSmtpClient.Ehlo;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.AuthButtonClick(Sender: TObject);
begin
    FAllInOneFlag                 := FALSE;
    SslSmtpClient.Username        := UsernameEdit.Text;
    SslSmtpClient.Password        := PasswordEdit.Text;
    SslSmtpClient.AuthType        := TSmtpAuthType(AuthComboBox.ItemIndex);
    SslSmtpClient.Auth;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ change Authentication Type, clear OAuth2 token }
procedure TSslSmtpTestForm.AuthComboBoxChange(Sender: TObject);
begin
    if NOT (TSmtpAuthType (AuthComboBox.ItemIndex) in [smtpAuthXOAuth2, smtpAuthOAuthBearer]) then begin
        if Length(PasswordEdit.Text) > 32 then PasswordEdit.Text := '';  // clear old token
    end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ If smtpAuthNone is seleted then Open combines methods Connect and Helo.   }
{  If any other authentication type is selected then Open combines methods  }
{  Connect, Ehlo and Auth.                                                  }
procedure TSslSmtpTestForm.OpenButtonClick(Sender: TObject);
begin
    FAllInOneFlag                 := FALSE;
    //SslSmtpClient.MailMessage     := MsgMemo.Lines;

  { V8.70 Assign property MailMessage and unassign OnGetData if you need }
  { automatic encoding and line wrapping                           }
    if UseMailMessageCheckBox.Checked then begin
        SslSmtpClient.MailMessage        := MsgMemo.Lines;
        SslSmtpClient.OnGetData          := nil;
        SslSmtpClient.WrapMsgMaxLineLen  := StrToIntDef(WrapAtEdit.Text, 76);
        SslSmtpClient.WrapMessageText    := WrapTextCheckBox.Checked;
    end
    else begin
        SslSmtpClient.OnGetData   := SslSmtpClientGetData;
        SslSmtpClient.MailMessage.Clear;
    end;

 { V8.70 character set stuff copied from non-SSL sample }
    SslSmtpClient.CharSet            := IcsCharsetComboBox1.Charset;        { generally UFF-8 }
    SslSmtpClient.ConvertToCharset   := ConvertToCharsetCheckBox.Checked;   { always converts for Unicode compilers }
    SslSmtpClient.DefaultEncoding    := TSmtpDefaultEncoding(DefEncodingComboBox.ItemIndex);
    SslSmtpClient.Allow8bitChars     := Allow8BitCheckBox.Checked;          { must be off to allow inline encoding }
    SslSmtpClient.FoldHeaders        := FoldHeadersCheckBox.Checked;

    SslSmtpClient.Host            := HostEdit.Text;
    SslSmtpClient.Port            := PortEdit.Text;
    SslSmtpClient.SignOn          := SignOnEdit.Text;
    SslSmtpClient.Username        := UsernameEdit.Text;
    SslSmtpClient.Password        := PasswordEdit.Text;
    SslSmtpClient.AuthType        := TSmtpAuthType(AuthComboBox.ItemIndex);
    if SslSmtpClient.AuthType in [smtpAuthXOAuth2, smtpAuthOAuthBearer] then
        SetupOAuth2;                                                            { V8.65 }
    SslSmtpClient.WSDebugOptions := [wsdlogConnWS, wsdlogSsl, wsdlogHdr, wsdlogCerts, wsdlogErrs];   { V9.6 what we want to log }
    SslSmtpClient.HdrPriority     := TSmtpPriority(PriorityComboBox.ItemIndex);
    SslSmtpClient.SslType         := TSmtpSslType(SmtpSslModeComboBox.ItemIndex);
    if SslSmtpClient.SslType <> smtpTlsNone then begin
        if (KeyEdit.Text <> '') or (CertEdit.Text <> '') then begin
            if (NOT (FileExists (KeyEdit.Text) and FileExists (CertEdit.Text))) then begin  { V9.6 sanity check }
                Display('Client SSL certificate and key must exist, if specified and required');
                Exit;
            end;
            SslContext1.SslPrivKeyFile := KeyEdit.Text;
            SslContext1.SslPassPhrase  := PassPhraseEdit.Text;
            SslContext1.SslCertFile    := CertEdit.Text;
        end;
        SslContext1.UseSharedCAStore := True;  { V9.6 }
        SslContext1.SslCAFile      := CAFileEdit.Text; // no longer needed
        SslContext1.SslCAPath      := CAPathEdit.Text;
        SslContext1.SslVerifyPeer  := VerifyPeerCheckBox.Checked;
        if SslContext1.SslVerifyPeer then begin
            SslSmtpClient.SslCertVerMethod := CertVerBundle;   { V9.6 }
            SslSmtpClient.OnSslVerifyPeer := SslSmtpClientSslVerifyPeer;
            SslSmtpClient.OnSslHandshakeDone := SslSmtpClientSslHandshakeDone;
        end
        else begin
            SslSmtpClient.OnSslVerifyPeer := nil;
            SslSmtpClient.OnSslHandshakeDone := nil;
        end;
    end;
    SslSmtpClient.Open;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Send originator }
procedure TSslSmtpTestForm.MailFromButtonClick(Sender: TObject);
begin
    FAllInOneFlag                 := FALSE;
    SslSmtpClient.FromName        := FromEdit.Text;
    SslSmtpClient.MailFrom;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Send recipients }
procedure TSslSmtpTestForm.RcptToButtonClick(Sender: TObject);
begin
    FAllInOneFlag := FALSE;
    SslSmtpClient.RcptName.Clear;
    SslSmtpClient.RcptNameAdd(ToEdit.Text, CcEdit.Text, BccEdit.text);
    SslSmtpClient.RcptTo;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ Send text and attached files to mail server }
procedure TSslSmtpTestForm.DataButtonClick(Sender: TObject);
begin
    FAllInOneFlag                 := FALSE;

  { V8.70 Assign property MailMessage and unassign OnGetData if you need }
  { automatic encoding and line wrapping                           }
    if UseMailMessageCheckBox.Checked then begin
        SslSmtpClient.MailMessage        := MsgMemo.Lines;
        SslSmtpClient.OnGetData          := nil;
        SslSmtpClient.WrapMsgMaxLineLen  := StrToIntDef(WrapAtEdit.Text, 76);
        SslSmtpClient.WrapMessageText    := WrapTextCheckBox.Checked;
    end
    else begin
        SslSmtpClient.OnGetData   := SslSmtpClientGetData;
        SslSmtpClient.MailMessage.Clear;
    end;

 { V8.70 character set stuff copied from non-SSL sample }
    SslSmtpClient.CharSet            := IcsCharsetComboBox1.Charset;
    SslSmtpClient.ConvertToCharset   := ConvertToCharsetCheckBox.Checked;
    SslSmtpClient.DefaultEncoding    := TSmtpDefaultEncoding(DefEncodingComboBox.ItemIndex);
    SslSmtpClient.Allow8bitChars     := Allow8BitCheckBox.Checked;
    SslSmtpClient.FoldHeaders        := FoldHeadersCheckBox.Checked;

    SslSmtpClient.RcptName.Clear;
    SslSmtpClient.RcptNameAdd(ToEdit.Text, CcEdit.Text, BccEdit.text);
    SslSmtpClient.HdrFrom         := FromEdit.Text;
    SslSmtpClient.HdrTo           := ToEdit.Text;
    SslSmtpClient.HdrCc           := CcEdit.Text;
    SslSmtpClient.HdrSubject      := SubjectEdit.Text;
    SslSmtpClient.EmailFiles      := FileAttachMemo.Lines;
    SslSmtpClient.SendMode        := smtpCopyToStream;
    SslSmtpClient.Data;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ MailFrom, RcptTo and Data methods combined }
procedure TSslSmtpTestForm.MailButtonClick(Sender: TObject);
begin
    FAllInOneFlag                 := FALSE;

  { V8.70 Assign property MailMessage and unassign OnGetData if you need }
  { automatic encoding and line wrapping                           }
    if UseMailMessageCheckBox.Checked then begin
        SslSmtpClient.MailMessage        := MsgMemo.Lines;
        SslSmtpClient.OnGetData          := nil;
        SslSmtpClient.WrapMsgMaxLineLen  := StrToIntDef(WrapAtEdit.Text, 76);
        SslSmtpClient.WrapMessageText    := WrapTextCheckBox.Checked;
    end
    else begin
        SslSmtpClient.OnGetData   := SslSmtpClientGetData;
        SslSmtpClient.MailMessage.Clear;
    end;

 { V8.70 character set stuff copied from non-SSL sample }
    SslSmtpClient.CharSet            := IcsCharsetComboBox1.Charset;        { generally UFF-8 }
    SslSmtpClient.ConvertToCharset   := ConvertToCharsetCheckBox.Checked;   { always converts for Unicode compilers }
    SslSmtpClient.DefaultEncoding    := TSmtpDefaultEncoding(DefEncodingComboBox.ItemIndex);
    SslSmtpClient.Allow8bitChars     := Allow8BitCheckBox.Checked;          { must be off to allow inline encoding }
    SslSmtpClient.FoldHeaders        := FoldHeadersCheckBox.Checked;

    SslSmtpClient.RcptName.Clear;
    SslSmtpClient.RcptNameAdd(ToEdit.Text, CcEdit.Text, BccEdit.text);
    SslSmtpClient.HdrFrom         := FromEdit.Text;
    SslSmtpClient.HdrTo           := ToEdit.Text;
    SslSmtpClient.HdrCc           := CcEdit.Text;
    SslSmtpClient.HdrSubject      := SubjectEdit.Text;
    SslSmtpClient.SignOn          := SignOnEdit.Text;
    SslSmtpClient.FromName        := FromEdit.Text;
    SslSmtpClient.EmailFiles      := FileAttachMemo.Lines;
    SslSmtpClient.Host            := HostEdit.Text;
    SslSmtpClient.Port            := PortEdit.Text;
    //SslSmtpClient.SendMode        := smtpCopyToStream;
    SslSmtpClient.Mail;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.QuitButtonClick(Sender: TObject);
begin
    FAllInOneFlag                 := FALSE;
    SslSmtpClient.Quit;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.AbortButtonClick(Sender: TObject);
begin
    FAllInOneFlag := FALSE;
    SslSmtpClient.Abort;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.SslSmtpClientRequestDone(
    Sender : TObject;
    RqType : TSmtpRequest;
    Error  : Word);
begin
    { For every operation, we display the status }
    if (Error > 0) and  (Error < 10000) then
        Display('RequestDone Rq=' + IntToStr(Ord(RqType)) + ' Error='+ SslSmtpClient.ErrorMessage)
    else
        Display('RequestDone Rq=' + IntToStr(Ord(RqType)) + ' Error='+ IntToStr(Error));
    { Check if the user has asked for "All-In-One" demo }
    if not FAllInOneFlag then
        Exit;             { No, nothing more to do here }
    { We are in "All-In-One" demo, start next operation }
    { But first check if previous one was OK            }
    if Error <> 0 then begin
        FAllInOneFlag := FALSE;   { Terminate All-In-One demo }
        Display('Error, stoped All-In-One demo');
        Exit;
    end;
    case RqType of
    smtpConnect:  begin
                      if SslSmtpClient.AuthType = smtpAuthNone then
                          SslSmtpClient.Helo
                      else
                          SslSmtpClient.Ehlo;
                  end;
    smtpHelo:     SslSmtpClient.MailFrom;
    smtpEhlo: if SslSmtpClient.SslType = smtpTlsExplicit then begin
                  Inc(FEhloCount);
                  if FEhloCount = 1 then
                      SslSmtpClient.StartTls
                  else if FEhloCount > 1 then
                      SslSmtpClient.Auth;
              end
              else
                  SslSmtpClient.Auth;
    smtpStartTls: SslSmtpClient.Ehlo; // We need to re-issue the Ehlo command
    smtpAuth:     SslSmtpClient.MailFrom;
    smtpMailFrom: SslSmtpClient.RcptTo;
    smtpRcptTo:   SslSmtpClient.Data;
    smtpData:     SslSmtpClient.Quit;
    smtpQuit:     Display('All-In-One done !');
    end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.AllInOneButtonClick(Sender: TObject);
begin
    if SslSmtpClient.Connected then begin
        MessageBeep(MB_OK);
        Display('All-In-One demo started in connected state.');
        Display('Please quit or abort the connection first.');
        Exit;
    end;

    FAllInOneFlag               := TRUE;

  { V8.70 Assign property MailMessage and unassign OnGetData if you need }
  { automatic encoding and line wrapping                           }
    if UseMailMessageCheckBox.Checked then begin
        SslSmtpClient.MailMessage        := MsgMemo.Lines;
        SslSmtpClient.OnGetData          := nil;
        SslSmtpClient.WrapMsgMaxLineLen  := StrToIntDef(WrapAtEdit.Text, 76);
        SslSmtpClient.WrapMessageText    := WrapTextCheckBox.Checked;
    end
    else begin
        SslSmtpClient.OnGetData   := SslSmtpClientGetData;
        SslSmtpClient.MailMessage.Clear;
    end;

 { V8.70 character set stuff copied from non-SSL sample }
    SslSmtpClient.CharSet            := IcsCharsetComboBox1.Charset;        { generally UFF-8 }
    SslSmtpClient.ConvertToCharset   := ConvertToCharsetCheckBox.Checked;   { always converts for Unicode compilers }
    SslSmtpClient.DefaultEncoding    := TSmtpDefaultEncoding(DefEncodingComboBox.ItemIndex);
    SslSmtpClient.Allow8bitChars     := Allow8BitCheckBox.Checked;          { must be off to allow inline encoding }
    SslSmtpClient.FoldHeaders        := FoldHeadersCheckBox.Checked;

    FEhloCount                  := 0;
    { Initialize all SMTP component properties from our GUI }
    SslSmtpClient.Host          := HostEdit.Text;
    SslSmtpClient.Port          := PortEdit.Text;
    SslSmtpClient.SignOn        := SignOnEdit.Text;
    SslSmtpClient.FromName      := FromEdit.Text;
    SslSmtpClient.HdrFrom       := FromEdit.Text;
    SslSmtpClient.HdrTo         := ToEdit.Text;
    SslSmtpClient.HdrCc         := CcEdit.Text;
    SslSmtpClient.HdrSubject    := SubjectEdit.Text; { + #13#10#9 + ' Testing continuation line !'};
    SslSmtpClient.EmailFiles    := FileAttachMemo.Lines;
    SslSmtpClient.AuthType      := TSmtpAuthType(AuthComboBox.ItemIndex);
    if SslSmtpClient.AuthType in [smtpAuthXOAuth2, smtpAuthOAuthBearer] then
        SetupOAuth2;                                                            { V8.65 }
    SslSmtpClient.WSDebugOptions := [wsdlogConnWS, wsdlogSsl, wsdlogHdr, wsdlogCerts, wsdlogErrs];   { V9.6 what we want to log }
    SslSmtpClient.SslType       := TSmtpSslType(SmtpSslModeComboBox.ItemIndex);
    if SslSmtpClient.SslType <> smtpTlsNone then begin
        if (KeyEdit.Text <> '') or (CertEdit.Text <> '') then begin
            if (NOT (FileExists (KeyEdit.Text) and FileExists (CertEdit.Text))) then begin  { V9.6 sanity check }
                Display('Client SSL certificate and key must exist, if specified and required');
                Exit;
            end;
            SslContext1.SslPrivKeyFile := KeyEdit.Text;
            SslContext1.SslPassPhrase  := PassPhraseEdit.Text;
            SslContext1.SslCertFile    := CertEdit.Text;
        end;
        SslContext1.UseSharedCAStore := True;  { V9.6 }
        SslContext1.SslCAFile      := CAFileEdit.Text; // no longer needed
        SslContext1.SslCAPath      := CAPathEdit.Text;
        SslContext1.SslVerifyPeer  := VerifyPeerCheckBox.Checked;
        if SslContext1.SslVerifyPeer then begin
            SslSmtpClient.SslCertVerMethod := CertVerBundle;   { V9.6 }
            SslSmtpClient.OnSslVerifyPeer := SslSmtpClientSslVerifyPeer;
            SslSmtpClient.OnSslHandshakeDone := SslSmtpClientSslHandshakeDone;
        end
        else begin
            SslSmtpClient.OnSslVerifyPeer := nil;
            SslSmtpClient.OnSslHandshakeDone := nil;
        end;
    end;

    SslSmtpClient.Username       := UsernameEdit.Text;
    SslSmtpClient.Password       := PasswordEdit.Text;
    SslSmtpClient.HdrPriority    := TSmtpPriority(PriorityComboBox.ItemIndex);
    { Recipient list is computed from To, Cc and Bcc fields }
    SslSmtpClient.RcptName.Clear;
    SslSmtpClient.RcptNameAdd(ToEdit.Text, CcEdit.Text, BccEdit.text);
    Display('Connecting to SMTP server...');
    { Start first operation to do to send an email          }
    { Next operations are started from OnRequestDone event  }
    SslSmtpClient.Connect;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.SslSmtpClientBeforeFileOpen(Sender: TObject;
  Idx: Integer; FileName: String; var Action: TSmtpBeforeOpenFileAction);
begin
    Action := smtpBeforeOpenFileNone;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.TlsButtonClick(Sender: TObject);
begin
    FAllInOneFlag := FALSE;
    SslSmtpClient.StartTls;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.WMSslReconnect(var Msg: TMessage);
begin
    OpenButtonClick(Self);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.SslSmtpClientSslVerifyPeer(Sender: TObject;
    var Ok  : Integer;
    Cert    : TX509Base);
begin
 {   Display(          // V9.6 done in WSocket
            'Received certificate - Depth: ' +
            IntToStr(Cert.VerifyDepth)+ #13#10 +
            'Subject: "' + Cert.SubjectOneLine + '"'#13#10 +
            'Issuer:  "' + Cert.IssuerOneLine + '"'#13#10  +
            'Verify result: ' + Cert.VerifyErrMsg);    }
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.SslSmtpClientSslHandshakeDone(Sender: TObject;
  ErrCode: Word; PeerCert: TX509Base; var Disconnect: Boolean);
begin
 {   if (ErrCode = 0) then begin    // V9.6 done in WSocket
        if PeerCert.PostConnectionCheck(SslSmtpClient.Host) then
            Display('! Post connection check ok')
        else begin
            Display('! Post connection check **failed** - ' +
                                  'Hostname "' + SslSmtpClient.Host +
                                  '" not found in peer certificate');
            Disconnect := TRUE;
        end;
    end;    }
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ called by SMTP component to when it needs an OAuth2 token }
procedure TSslSmtpTestForm.SslSmtpClientGetNewToken(Sender: TObject);           { V8.65 }
begin
    if NOT IcsRestEmail1.GetNewToken(True) then   // allow interaction, waits for broweser window to be completed
        Display('Failed to get OAuth2 Bearer Token')
    else begin
        if (Pos (IcsRestEmail1.NewAccEmail, SslSmtpClient.UserName) = 0) and
                                              (IcsRestEmail1.NewAccEmail <> '') then
            Display('OAuth2 Token for Wrong Account, Expected: ' +
                 SslSmtpClient.Username + ', Got: ' + IcsRestEmail1.NewAccEmail)
        else  begin
            SslSmtpClient.OAuthToken := IcsRestEmail1.AccToken;
            SslSmtpClient.TokenExpireDT := IcsRestEmail1.AccExpireDT;
            Display('Got New OAuth2 Bearer Token OK');
        end;
    end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ called by TIcsRestEmail when it has a new token we should save }
procedure TSslSmtpTestForm.IcsRestEmail1EmailNewToken(Sender: TObject);         { V8.65 }
begin
    if IcsRestEmail1.RefrToken <> '' then begin
        PasswordEdit.Text := IcsRestEmail1.RefrToken;
        Display('Saved OAuth2 Refresh Token');
    end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSslSmtpTestForm.IcsRestEmail1EmailProg(Sender: TObject;              { V8.65 }
  LogOption: TLogOption; const Msg: string);
begin
    Display(Msg);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ called by TIcsRestEmail to display the embedded browser window }
procedure TSslSmtpTestForm.IcsRestEmail1OAEmbed(Sender: TObject; const URL: string; var Success: Boolean);    { V8.71 }
begin
    Success := OAuthBrowser1.BrowserEmbedded((Sender as TRestOAuth), Self, URL);
end;

{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
end.

