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

Read iCloud Email Account using IMAP

See more IMAP Examples

Demonstrates how to set the IMAP settings for an iCloud email account and downloads the email from Inbox.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oEmail
LOCAL i
LOCAL n
LOCAL nBUid

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 the iCloud IMAP Mail Server
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.mail.me.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  The username is usually the name part of your iCloud email address 
//  (for example, emilyparker, not emilyparker@icloud.com).
nSuccess := oImap:Login("ICLOUD_USERNAME", "ICLOUD_PASSWORD")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

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

//  Once the folder/mailbox is selected, the NumMessages property
//  will contain the number of emails in the mailbox.
//  Loop from 1 to NumMessages to fetch each email by sequence number.

oEmail := CreateObject("Chilkat.Email")

n := oImap:NumMessages
nBUid := 0
FOR i := 1 TO n

    //  Download the email by sequence number.
    nSuccess := oImap:FetchEmail(0, i, nBUid, oEmail)
    IF (nSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oEmail:destroy()
        RETURN
    ENDIF

    ? Str(i) + ": " + oEmail:From
    ? "    " + oEmail:Subject
    ? "-"
NEXT

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

? "Success."

//  Sample output:

//  	1: iCloud <noreply@email.apple.com>
//  	    Welcome to iCloud Mail.
//  	-
//  	2: "Chilkat Software" <support@chilkatsoft.com>
//  	    This is a test
//  	-
//  	Success.

oImap:destroy()
oEmail:destroy()