Sample code for 30+ languages & platforms
Java

Get an Attached Message as an Email Object

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachedEmail method, which copies the Nth embedded message/rfc822 MIME part into another Email object. The index is zero-based (valid values run from 0 through NumAttachedMessages - 1). This example attaches an email and then extracts it back into its own object.

Background: When a message forwards another email as an attachment, that nested message is a complete email in its own right. GetAttachedEmail turns it back into a fully navigable Email object so you can read its subject, sender, body, and even its own attachments — essential for processing forwarded mail, or for tools that unpack reported spam/phishing to examine the original.

Chilkat Java Downloads

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

public class ChilkatExample {

    public static void main(String[] args) {
        // Demonstrates the GetAttachedEmail method, which copies the Nth embedded message/rfc822
        // MIME part into another Email object.  The index is zero-based.

        // Build an inner email and attach it to an outer email.
        Email innerEmail = new Email();
        innerEmail.setSubject("Embedded message");
        innerEmail.setFrom("alice@example.com");
        innerEmail.setBody("This is the embedded message.");

        Email email = new Email();
        email.setSubject("Has an attached message");
        try {
            email.attachEmail(innerEmail);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Copy the first embedded message (index 0) into its own Email object.
        Email attached = new Email();
        try {
            email.getAttachedEmail(0, attached);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Attached email subject: " + attached.getSubject());
        System.out.println("Attached email from: " + attached.getFrom());
    }
}