Sample code for 30+ languages & platforms
Java

Iterate Email Headers

Demonstrates how to iterate over the email header fields.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;

public class ChilkatExample {

    public static void main(String[] args) {
        Email email = new Email();

        // First, load an email from a file.
        // Note: an email object may be loaded from a file, or
        // downloaded directly from a POP3 or IMAP server...

        try {
            email.loadEml("testReceivedHdrs.eml");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // How many header fields?
        int n = email.getNumHeaderFields();
        if (n > 0) {
            // Display the name and value of each header:
            String name = "";
            String value = "";
            for (int i = 0; i < n; i++) {
                name = email.getHeaderFieldName(i);
                value = email.getHeaderFieldValue(i);
                System.out.println(i);
                System.out.println(name);
                System.out.println(value);
            }
        }
    }
}