Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi DLL) IMAP Read Encrypted EmailSee more IMAP ExamplesDemonstrates how to read encrypted email from an IMAP mailbox. Reading encrypted email works the same as reading non-encrypted email. If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly. Information about the original encrypted state of the email is available after it has been downloaded and decrypted. Note: This example requires Chilkat v10.1.2 or later because it uses the Secrets class.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, MessageSet, Imap, Cert, Email, JsonObject, Secrets; ... procedure TForm1.Button1Click(Sender: TObject); var imap: HCkImap; success: Boolean; secrets: HCkSecrets; json: HCkJsonObject; password: PWideChar; messageSet: HCkMessageSet; uid: Cardinal; email: HCkEmail; cert: HCkCert; begin imap := CkImap_Create(); CkImap_putSsl(imap,True); CkImap_putPort(imap,993); success := CkImap_Connect(imap,'imap.example2.com'); if (success <> True) then begin Memo1.Lines.Add(CkImap__lastErrorText(imap)); Exit; end; // We'll get the IMAP email account's password from the Apple Keychain or Windows Credentials Manager. // See how we originally saved the email credentials to the Keychain here: // Save Email Credentials in Apple Keychain or Windows Credentials Manager secrets := CkSecrets_Create(); // On Windows, this is the Windows Credentials Manager // On MacOS/iOS, it is the Apple Keychain CkSecrets_putLocation(secrets,'local_manager'); // Specify the name of the secret. // service and username are required. // appName and domain are optional. // Note: The values are arbitrary and can be anything you want. json := CkJsonObject_Create(); CkJsonObject_UpdateString(json,'appName','MyEmailApp'); CkJsonObject_UpdateString(json,'service','IMAP'); CkJsonObject_UpdateString(json,'domain','example2.com'); CkJsonObject_UpdateString(json,'username','jane@example2.com'); password := CkSecrets__getSecretStr(secrets,json); if (CkSecrets_getLastMethodSuccess(secrets) = False) then begin Memo1.Lines.Add(CkSecrets__lastErrorText(secrets)); Exit; end; success := CkImap_Login(imap,'jane@example2.com',password); if (success <> True) then begin Memo1.Lines.Add(CkImap__lastErrorText(imap)); Exit; end; // Select an IMAP mailbox success := CkImap_SelectMailbox(imap,'Inbox'); if (success <> True) then begin Memo1.Lines.Add(CkImap__lastErrorText(imap)); Exit; end; // This example: Send Encrypted Email using Certificate in Apple Keychain // sent an email with the subject "This email is encrypted". // Let's download an email with the word "encrypted" in the subject. messageSet := CkImap_Search(imap,'SUBJECT encrypted',True); if (CkImap_getLastMethodSuccess(imap) = False) then begin Memo1.Lines.Add(CkImap__lastErrorText(imap)); Exit; end; if (CkMessageSet_getCount(messageSet) = 0) then begin Memo1.Lines.Add('No messages found.'); CkMessageSet_Dispose(messageSet); Exit; end; // Reading encrypted email works the same as reading non-encrypted email. // If the required certificate and private key are available on the system (e.g., in the macOS Keychain or Windows Certificate Store), // Chilkat will automatically locate them, decrypt the email, and handle the process seamlessly. uid := CkMessageSet_GetId(messageSet,0); email := CkImap_FetchSingle(imap,uid,True); if (CkImap_getLastMethodSuccess(imap) = False) then begin Memo1.Lines.Add(CkImap__lastErrorText(imap)); CkMessageSet_Dispose(messageSet); Exit; end; // Here we can show if the email was received encrypted, if it was successfully decrypted, and // which certificate was used to decrypt. Memo1.Lines.Add('Email received encrypted: ' + IntToStr(Ord(CkEmail_getReceivedEncrypted(email)))); // Was it successfully decrypted? Memo1.Lines.Add('Successfully decrypted: ' + IntToStr(Ord(CkEmail_getDecrypted(email)))); // What cert was used to decrypt? Memo1.Lines.Add('Encrypted by: ' + CkEmail__encryptedBy(email)); cert := CkEmail_GetEncryptedByCert(email); if (CkEmail_getLastMethodSuccess(email) = True) then begin Memo1.Lines.Add('Certificate DN: ' + CkCert__subjectDN(cert)); CkCert_Dispose(cert); end; // Show the decrypted email body. Memo1.Lines.Add(CkEmail__body(email)); CkMessageSet_Dispose(messageSet); CkEmail_Dispose(email); CkImap_Disconnect(imap); CkImap_Dispose(imap); CkSecrets_Dispose(secrets); CkJsonObject_Dispose(json); end; |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.