Sample code for 30+ languages & platforms
Visual Basic 6.0

Get IMAP UID from Email Header

See more IMAP Examples

Demonstrates how to get the IMAP UID from an email header. After fetching an email or header using IMAP, the UID is contained in the ckx-imap-uid header field.

Chilkat Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

' This example requires the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.

Dim imap As New ChilkatImap

' Connect to an IMAP server.
' Use TLS
imap.Ssl = 1
imap.Port = 993
success = imap.Connect("imap.example.com")
If (success = 0) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

' Login
success = imap.Login("login","password")
If (success = 0) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

' Select an IMAP mailbox
success = imap.SelectMailbox("Inbox")
If (success = 0) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

Dim fetchUids As Long
fetchUids = 1
' Get the message UIDs of all the emails in the mailbox

Dim messageSet As New MessageSet
success = imap.QueryMbx("ALL",fetchUids,messageSet)
If (success = 0) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

Dim bundle As New ChilkatEmailBundle
Dim headersOnly As Long
headersOnly = 1
success = imap.FetchMsgSet(headersOnly,messageSet,bundle)
If (success = 0) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

' The UID of each fetched email header is available 
' in the ckx-imap-uid header field.
Dim email As New ChilkatEmail
Dim msgSet1 As New MessageSet
Dim i As Long
i = 0
Dim szBundle As Long
szBundle = bundle.MessageCount
Do While i < szBundle
    success = bundle.EmailAt(i,email)

    ' Build a message set containing one UID
    Dim uidStr As String
    uidStr = email.GetHeaderField("ckx-imap-uid")
    success = msgSet1.FromCompactString(uidStr)

    ' Move this message to some other folder.
    success = imap.MoveMessages(msgSet1,"someOtherFolder")
    If (success <> 1) Then
        Debug.Print imap.LastErrorText
        ' Exit the loop...
        i = szBundle
    End If

    i = i + 1
Loop

' Disconnect from the IMAP server.
success = imap.Disconnect()