Constructing MHTML files in Java using JavaMail API

Apparently, there is no library for creating MHTML files. (If you know one, let me know.)

However, this can be easily performed using JavaMail API, as demonstrated in the following example. It assumes there is an image named pooh.jpg in its working directory and produces example.mhtml containing a very simple web page with the image.

MHTMLExample.java java
/*
    Author: Petr Pudlak, 2011, http://petr.pudlak.name/contact/
 
    This program is free software: you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or (at your
    option) any later version.
 
    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
    License for more details.
 
    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
import java.util.*;
 
/**
 * Example how to construct a MHT (AKA MHTML) file in Java using JavaMail
 * API. Creates a simple HTML page (from a String) with an image, which is
 * pulled from a file. Requires mail.jar in the classpath (and also Java
 * Activation Framework if JDK version is less than 1.6).
 *
 * The program expects a file named <em>pooh.jpg</em> in the current directory
 * and produces a very simple MHT archive to stdout.
 */
public class MHTMLExample
{
    public static void main(String argv[]) throws Exception
    {
        // get system properties
        Properties props = new Properties();
        // System.getProperties();
        // create a new session
        Session session = Session.getInstance(props, null);
        // construct a MIME message message
        MimeMessage message = new MimeMessage(session);
        MimeMultipart mpart = new MimeMultipart("related");
        String htmlPage =
            "<html><head><title>Example</title></head>" +
            "<body><p>ExampleExampleSome text..." +
            "<img src='pooh.jpg'></p></body>";
        mpart.addBodyPart(bodyPart(
            new StringSource("text/html", "index.html", htmlPage) ));
        mpart.addBodyPart(bodyPart(new FileDataSource("pooh.jpg")));
        message.setContent(mpart);
        // the subject is displayed as the window title in the browser
        message.setSubject("MHTML example");
        // one can set the URL of the original page:
        message.addHeader("Content-Location", "index.html");
 
        // Save to example.mhtml
        FileOutputStream out = new FileOutputStream("example.mhtml");
        message.writeTo(out);
        out.close();
    }
    static BodyPart bodyPart(DataSource ds) throws MessagingException
    {
        MimeBodyPart body = new MimeBodyPart();
        DataHandler dh = new DataHandler(ds);
        body.setDisposition("inline");
        body.setDataHandler(dh);
        body.setFileName(dh.getName());
        // the URL of the file; we set it simply to its name
        body.addHeader("Content-Location", dh.getName());
        return body;
    }
 
    /**
     * A simple in-memory implementation of {@link DataSource}.
     */
    static final class StringSource implements DataSource
    {
        private final String contentType;
        private final String name;
        private final byte[] data;
        public StringSource(String contentType, String name, String data)
        {
            this.contentType = contentType;
            this.data = data.getBytes();
            this.name = name;
        }
        public String getContentType()
        {
            return contentType;
        }
        public OutputStream getOutputStream() throws IOException
        {
            throw new IOException();
        }
        public InputStream getInputStream() throws IOException
        {
            return new ByteArrayInputStream(data);
        }
        public String getName()
        {
            return name;
        }
    }
}
Poslední úprava: 2011/06/11 14:18