Sample code for 30+ languages & platforms
PureBasic

Append a Part to a Multipart MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.AppendPart method, which appends a copy of another Mime as the last direct child. The only argument is the source Mime. Chilkat copies it, so later changes to the source do not affect the appended part.

Background: This is the general way to build up a multipart entity from parts you construct yourself — a text body, an HTML body, a nested multipart. Because it appends a copy, you can reuse and modify the source object afterward, or append it more than once, without surprises. For attaching a file directly, AppendPartFromFile is more convenient.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkMime.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Mime.AppendPart method, which appends a copy of another Mime as the last direct
    ;  child.  The only argument is the Mime to append.  Chilkat copies it, so later changes to the
    ;  source do not affect the appended part.

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

    success = CkMime::ckNewMultipartMixed(mime)
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    ;  Build a part and append a copy of it.
    part.i = CkMime::ckCreate()
    If part.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkMime::ckSetBodyFromPlainText(part,"This is an appended part.")
    If success = 0
        Debug CkMime::ckLastErrorText(part)
        CkMime::ckDispose(mime)
        CkMime::ckDispose(part)
        ProcedureReturn
    EndIf

    success = CkMime::ckAppendPart(mime,part)
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        CkMime::ckDispose(part)
        ProcedureReturn
    EndIf

    Debug "Number of parts: " + Str(CkMime::ckNumParts(mime))


    CkMime::ckDispose(mime)
    CkMime::ckDispose(part)


    ProcedureReturn
EndProcedure