Sample code for 30+ languages & platforms
VBScript

Fetch Only an IMAP Message's Headers

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchSingleHeaderAsMime method, which downloads and returns the MIME header block for one message, without downloading the body. If the second argument (bUID) is true, the first argument is a UID; otherwise it is a sequence number. This example fetches message 1's headers.

Background: Headers are tiny compared to a full message with attachments, so fetching just the header block is the fast, bandwidth-friendly way to build a message list — sender, subject, date — without pulling every body. It is also gentler on read state: retrieving only headers does not mark a message as seen. Download the full message later, on demand, when the user opens it.

Chilkat VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  Demonstrates the Imap.FetchSingleHeaderAsMime method, which downloads and returns the MIME
'  header block for one message, without downloading the body.  If the 2nd argument (bUID)
'  is 1, the 1st argument is a UID; otherwise it is a sequence number.

set imap = CreateObject("Chilkat.Imap")

imap.Ssl = 1
imap.Port = 993

success = imap.Connect("imap.example.com")
If (success = 0) Then
    outFile.WriteLine(imap.LastErrorText)
    WScript.Quit
End If

success = imap.Login("user@example.com","myPassword")
If (success = 0) Then
    outFile.WriteLine(imap.LastErrorText)
    WScript.Quit
End If

success = imap.SelectMailbox("Inbox")
If (success = 0) Then
    outFile.WriteLine(imap.LastErrorText)
    WScript.Quit
End If

'  Download only the MIME header block of message sequence number 1.  bUID = 0.
headers = imap.FetchSingleHeaderAsMime(1,0)
If (imap.LastMethodSuccess = 0) Then
    outFile.WriteLine(imap.LastErrorText)
    WScript.Quit
End If

outFile.WriteLine(headers)

success = imap.Disconnect()
If (success = 0) Then
    outFile.WriteLine(imap.LastErrorText)
    WScript.Quit
End If


outFile.Close