Sample code for 30+ languages & platforms
PureBasic

Render an Email to MIME in a StringBuilder

See more SMTP Examples

Demonstrates the Chilkat MailMan.RenderToMimeSb method, which renders an Email object as MIME text and appends the result to a StringBuilder, without sending the email. This example renders a message into a StringBuilder and prints it.

Background: This is the StringBuilder version of RenderToMime. Appending into a StringBuilder is more efficient when the MIME is large or when you plan to further inspect or manipulate it — searching, replacing, or accumulating additional text — without creating extra intermediate string copies.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the MailMan.RenderToMimeSb method, which renders an Email object as MIME text
    ;  and appends the result to a StringBuilder (without sending the email).

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

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

    CkEmail::setCkSubject(email, "Rendered email")
    CkEmail::setCkFrom(email, "alice@example.com")
    CkEmail::ckAddTo(email,"Bob","bob@example.com")
    CkEmail::setCkBody(email, "This message is rendered to MIME in a StringBuilder.")

    ;  Render the MIME into a StringBuilder.
    sbMime.i = CkStringBuilder::ckCreate()
    If sbMime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkMailMan::ckRenderToMimeSb(mailman,email,sbMime)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        CkEmail::ckDispose(email)
        CkStringBuilder::ckDispose(sbMime)
        ProcedureReturn
    EndIf

    Debug CkStringBuilder::ckGetAsString(sbMime)


    CkMailMan::ckDispose(mailman)
    CkEmail::ckDispose(email)
    CkStringBuilder::ckDispose(sbMime)


    ProcedureReturn
EndProcedure