|
Commons Email example source code file (HtmlEmail.java)
This example Commons Email source code file (HtmlEmail.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Commons Email HtmlEmail.java source code
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.mail;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
/**
* An HTML multipart email.
*
* <p>This class is used to send HTML formatted email. A text message
* can also be set for HTML unaware email clients, such as text-based
* email clients.
*
* <p>This class also inherits from {@link MultiPartEmail}, so it is easy to
* add attachments to the email.
*
* <p>To send an email in HTML, one should create a HtmlEmail , then
* use the {@link #setFrom(String)}, {@link #addTo(String)} etc. methods.
* The HTML content can be set with the {@link #setHtmlMsg(String)} method. The
* alternative text content can be set with {@link #setTextMsg(String)}.
*
* <p>Either the text or HTML can be omitted, in which case the "main"
* part of the multipart becomes whichever is supplied rather than a
* <code>multipart/alternative.
*
* <h3>Embedding Images and Media
*
* <p>It is also possible to embed URLs, files, or arbitrary
* <code>DataSources directly into the body of the mail:
* <pre>
* HtmlEmail he = new HtmlEmail();
* File img = new File("my/image.gif");
* PNGDataSource png = new PNGDataSource(decodedPNGOutputStream); // a custom class
* StringBuffer msg = new StringBuffer();
* msg.append("<html><body>");
* msg.append("<img src=cid:").append(he.embed(img)).append(">");
* msg.append("<img src=cid:").append(he.embed(png)).append(">");
* msg.append("</body></html>");
* he.setHtmlMsg(msg.toString());
* // code to set the other email fields (not shown)
* </pre>
*
* <p>Embedded entities are tracked by their name, which for File s is
* the filename itself and for <code>URLs is the canonical path. It is
* an error to bind the same name to more than one entity, and this class will
* attempt to validate that for <code>Files and URL s. When
* embedding a <code>DataSource, the code uses the equals()
* method defined on the <code>DataSources to make the determination.
*
* @since 1.0
* @author <a href="mailto:unknown">Regis Koenig
* @author <a href="mailto:sean@informage.net">Sean Legassick
* @version $Id: HtmlEmail.java 785383 2009-06-16 20:36:22Z sgoeschl $
*/
public class HtmlEmail extends MultiPartEmail
{
/** Definition of the length of generated CID's */
public static final int CID_LENGTH = 10;
/** prefix for default HTML mail */
private static final String HTML_MESSAGE_START = "<html>";
/** suffix for default HTML mail */
private static final String HTML_MESSAGE_END = "</pre> |