Sample code for 30+ languages & platforms
CkJava Requires Chilkat v11.0.0+

Read text and binary MIME parts by content type

See more Email Object Examples

Use the same wildcard and inclusion flags for both counting and retrieval. Request decoded text for textual parts and BinData for binary parts. Use GetAttachedEmail when the goal is to inspect a nested email as a message.

Chilkat CkJava Downloads

CkJava
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[])
  {
    boolean success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkEmail email = new CkEmail();
    success = email.LoadEml("workshop.eml");
    if (success != true) {
        System.out.println(email.lastErrorText());
        return;
        }

    String types = "*/*";
    boolean inlineOnly = false;
    boolean excludeAttachments = false;
    int count = email.GetNumPartsOfType(types,inlineOnly,excludeAttachments);
    CkStringBuilder mediaType = new CkStringBuilder();
    int i = 0;
    while ((i < count)) {
        mediaType.SetString(email.getNthContentType(i,types,inlineOnly,excludeAttachments));
        if (mediaType.StartsWith("text/",false) == true) {
            String partText = email.getNthTextPartOfType(i,types,inlineOnly,excludeAttachments);
            if (email.get_LastMethodSuccess() != true) {
                System.out.println(email.lastErrorText());
                return;
                }

            System.out.println(mediaType.getAsString() + "  " + partText);
            }
        else {
            CkBinData data = new CkBinData();
            success = email.GetNthBinaryPartOfTypeBd(i,types,inlineOnly,excludeAttachments,data);
            if (success != true) {
                System.out.println(email.lastErrorText());
                return;
                }

            System.out.println(mediaType.getAsString() + "  " + data.get_NumBytes() + " bytes");
            }

        i = i+1;
        }
  }
}