Sample code for 30+ languages & platforms
Lianja

Append MIME with Initial IMAP Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMimeWithFlags method, which uploads a MIME message and sets its initial system flags. The first argument is the mailbox and the second is the MIME text. The third through sixth arguments control \Seen, \Flagged, \Answered, and \Draft respectively — pass true to set a flag. This example uploads a message that is already marked read.

Background: When importing or archiving mail you frequently want the uploaded message to arrive in a particular state — already read, pre-flagged, or marked as a draft. Setting the flags at append time is a single operation, avoiding a second STORE round trip. These explicit arguments take precedence over the AppendSeen property.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

//  Demonstrates the Imap.AppendMimeWithFlags method, which uploads a MIME message and sets its
//  initial system flags.  The 1st argument is the mailbox, the 2nd is the MIME text, and the
//  3rd through 6th arguments set the \Seen, \Flagged, \Answered, and \Draft flags.

loImap = createobject("CkImap")

loImap.Ssl = .T.
loImap.Port = 993

llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llSuccess = loImap.Login("user@example.com","myPassword")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

//  Build the email to be uploaded.
loEmail = createobject("CkEmail")
loEmail.Subject = "Meeting agenda"
loEmail.From = "Alice <alice@example.com>"
llSuccess = loEmail.AddTo("Bob","bob@example.com")
loEmail.Body = "Let's meet at 10am to review the agenda."

lcMimeText = loEmail.GetMime()
if (loEmail.LastMethodSuccess = .F.) then
    ? loEmail.LastErrorText
    release loImap
    release loEmail
    return
endif

//  Choose the initial flags.  Named variables make each argument's meaning clear at the call.
llSeen = .T.
llFlagged = .F.
llAnswered = .F.
llDraft = .F.

//  Upload to the Inbox already marked as read (\Seen), with the other flags unset.
llSuccess = loImap.AppendMimeWithFlags("Inbox",lcMimeText,llSeen,llFlagged,llAnswered,llDraft)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loEmail
    return
endif

? "Appended UID: " + str(loImap.AppendUid)

llSuccess = loImap.Disconnect()
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loEmail
    return
endif



release loImap
release loEmail