Sample code for 30+ languages & platforms
Delphi DLL

IMAP Download and Verify Signed (S/MIME) Email

See more IMAP Examples

Demonstrates how to download and verify digitally signed S/MIME email.

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, Imap, Cert, Email, MessageSet;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
imap: HCkImap;
fetchUids: Boolean;
messageSet: HCkMessageSet;
email: HCkEmail;
cert: HCkCert;
i: Integer;
uid: PWideChar;

begin
success := False;

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

imap := CkImap_Create();

// Connect to an IMAP server.
// Use TLS
CkImap_putSsl(imap,True);
CkImap_putPort(imap,993);
success := CkImap_Connect(imap,'imap.example.com');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

success := CkImap_Login(imap,'myLogin','myPassword');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

// Select an IMAP mailbox
success := CkImap_SelectMailbox(imap,'Inbox');
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

// We can choose to fetch UIDs or sequence numbers.
fetchUids := True;

// Get the message IDs of all the emails in the mailbox
messageSet := CkMessageSet_Create();
success := CkImap_QueryMbx(imap,'ALL',fetchUids,messageSet);
if (success = False) then
  begin
    Memo1.Lines.Add(CkImap__lastErrorText(imap));
    Exit;
  end;

email := CkEmail_Create();
cert := CkCert_Create();

i := 0;
while i < CkMessageSet_getCount(messageSet) do
  begin

    uid := CkMessageSet_GetId(messageSet,i);
    Memo1.Lines.Add('uid: ' + uid);

    success := CkImap_FetchEmail(imap,False,uid,True,email);
    if (success = False) then
      begin
        Memo1.Lines.Add(CkImap__lastErrorText(imap));
        Exit;
      end;

    // The security layers of signed and/or encrypted emails
    // are automatically "unwrapped" when loaded into
    // a Chilkat email object.
    // An application only needs to check to see if an email
    // was received signed or encrypted, and then examine
    // the success/failure.  For example:
    if (CkEmail_getReceivedSigned(email) = True) then
      begin

        Memo1.Lines.Add('This email was signed.');

        // Check to see if the signatures were verified.
        if (CkEmail_getSignaturesValid(email) = True) then
          begin
            Memo1.Lines.Add('Digital signature(s) verified.');
            Memo1.Lines.Add('Signer: ' + CkEmail__signedBy(email));

            // Get the certificate used for signing.
            success := CkEmail_LastSignerCert(email,0,cert);

            if (success = False) then
              begin
                Memo1.Lines.Add('Failed to get signing certificate object.');
              end
            else
              begin
                Memo1.Lines.Add('Signing cert: ' + CkCert__subjectCN(cert));
              end;

          end
        else
          begin
            Memo1.Lines.Add('Digital signature verification failed.');
          end;

      end;

    i := i + 1;
  end;

// Disconnect from the IMAP server.
success := CkImap_Disconnect(imap);

CkImap_Dispose(imap);
CkMessageSet_Dispose(messageSet);
CkEmail_Dispose(email);
CkCert_Dispose(cert);

end;