Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

Fetch Single Email by UID or Sequence Number

Assuming the UID is known, download a single email by UID from an IMAP mail server.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oEmail
LOCAL nUid
LOCAL nIsUid
LOCAL j
LOCAL nNumAttach
LOCAL nAttachSize

nSuccess := 0

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

oImap := CreateObject("Chilkat.Imap")

//  Connect to an IMAP server.
//  Use TLS
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Login
nSuccess := oImap:Login("***", "***")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

oEmail := CreateObject("Chilkat.Email")

nUid := 2014
nIsUid := 1

nSuccess := oImap:FetchEmail(0, nUid, nIsUid, oEmail)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oEmail:destroy()
    RETURN
ENDIF

//  Display the From and Subject
? oEmail:FromAddress
? oEmail:Subject

//  Display the Body property, which is the default body.
//  If an email has an HTML body, the Body property contains
//  the HTML source.  Otherwise it contains the plain-text
//  body.
? "---- EMAIL BODY ----"
? oEmail:Body
? "--------------------"

//  Display the recipients:

FOR j := 0 TO oEmail:NumTo - 1
    ? oEmail:GetToName(j) + ", " + oEmail:GetToAddr(j)
NEXT
FOR j := 0 TO oEmail:NumCC - 1
    ? oEmail:GetCcName(j) + ", " + oEmail:GetCcAddr(j)
NEXT

//  Show the total size of the email, including body and attachments:
? Str(oEmail:Size)

//  When a full email is downloaded, we would use the
//  email.NumAttachments property in conjunction with the
//  email.GetMailAttach* methods.
//  However, when an email object contains only the header,
//  we need to access the attachment info differently:
nNumAttach := oImap:GetMailNumAttach(oEmail)
? Str(nNumAttach)

FOR j := 0 TO nNumAttach - 1
    ? oImap:GetMailAttachFilename(oEmail, j)
    nAttachSize := oImap:GetMailAttachSize(oEmail, j)
    ? "    size = " + Str(nAttachSize) + " bytes"
NEXT

? "--"

//  Disconnect from the IMAP server.
nSuccess := oImap:Disconnect()

oImap:destroy()
oEmail:destroy()