Sample code for 30+ languages & platforms
Java

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

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    //  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.

    CkEmail email = new CkEmail();
    email.put_Subject("A message");
    email.put_Body("Some body text.");
    email.AddTo("Bob","bob@example.com");
    email.AddStringAttachment("notes.txt","Some notes.");

    System.out.println("NumTo before clear = " + email.get_NumTo());
    System.out.println("NumAttachments before clear = " + email.get_NumAttachments());

    //  Remove all content, resetting the email object.
    email.Clear();

    System.out.println("NumTo after clear = " + email.get_NumTo());
    System.out.println("NumAttachments after clear = " + email.get_NumAttachments());
  }
}