Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
oleobject loo_Email
string ls_MimeText
integer li_Seen
integer li_Flagged
integer li_Answered
integer li_Draft

li_Success = 0

//  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.

loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Imap.Ssl = 1
loo_Imap.Port = 993

li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login("user@example.com","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

//  Build the email to be uploaded.
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

loo_Email.Subject = "Meeting agenda"
loo_Email.From = "Alice <alice@example.com>"
li_Success = loo_Email.AddTo("Bob","bob@example.com")
loo_Email.Body = "Let's meet at 10am to review the agenda."

ls_MimeText = loo_Email.GetMime()
if loo_Email.LastMethodSuccess = 0 then
    Write-Debug loo_Email.LastErrorText
    destroy loo_Imap
    destroy loo_Email
    return
end if

//  Choose the initial flags.  Named variables make each argument's meaning clear at the call.
li_Seen = 1
li_Flagged = 0
li_Answered = 0
li_Draft = 0

//  Upload to the Inbox already marked as read (\Seen), with the other flags unset.
li_Success = loo_Imap.AppendMimeWithFlags("Inbox",ls_MimeText,li_Seen,li_Flagged,li_Answered,li_Draft)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Email
    return
end if

Write-Debug "Appended UID: " + string(loo_Imap.AppendUid)

li_Success = loo_Imap.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Email
    return
end if



destroy loo_Imap
destroy loo_Email