![]() |
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
(DataFlex) 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.
Use ChilkatAx-win32.pkg Procedure Test Boolean iSuccess Handle hoImap Boolean iFetchUids Variant vMessageSet Handle hoMessageSet Variant vBundle Handle hoBundle Boolean iHeadersOnly Variant vFullEmail Handle hoFullEmail Variant vEmailHeader Handle hoEmailHeader Integer i Integer iNumAttach String sUidStr Integer iUid Integer j String sFilename String sTemp1 Move False To iSuccess // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. Get Create (RefClass(cComChilkatImap)) To hoImap If (Not(IsComObjectCreated(hoImap))) Begin Send CreateComObject of hoImap End // Connect to an IMAP server. // Use TLS Set ComSsl Of hoImap To True Set ComPort Of hoImap To 993 Get ComConnect Of hoImap "imap.example.com" To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoImap To sTemp1 Showln sTemp1 Procedure_Return End // Login Get ComLogin Of hoImap "myLogin" "myPassword" To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoImap To sTemp1 Showln sTemp1 Procedure_Return End // Select an IMAP mailbox Get ComSelectMailbox Of hoImap "Inbox" To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoImap To sTemp1 Showln sTemp1 Procedure_Return End // We can choose to fetch UIDs or sequence numbers. Move True To iFetchUids // Get the message IDs of all the emails in the mailbox Get Create (RefClass(cComChilkatMessageSet)) To hoMessageSet If (Not(IsComObjectCreated(hoMessageSet))) Begin Send CreateComObject of hoMessageSet End Get pvComObject of hoMessageSet to vMessageSet Get ComQueryMbx Of hoImap "ALL" iFetchUids vMessageSet To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoImap To sTemp1 Showln sTemp1 Procedure_Return End // Fetch the email headers into a bundle object: Get Create (RefClass(cComChilkatEmailBundle)) To hoBundle If (Not(IsComObjectCreated(hoBundle))) Begin Send CreateComObject of hoBundle End Move True To iHeadersOnly Get pvComObject of hoMessageSet to vMessageSet Get pvComObject of hoBundle to vBundle Get ComFetchMsgSet Of hoImap iHeadersOnly vMessageSet vBundle To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoImap To sTemp1 Showln sTemp1 Procedure_Return End // Scan for emails with attachments, and save the attachments // to a sub-directory. Get Create (RefClass(cComChilkatEmail)) To hoFullEmail If (Not(IsComObjectCreated(hoFullEmail))) Begin Send CreateComObject of hoFullEmail End Get Create (RefClass(cComChilkatEmail)) To hoEmailHeader If (Not(IsComObjectCreated(hoEmailHeader))) Begin Send CreateComObject of hoEmailHeader End Move 0 To i While (i < (ComMessageCount(hoBundle))) // The bundle contains email headers.. Get pvComObject of hoEmailHeader to vEmailHeader Get ComEmailAt Of hoBundle i vEmailHeader To iSuccess // Does this email have attachments? // Use GetMailNumAttach because the attachments // are not actually in the email object because // we only downloaded headers. Get pvComObject of hoEmailHeader to vEmailHeader Get ComGetMailNumAttach Of hoImap vEmailHeader To iNumAttach If (iNumAttach > 0) 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. Get ComGetHeaderField Of hoEmailHeader "ckx-imap-uid" To sUidStr Move (sUidStr) To iUid Get pvComObject of hoFullEmail to vFullEmail Get ComFetchEmail Of hoImap False iUid True vFullEmail To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoImap To sTemp1 Showln sTemp1 Procedure_Return End Get ComSaveAllAttachments Of hoFullEmail "attachmentsDir" To iSuccess For j From 0 To (iNumAttach - 1) Get pvComObject of hoEmailHeader to vEmailHeader Get ComGetMailAttachFilename Of hoImap vEmailHeader j To sFilename Showln sFilename Loop End Move (i + 1) To i Loop // Disconnect from the IMAP server. Get ComDisconnect Of hoImap To iSuccess End_Procedure |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.