B4X Requires Chilkat v11.0.0+
B4X
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 B4X Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim imap As ChilkatImap
imap.Initialize("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
Log(imap.LastErrorText)
Return
End If
success = imap.Login("MAIL_USER", "MAIL_PASSWORD")
If success <> True Then
Log(imap.LastErrorText)
Return
End If
success = imap.SelectMailbox("INBOX")
If success <> True Then
Log(imap.LastErrorText)
Return
End If
Dim matches As ChilkatMessageSet
matches.Initialize
success = imap.QueryMbx("SUBJECT " & Chr(34) & "Sample message" & Chr(34), True, matches)
If success <> True Then
Log(imap.LastErrorText)
Return
End If
If matches.Count = 0 Then
Log("No matching message in INBOX.")
success = imap.Disconnect
If success <> True Then
Log(imap.LastErrorText)
Return
End If
Return
End If
' The search returned UIDs. Use the same identifier type for the fetch.
Dim uid As Long = matches.GetId(0)
Dim email As ChilkatEmail
email.Initialize
success = imap.FetchEmail(False, uid, True, email)
If success <> True Then
Log(imap.LastErrorText)
Return
End If
Log("Mailbox INBOX, UIDVALIDITY " & imap.UidValidity & ", UID " & uid)
success = imap.Disconnect
If success <> True Then
Log(imap.LastErrorText)
Return
End If
' The downloaded Email remains usable after disconnecting.
Log("Subject: " & email.Subject & " Files: " & email.NumAttachments)
success = email.SaveEml("imap-received.eml")
If success <> True Then
Log(email.LastErrorText)
Return
End If
Log("Saved imap-received.eml.")