Sample code for 30+ languages & platforms
Xojo Plugin 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 Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim imap As New Chilkat.Imap
imap.Ssl = True
imap.Port = 993
imap.RequireSslCertVerify = True
imap.PeekMode = True
imap.AutoDownloadAttachments = True
success = imap.Connect("imap.example.com")
If (success <> True) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

success = imap.Login("MAIL_USER","MAIL_PASSWORD")
If (success <> True) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

success = imap.SelectMailbox("INBOX")
If (success <> True) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

Dim matches As New Chilkat.MessageSet
success = imap.QueryMbx("SUBJECT ""Sample message""",True,matches)
If (success <> True) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

If (matches.Count = 0) Then
    System.DebugLog("No matching message in INBOX.")
    success = imap.Disconnect()
    If (success <> True) Then
        System.DebugLog(imap.LastErrorText)
        Return
    End If

    Return
End If

//  The search returned UIDs. Use the same identifier type for the fetch.
Dim uid As UInt32
uid = matches.GetId(0)
Dim email As New Chilkat.Email
success = imap.FetchEmail(False,uid,True,email)
If (success <> True) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

System.DebugLog("Mailbox INBOX, UIDVALIDITY " + Str(imap.UidValidity) + ", UID " + Str(uid))
success = imap.Disconnect()
If (success <> True) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

//  The downloaded Email remains usable after disconnecting.
System.DebugLog("Subject: " + email.Subject + "  Files: " + Str(email.NumAttachments))

success = email.SaveEml("imap-received.eml")
If (success <> True) Then
    System.DebugLog(email.LastErrorText)
    Return
End If

System.DebugLog("Saved imap-received.eml.")