Sample code for 30+ languages & platforms
B4X

Fetch a Single IMAP Message into an Email Object

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchEmail method, which downloads one message (or its headers) into an Email object. The arguments are headerOnly, the message id, bUid (UID vs sequence number), and the Email that receives the message. This example fetches message 1 in full and reads its subject and from.

Background: FetchEmail parses a downloaded message into a fully navigable Email object, giving you its subject, sender, body, and attachments. Passing headerOnly = true downloads just the headers — a fast way to build a message-list preview — after which you can fetch the full message on demand. Because a mailbox must be selected first, and fetching a full message can set the \Seen flag, use ExamineMailbox instead of SelectMailbox when you must not change read state.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

'  Demonstrates the Imap.FetchEmail method, which downloads one message (or its headers) into
'  an Email object.  The 1st argument is headerOnly, the 2nd is the message id, the 3rd
'  (bUid) selects UID vs sequence number, and the 4th is the Email that receives the message.

Dim imap As ChilkatImap
imap.Initialize("imap")

imap.Ssl = True
imap.Port = 993

success = imap.Connect("imap.example.com")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If

success = imap.Login("user@example.com", "myPassword")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


success = imap.SelectMailbox("Inbox")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


'  Download message sequence number 1 in full.  headerOnly=False, bUid=False.
Dim email As ChilkatEmail
email.Initialize
success = imap.FetchEmail(False, 1, False, email)
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


Log("Subject: " & email.Subject)
Log("From: " & email.From)

success = imap.Disconnect
If success = False Then
    Log(imap.LastErrorText)
    Return
End If