Sample code for 30+ languages & platforms
PureBasic

Clear an Email Object

See more Email Object Examples

Demonstrates the Chilkat Email.Clear method, which removes the current message content — recipients, headers, body representations, attachments, related items, and embedded messages — leaving an empty email object. This example builds a message with a recipient and attachment, clears it, and prints the counts before and after.

Background: Reusing a single Email object across many operations is efficient, but leftover state from a previous message could leak into the next. Clear resets the object completely, giving you a clean slate — the safe way to start a fresh message without allocating a new object.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    ;  Demonstrates the Clear method, which removes the current message content, including
    ;  recipients, headers, body representations, attachments, related items, and embedded
    ;  messages -- leaving an empty email object.

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

    CkEmail::setCkSubject(email, "A message")
    CkEmail::setCkBody(email, "Some body text.")
    CkEmail::ckAddTo(email,"Bob","bob@example.com")
    CkEmail::ckAddStringAttachment(email,"notes.txt","Some notes.")

    Debug "NumTo before clear = " + Str(CkEmail::ckNumTo(email))
    Debug "NumAttachments before clear = " + Str(CkEmail::ckNumAttachments(email))

    ;  Remove all content, resetting the email object.
    CkEmail::ckClear(email)

    Debug "NumTo after clear = " + Str(CkEmail::ckNumTo(email))
    Debug "NumAttachments after clear = " + Str(CkEmail::ckNumAttachments(email))


    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure