Sample code for 30+ languages & platforms
PureBasic

Append a MIME Message to an IMAP Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMime method, which uploads a complete RFC 822/MIME message to a mailbox. The first argument is the destination mailbox name and the second is the MIME text. This example obtains the MIME from an Email object and uploads it to Drafts.

Background: Use AppendMime when you already hold a message's raw source — a .eml file, a message you assembled, or one you received. Chilkat sends the text exactly as supplied, without normalizing line endings or re-encoding headers, so every header and MIME boundary is preserved verbatim. The supplied text must be plain text (no raw non-text binary bytes).

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkEmail.pb"
IncludeFile "CkImap.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Imap.AppendMime method, which uploads a complete MIME message to a mailbox.
    ;  The 1st argument is the destination mailbox name and the 2nd is the MIME text.

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)

    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"user@example.com","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  Build the email to be uploaded.
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "Meeting agenda")
    CkEmail::setCkFrom(email, "Alice <alice@example.com>")
    success = CkEmail::ckAddTo(email,"Bob","bob@example.com")
    CkEmail::setCkBody(email, "Let's meet at 10am to review the agenda.")

    ;  Obtain the MIME text to upload.  Here it is generated from the Email, but it could equally
    ;  be read from a .eml file.
    mimeText.s = CkEmail::ckGetMime(email)
    If CkEmail::ckLastMethodSuccess(email) = 0
        Debug CkEmail::ckLastErrorText(email)
        CkImap::ckDispose(imap)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    ;  Append (upload) the MIME to the "Drafts" mailbox.
    success = CkImap::ckAppendMime(imap,"Drafts",mimeText)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    Debug "Appended UID: " + Str(CkImap::ckAppendUid(imap))

    success = CkImap::ckDisconnect(imap)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf



    CkImap::ckDispose(imap)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure