![]() |
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
(Visual FoxPro) 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.
LOCAL lnSuccess LOCAL loImap LOCAL lnFetchUids LOCAL loMessageSet LOCAL loBundle LOCAL lnHeadersOnly LOCAL loFullEmail LOCAL loEmailHeader LOCAL i LOCAL lnNumAttach LOCAL lcUidStr LOCAL lnUid LOCAL j LOCAL lcFilename lnSuccess = 0 * This example requires the Chilkat API to have been previously unlocked. * See Global Unlock Sample for sample code. loImap = CreateObject('Chilkat.Imap') * Connect to an IMAP server. * Use TLS loImap.Ssl = 1 loImap.Port = 993 lnSuccess = loImap.Connect("imap.example.com") IF (lnSuccess = 0) THEN ? loImap.LastErrorText RELEASE loImap CANCEL ENDIF * Login lnSuccess = loImap.Login("myLogin","myPassword") IF (lnSuccess = 0) THEN ? loImap.LastErrorText RELEASE loImap CANCEL ENDIF * Select an IMAP mailbox lnSuccess = loImap.SelectMailbox("Inbox") IF (lnSuccess = 0) THEN ? loImap.LastErrorText RELEASE loImap CANCEL ENDIF * We can choose to fetch UIDs or sequence numbers. lnFetchUids = 1 * Get the message IDs of all the emails in the mailbox loMessageSet = CreateObject('Chilkat.MessageSet') lnSuccess = loImap.QueryMbx("ALL",lnFetchUids,loMessageSet) IF (lnSuccess = 0) THEN ? loImap.LastErrorText RELEASE loImap RELEASE loMessageSet CANCEL ENDIF * Fetch the email headers into a bundle object: loBundle = CreateObject('Chilkat.EmailBundle') lnHeadersOnly = 1 lnSuccess = loImap.FetchMsgSet(lnHeadersOnly,loMessageSet,loBundle) IF (lnSuccess = 0) THEN ? loImap.LastErrorText RELEASE loImap RELEASE loMessageSet RELEASE loBundle CANCEL ENDIF * Scan for emails with attachments, and save the attachments * to a sub-directory. loFullEmail = CreateObject('Chilkat.Email') loEmailHeader = CreateObject('Chilkat.Email') i = 0 DO WHILE i < loBundle.MessageCount * The bundle contains email headers.. loBundle.EmailAt(i,loEmailHeader) * Does this email have attachments? * Use GetMailNumAttach because the attachments * are not actually in the email object because * we only downloaded headers. lnNumAttach = loImap.GetMailNumAttach(loEmailHeader) IF (lnNumAttach > 0) THEN * 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. lcUidStr = loEmailHeader.GetHeaderField("ckx-imap-uid") lnUid = VAL(lcUidStr) lnSuccess = loImap.FetchEmail(0,lnUid,1,loFullEmail) IF (lnSuccess = 0) THEN ? loImap.LastErrorText RELEASE loImap RELEASE loMessageSet RELEASE loBundle RELEASE loFullEmail RELEASE loEmailHeader CANCEL ENDIF lnSuccess = loFullEmail.SaveAllAttachments("attachmentsDir") FOR j = 0 TO lnNumAttach - 1 lcFilename = loImap.GetMailAttachFilename(loEmailHeader,j) ? lcFilename NEXT ENDIF i = i + 1 ENDDO * Disconnect from the IMAP server. lnSuccess = loImap.Disconnect() RELEASE loImap RELEASE loMessageSet RELEASE loBundle RELEASE loFullEmail RELEASE loEmailHeader |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.