![]() |
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) Scan for Emails with Attachments and Save Attachments to FilesScan for emails with attachments and save attachments. Note: This example requires Chilkat v11.0.0 or greater.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, EmailBundle, Imap, Email, MessageSet; ... procedure TForm1.Button1Click(Sender: TObject); var success: Boolean; imap: HCkImap; fetchUids: Boolean; messageSet: HCkMessageSet; bundle: HCkEmailBundle; headersOnly: Boolean; fullEmail: HCkEmail; emailHeader: HCkEmail; i: Integer; numAttach: Integer; uidStr: PWideChar; uid: Integer; j: Integer; filename: 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; // Login 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; // Fetch the email headers into a bundle object: bundle := CkEmailBundle_Create(); headersOnly := True; success := CkImap_FetchMsgSet(imap,headersOnly,messageSet,bundle); if (success = False) then begin Memo1.Lines.Add(CkImap__lastErrorText(imap)); Exit; end; // Scan for emails with attachments, and save the attachments // to a sub-directory. fullEmail := CkEmail_Create(); emailHeader := CkEmail_Create(); i := 0; while i < CkEmailBundle_getMessageCount(bundle) do begin // The bundle contains email headers.. CkEmailBundle_EmailAt(bundle,i,emailHeader); // Does this email have attachments? // Use GetMailNumAttach because the attachments // are not actually in the email object because // we only downloaded headers. numAttach := CkImap_GetMailNumAttach(imap,emailHeader); if (numAttach > 0) then begin // Download the entire email and save the // attachments. (Remember, we // need to download the entire email because // only the headers were previously downloaded. // The ckx-imap-uid header field is added when // headers are downloaded. This makes it possible // to get the UID from the email object. uidStr := CkEmail__getHeaderField(emailHeader,'ckx-imap-uid'); uid := StrToInt(uidStr); success := CkImap_FetchEmail(imap,False,uid,True,fullEmail); if (success = False) then begin Memo1.Lines.Add(CkImap__lastErrorText(imap)); Exit; end; success := CkEmail_SaveAllAttachments(fullEmail,'attachmentsDir'); for j := 0 to numAttach - 1 do begin filename := CkImap__getMailAttachFilename(imap,emailHeader,j); Memo1.Lines.Add(filename); end; end; i := i + 1; end; // Disconnect from the IMAP server. success := CkImap_Disconnect(imap); CkImap_Dispose(imap); CkMessageSet_Dispose(messageSet); CkEmailBundle_Dispose(bundle); CkEmail_Dispose(fullEmail); CkEmail_Dispose(emailHeader); end; |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.