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

Lianja
llSuccess = .F.

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

loImap = createobject("CkImap")

//  Connect to an IMAP server.
//  Use TLS
loImap.Ssl = .T.
loImap.Port = 993
llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

//  Login
llSuccess = loImap.Login("***","***")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

//  Select an IMAP mailbox
llSuccess = loImap.SelectMailbox("Inbox")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

loEmail = createobject("CkEmail")

lnUid = 2014
llIsUid = .T.

llSuccess = loImap.FetchEmail(.F.,lnUid,llIsUid,loEmail)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loEmail
    return
endif

//  Display the From and Subject
? loEmail.FromAddress
? loEmail.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 ----"
? loEmail.Body
? "--------------------"

//  Display the recipients:

for j = 0 to loEmail.NumTo - 1
    ? loEmail.GetToName(j) + ", " + loEmail.GetToAddr(j)
next
for j = 0 to loEmail.NumCC - 1
    ? loEmail.GetCcName(j) + ", " + loEmail.GetCcAddr(j)
next

//  Show the total size of the email, including body and attachments:
? str(loEmail.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:
lnNumAttach = loImap.GetMailNumAttach(loEmail)
? str(lnNumAttach)

for j = 0 to lnNumAttach - 1
    ? loImap.GetMailAttachFilename(loEmail,j)
    lnAttachSize = loImap.GetMailAttachSize(loEmail,j)
    ? "    size = " + str(lnAttachSize) + " bytes"
next

? "--"

//  Disconnect from the IMAP server.
llSuccess = loImap.Disconnect()


release loImap
release loEmail