Sample code for 30+ languages & platforms
Classic ASP

Fetch a Chunk of Messages, Tracking Failures

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchChunk2 method, which downloads a run of messages by sequence number and reports which succeeded and which failed. The arguments are the starting sequence number, the count, a MessageSet that receives the failed IDs, a MessageSet that receives the successfully-fetched IDs, and the EmailBundle. This example fetches the first chunk of messages and reports the success and failure counts.

Background: When bulk-downloading a large mailbox, individual messages can occasionally fail to fetch (a corrupt message on the server, a transient error) without failing the whole operation. FetchChunk2 is designed for that reality: it returns a partial EmailBundle plus two MessageSet objects so your code can log or retry exactly the messages that did not come through, rather than aborting the entire sync.

Chilkat Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  Demonstrates the Imap.FetchChunk2 method, which downloads a run of messages by sequence
'  number and reports which succeeded and which failed.  The 1st argument is the starting
'  sequence number, the 2nd is the count, the 3rd receives the failed IDs, the 4th receives
'  the successfully-fetched IDs, and the 5th is the EmailBundle.

set imap = Server.CreateObject("Chilkat.Imap")

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

success = imap.Login("user@example.com","myPassword")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

numMessages = imap.SelectMailbox("Inbox")
If (numMessages < 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

'  Fetch messages 1..25 (or fewer if the mailbox is smaller).
count = 25
If (count > numMessages) Then
    count = numMessages
End If

set failedSet = Server.CreateObject("Chilkat.MessageSet")
set fetchedSet = Server.CreateObject("Chilkat.MessageSet")
set bundle = Server.CreateObject("Chilkat.EmailBundle")
success = imap.FetchChunk2(1,count,failedSet,fetchedSet,bundle)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Fetched: " & fetchedSet.Count) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Failed:  " & failedSet.Count) & "</pre>"

success = imap.Disconnect()
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If


%>
</body>
</html>