/*
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
ExampleExampleSome text..." + "
"; 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; } } }