All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mentawai.mail.HtmlEmail Maven / Gradle / Ivy

Go to download

A Java full-stack web framework with programmatic configuration instead of XML and Annotations.

There is a newer version: 2.5.6
Show newest version
/*
 * Copyright 2001-2004 The Apache Software Foundation
 *
 * Licensed 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.mentawai.mail;


import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.activation.DataHandler;
import javax.activation.URLDataSource;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

import org.mentawai.util.StringUtils;

// Revision: 191951

/**
 * An HTML multipart email.
 *
 * 

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. * *

This class also inherits from MultiPartEmail, so it is easy to * add attachents to the email. * *

To send an email in HTML, one should create a HtmlEmail, then * use the setFrom, addTo, etc. methods. The HTML content can be set * with the setHtmlMsg method. The alternate text content can be set * with setTextMsg. * *

Either the text or HTML can be omitted, in which case the "main" * part of the multipart becomes whichever is supplied rather than a * multipart/alternative. * * @author Regis Koenig * @author Sean Legassick * @version $Id: HtmlEmail.java,v 1.1 2006/03/02 17:11:41 soliveira Exp $ */ public class HtmlEmail extends MultiPartEmail { /** Defintion of the length of generated CID's */ public static final int CID_LENGTH = 10; /** * Text part of the message. This will be used as alternative text if * the email client does not support HTML messages. */ protected String text; /** Html part of the message */ protected String html; /** Embeded images */ protected List inlineImages = new ArrayList(); public HtmlEmail() { super(); } /** * @return List the list of destinataries or null if not exist */ public List getToList(){ return new ArrayList(super.toList); } /** * @return List the list of cc or null if not exist */ public List getCcList(){ return new ArrayList(super.ccList); } /** * @return List the list of bcc or null if not exist */ public List getBccList(){ return new ArrayList(super.bccList); } /** * @return List the list of reply or null if not exist */ public List getReplyList(){ return new ArrayList(super.replyList); } /** * Set the text content. * * @param aText A String. * @return An HtmlEmail. * @throws EmailException see javax.mail.internet.MimeBodyPart * for defintions */ public HtmlEmail setTextMsg(String aText) throws EmailException { if (StringUtils.isEmpty(aText)) { throw new EmailException("The message cannot be empty."); } this.text = aText; return this; } /** * Set the HTML content. * * @param aHtml A String. * @return An HtmlEmail. * @throws EmailException see javax.mail.internet.MimeBodyPart * for defintions */ public HtmlEmail setHtmlMsg(String aHtml) throws EmailException { if (StringUtils.isEmpty(aHtml)) { throw new EmailException("The message cannot be empty."); } this.html = aHtml; return this; } /** * Set the message. * *

This method overrides the MultiPartEmail setMsg() method in * order to send an HTML message instead of a full text message in * the mail body. The message is formatted in HTML for the HTML * part of the message, it is let as is in the alternate text * part. * * @param msg A String. * @return An Email. * @throws EmailException see javax.mail.internet.MimeBodyPart * for defintions */ public Email setMsg(String msg) throws EmailException { if (StringUtils.isEmpty(msg)) { throw new EmailException("The message cannot be empty."); } setTextMsg(msg); setHtmlMsg( new StringBuffer().append("

").append(msg).append("
").toString()); return this; } /** * Embeds an URL in the HTML. * *

This method allows to embed a file located by an URL into * the mail body. It allows, for instance, to add inline images * to the email. Inline files may be referenced with a * cid:xxxxxx URL, where xxxxxx is the Content-ID * returned by the embed function. * *

Example of use:

     * HtmlEmail he = new HtmlEmail();
     * he.setHtmlMsg("<html><img src=cid:" +
     *  embed("file:/my/image.gif","image.gif") +
     *  "></html>");
     * // code to set the others email fields (not shown)
     * 
* * @param url The URL of the file. * @param name The name that will be set in the filename header * field. * @return A String with the Content-ID of the file. * @throws EmailException when URL suplpied is invalid * also see javax.mail.internet.MimeBodyPart for defintions */ public String embed(URL url, String name) throws EmailException { // verify that the URL is valid try { InputStream is = url.openStream(); is.close(); } catch (IOException e) { throw new EmailException("Invalid URL"); } MimeBodyPart mbp = new MimeBodyPart(); try { mbp.setDataHandler(new DataHandler(new URLDataSource(url))); mbp.setFileName(name); mbp.setDisposition("inline"); String cid = RandomStringUtils.randomAlphabetic(HtmlEmail.CID_LENGTH).toLowerCase(); mbp.addHeader("Content-ID", "<" + cid + ">"); this.inlineImages.add(mbp); return cid; } catch (MessagingException me) { throw new EmailException(me); } } /** * Does the work of actually sending the email. * * @exception EmailException if there was an error. */ public void send() throws EmailException { try { // if the email has attachments then the base type is mixed, // otherwise it should be related if (this.isBoolHasAttachments()) { this.buildAttachments(); } else { this.buildNoAttachments(); } } catch (MessagingException me) { throw new EmailException(me); } super.send(); } /** * @throws EmailException EmailException * @throws MessagingException MessagingException */ private void buildAttachments() throws MessagingException, EmailException { MimeMultipart container = this.getContainer(); MimeMultipart subContainer = null; MimeMultipart subContainerHTML = new MimeMultipart("related"); BodyPart msgHtml = null; BodyPart msgText = null; container.setSubType("mixed"); subContainer = new MimeMultipart("alternative"); if (StringUtils.isNotEmpty(this.text)) { msgText = new MimeBodyPart(); subContainer.addBodyPart(msgText); if (StringUtils.isNotEmpty(this.charset)) { msgText.setContent( this.text, Email.TEXT_PLAIN + "; charset=" + this.charset); } else { msgText.setContent(this.text, Email.TEXT_PLAIN); } } if (StringUtils.isNotEmpty(this.html)) { if (this.inlineImages.size() > 0) { msgHtml = new MimeBodyPart(); subContainerHTML.addBodyPart(msgHtml); } else { msgHtml = new MimeBodyPart(); subContainer.addBodyPart(msgHtml); } if (StringUtils.isNotEmpty(this.charset)) { msgHtml.setContent( this.html, Email.TEXT_HTML + "; charset=" + this.charset); } else { msgHtml.setContent(this.html, Email.TEXT_HTML); } Iterator iter = this.inlineImages.iterator(); while (iter.hasNext()) { subContainerHTML.addBodyPart(iter.next()); } } // add sub containers to message this.addPart(subContainer, 0); if (this.inlineImages.size() > 0) { // add sub container to message this.addPart(subContainerHTML, 1); } } /** * @throws EmailException EmailException * @throws MessagingException MessagingException */ private void buildNoAttachments() throws MessagingException, EmailException { MimeMultipart container = this.getContainer(); MimeMultipart subContainerHTML = new MimeMultipart("related"); container.setSubType("alternative"); BodyPart msgText = null; BodyPart msgHtml = null; if (StringUtils.isNotEmpty(this.text)) { msgText = this.getPrimaryBodyPart(); if (StringUtils.isNotEmpty(this.charset)) { msgText.setContent( this.text, Email.TEXT_PLAIN + "; charset=" + this.charset); } else { msgText.setContent(this.text, Email.TEXT_PLAIN); } } if (StringUtils.isNotEmpty(this.html)) { // if the txt part of the message was null, then the html part // will become the primary body part if (msgText == null) { msgHtml = getPrimaryBodyPart(); } else { if (this.inlineImages.size() > 0) { msgHtml = new MimeBodyPart(); subContainerHTML.addBodyPart(msgHtml); } else { msgHtml = new MimeBodyPart(); container.addBodyPart(msgHtml, 1); } } if (StringUtils.isNotEmpty(this.charset)) { msgHtml.setContent( this.html, Email.TEXT_HTML + "; charset=" + this.charset); } else { msgHtml.setContent(this.html, Email.TEXT_HTML); } Iterator iter = this.inlineImages.iterator(); while (iter.hasNext()) { subContainerHTML.addBodyPart(iter.next()); } if (this.inlineImages.size() > 0) { // add sub container to message this.addPart(subContainerHTML); } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy