![]() |
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
(Classic ASP) 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.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <% success = 0 ' This example requires the Chilkat API to have been previously unlocked. ' See Global Unlock Sample for sample code. set imap = Server.CreateObject("Chilkat.Imap") ' Connect to an IMAP server. ' Use TLS imap.Ssl = 1 imap.Port = 993 success = imap.Connect("imap.example.com") If (success = 0) Then Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>" Response.End End If ' Login success = imap.Login("myLogin","myPassword") If (success = 0) Then Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>" Response.End End If ' Select an IMAP mailbox success = imap.SelectMailbox("Inbox") If (success = 0) Then Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>" Response.End End If ' We can choose to fetch UIDs or sequence numbers. fetchUids = 1 ' Get the message IDs of all the emails in the mailbox set messageSet = Server.CreateObject("Chilkat.MessageSet") success = imap.QueryMbx("ALL",fetchUids,messageSet) If (success = 0) Then Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>" Response.End End If ' Fetch the email headers into a bundle object: set bundle = Server.CreateObject("Chilkat.EmailBundle") headersOnly = 1 success = imap.FetchMsgSet(headersOnly,messageSet,bundle) If (success = 0) Then Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>" Response.End End If ' Scan for emails with attachments, and save the attachments ' to a sub-directory. set fullEmail = Server.CreateObject("Chilkat.Email") set emailHeader = Server.CreateObject("Chilkat.Email") i = 0 Do While i < bundle.MessageCount ' The bundle contains email headers.. success = bundle.EmailAt(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 = imap.GetMailNumAttach(emailHeader) If (numAttach > 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. uidStr = emailHeader.GetHeaderField("ckx-imap-uid") uid = CLng(uidStr) success = imap.FetchEmail(0,uid,1,fullEmail) If (success = 0) Then Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>" Response.End End If success = fullEmail.SaveAllAttachments("attachmentsDir") For j = 0 To numAttach - 1 filename = imap.GetMailAttachFilename(emailHeader,j) Response.Write "<pre>" & Server.HTMLEncode( filename) & "</pre>" Next End If i = i + 1 Loop ' Disconnect from the IMAP server. success = imap.Disconnect() %> </body> </html> |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.