Sample code for 30+ languages & platforms
Visual Basic 6.0 Requires Chilkat v11.0.0+

Find an email by subject and fetch it by IMAP UID

See more Email Object Examples

Search the selected INBOX by subject and fetch the first matching UID into an Email. The example requests complete attachment bodies and uses peek mode to preserve the read flag. Configure your own IMAP TLS endpoint and credentials.

Chilkat Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim imap As New ChilkatImap
imap.Ssl = 1
imap.Port = 993
imap.RequireSslCertVerify = 1
imap.PeekMode = 1
imap.AutoDownloadAttachments = 1
success = imap.Connect("imap.example.com")
If (success <> 1) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

success = imap.Login("MAIL_USER","MAIL_PASSWORD")
If (success <> 1) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

success = imap.SelectMailbox("INBOX")
If (success <> 1) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

Dim matches As New MessageSet
success = imap.QueryMbx("SUBJECT ""Sample message""",1,matches)
If (success <> 1) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

If (matches.Count = 0) Then
    Debug.Print "No matching message in INBOX."
    success = imap.Disconnect()
    If (success <> 1) Then
        Debug.Print imap.LastErrorText
        Exit Sub
    End If

    Exit Sub
End If

'  The search returned UIDs. Use the same identifier type for the fetch.
Dim uid As Long
uid = matches.GetId(0)
Dim email As New ChilkatEmail
success = imap.FetchEmail(0,uid,1,email)
If (success <> 1) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

Debug.Print "Mailbox INBOX, UIDVALIDITY " & imap.UidValidity & ", UID " & uid
success = imap.Disconnect()
If (success <> 1) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

'  The downloaded Email remains usable after disconnecting.
Debug.Print "Subject: " & email.Subject & "  Files: " & email.NumAttachments

success = email.SaveEml("imap-received.eml")
If (success <> 1) Then
    Debug.Print email.LastErrorText
    Exit Sub
End If

Debug.Print "Saved imap-received.eml."