Sample code for 30+ languages & platforms
Classic ASP

Fetch a MessageSet into an EmailBundle

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchMsgSet method, which downloads the messages identified by a MessageSet and appends them to an EmailBundle. The arguments are headersOnly, the MessageSet, and the EmailBundle (which is not cleared). This example searches for unseen messages and downloads the whole set.

Background: This is the natural pairing with QueryMbx: search to get a MessageSet of matching identifiers, then download exactly those messages in one call. Fetching a set is far more efficient than looping one message at a time, and it returns an EmailBundle you iterate with MessageCount and EmailAt. Pass headersOnly = true to pull just headers for a fast preview list.

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.FetchMsgSet method, which downloads the messages identified by a
'  MessageSet and appends them to an EmailBundle.  The 1st argument is headersOnly, the 2nd
'  is the MessageSet, and the 3rd is the EmailBundle (which is not cleared).

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

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

'  Find the messages to download (here, the unseen messages by UID).
set msgSet = Server.CreateObject("Chilkat.MessageSet")
success = imap.QueryMbx("UNSEEN",1,msgSet)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

'  Download the messages in the set (full bodies) into a bundle.
set bundle = Server.CreateObject("Chilkat.EmailBundle")
success = imap.FetchMsgSet(0,msgSet,bundle)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

n = bundle.MessageCount
Response.Write "<pre>" & Server.HTMLEncode( "Fetched " & n & " messages.") & "</pre>"
set email = Server.CreateObject("Chilkat.Email")

For i = 0 To n - 1
    success = bundle.EmailAt(i,email)
    Response.Write "<pre>" & Server.HTMLEncode( email.Subject) & "</pre>"
Next

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


%>
</body>
</html>