Sample code for 30+ languages & platforms
Java

Create a multipart/related MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.NewMultipartRelated method, which initializes the object as an empty multipart/related entity. It takes no arguments; the primary part and its related resources are then appended.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: multipart/related ties a primary part to the resources it references — most commonly an HTML body and the inline images it embeds via cid: references. Unlike a plain attachment, a related image is displayed within the message rather than listed separately. The HTML part comes first, followed by each referenced resource with a matching Content-ID.

Chilkat Java Downloads

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

public class ChilkatExample {

    public static void main(String[] args) {
        // Demonstrates the Mime.NewMultipartRelated method, which initializes the object as an empty
        // multipart/related entity, clearing any existing content.  It takes no arguments.

        Mime mime = new Mime();

        try {
            mime.newMultipartRelated();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // multipart/related groups a primary part with the resources it references, such as an HTML body
        // and the inline images it displays.
        Mime htmlPart = new Mime();
        try {
            htmlPart.setBodyFromHtml("<html><body><img src=\"cid:logo\"></body></html>");

            mime.appendPart(htmlPart);

            mime.appendPartFromFile("qa_data/logo.png");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Number of parts: " + mime.getNumParts());
    }
}