Sample code for 30+ languages & platforms
Java

Get a Header Field of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachmentHeader method, which returns the value of a named header field of an attachment. The first argument is the zero-based attachment index and the second is the MIME header-field name. This example reads the Content-Disposition header of the first attachment.

Background: Each attachment is a MIME part with its own header block. Where GetAttachmentAttr extracts a single named parameter, GetAttachmentHeader returns the entire value of a header field — for example the full Content-Disposition: attachment; filename="notes.txt" line. That is useful for inspecting or debugging exactly how a part is described, including any custom X- headers a sender may have added.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Email;

public class ChilkatExample {

    public static void main(String[] args) {
        // Demonstrates the GetAttachmentHeader method, which returns the value of a header field
        // (by name) of an attachment.  The first argument is the zero-based attachment index and the second is the
        // MIME header-field name.

        Email email = new Email();
        email.setSubject("Attachment header field");

        email.addStringAttachment("notes.txt", "Some notes.");

        // Get the Content-Disposition header of the first attachment (index 0).
        String disp = email.getAttachmentHeader(0, "Content-Disposition");
        System.out.println("Attachment 0 Content-Disposition: " + disp);
    }
}