001: package org.apache.turbine.util.mail;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.net.URL;
020:
021: import javax.activation.DataHandler;
022: import javax.activation.URLDataSource;
023:
024: import javax.mail.MessagingException;
025: import javax.mail.internet.MimeBodyPart;
026: import javax.mail.internet.MimeMultipart;
027:
028: import org.apache.commons.lang.StringUtils;
029:
030: import org.apache.ecs.Document;
031: import org.apache.ecs.ElementContainer;
032: import org.apache.ecs.html.Body;
033: import org.apache.ecs.html.Html;
034: import org.apache.ecs.html.PRE;
035:
036: /**
037: * An HTML multipart email.
038: *
039: * <p>This class is used to send HTML formatted email. A text message
040: * can also be set for HTML unaware email clients, such as text-based
041: * email clients.
042: *
043: * <p>This class also inherits from MultiPartEmail, so it is easy to
044: * add attachents to the email.
045: *
046: * <p>To send an email in HTML, one should create a HtmlEmail, then
047: * use the setFrom, addTo, etc. methods. The HTML content can be set
048: * with the setHtmlMsg method. The alternate text content can be set
049: * with setTextMsg.
050: *
051: * <p>Either the text or HTML can be omitted, in which case the "main"
052: * part of the multipart becomes whichever is supplied rather than a
053: * multipart/alternative.
054: *
055: * @author <a href="mailto:unknown">Regis Koenig</a>
056: * @author <a href="mailto:sean@informage.net">Sean Legassick</a>
057: * @version $Id: HtmlEmail.java 264148 2005-08-29 14:21:04Z henning $
058: * @deprecated Use org.apache.commons.mail.HtmlEmail instead.
059: */
060: public class HtmlEmail extends MultiPartEmail {
061: protected MimeMultipart htmlContent;
062:
063: protected String text;
064: protected String html;
065:
066: /**
067: * Basic constructor.
068: *
069: * @exception MessagingException.
070: */
071: public HtmlEmail() throws MessagingException {
072: this .init();
073: }
074:
075: /**
076: * Instantiates a new MimeMultipart object if it isn't already
077: * instantiated.
078: *
079: * @return A MimeMultipart object
080: */
081: public MimeMultipart getHtmlContent() {
082: if (htmlContent == null) {
083: htmlContent = new MimeMultipart();
084: }
085: return htmlContent;
086: }
087:
088: /**
089: * Set the text content.
090: *
091: * @param text A String.
092: * @return An HtmlEmail.
093: * @exception MessagingException.
094: */
095: public HtmlEmail setTextMsg(String text) throws MessagingException {
096: this .text = text;
097: return this ;
098: }
099:
100: /**
101: * Set the HTML content.
102: *
103: * @param html A String.
104: * @return An HtmlEmail.
105: * @exception MessagingException.
106: */
107: public HtmlEmail setHtmlMsg(String html) throws MessagingException {
108: this .html = html;
109: return this ;
110: }
111:
112: /**
113: * Set the HTML content based on an ECS document.
114: *
115: * @param doc A Document.
116: * @return An HtmlEmail.
117: * @exception MessagingException.
118: */
119: public HtmlEmail setHtmlMsg(Document doc) throws MessagingException {
120: return setHtmlMsg(doc.toString());
121: }
122:
123: /**
124: * Set the message.
125: *
126: * <p>This method overrides the MultiPartEmail setMsg() method in
127: * order to send an HTML message instead of a full text message in
128: * the mail body. The message is formatted in HTML for the HTML
129: * part of the message, it is let as is in the alternate text
130: * part.
131: *
132: * @param msg A String.
133: * @return An Email.
134: * @exception MessagingException.
135: */
136: public Email setMsg(String msg) throws MessagingException {
137: setTextMsg(msg);
138: setHtmlMsg(new ElementContainer(new Html(new Body()
139: .addElement(new PRE(msg)))).toString());
140: return this ;
141: }
142:
143: /**
144: * Embeds an URL in the HTML.
145: *
146: * <p>This method allows to embed a file located by an URL into
147: * the mail body. It allows, for instance, to add inline images
148: * to the email. Inline files may be referenced with a
149: * <code>cid:xxxxxx</code> URL, where xxxxxx is the Content-ID
150: * returned by the embed function.
151: *
152: * <p>Example of use:<br><code><pre>
153: * HtmlEmail he = new HtmlEmail();
154: * he.setHtmlMsg("<html><img src=cid:"+embed("file:/my/image.gif","image.gif")+"></html>");
155: * // code to set the others email fields (not shown)
156: * </pre></code>
157: *
158: * @param url The URL of the file.
159: * @param name The name that will be set in the filename header
160: * field.
161: * @return A String with the Content-ID of the file.
162: * @exception MessagingException.
163: */
164: public String embed(URL url, String name) throws MessagingException {
165: MimeBodyPart mbp = new MimeBodyPart();
166:
167: mbp.setDataHandler(new DataHandler(new URLDataSource(url)));
168: mbp.setFileName(name);
169: mbp.setDisposition("inline");
170: String cid = org.apache.turbine.util.GenerateUniqueId
171: .getIdentifier();
172: mbp.addHeader("Content-ID", cid);
173:
174: getHtmlContent().addBodyPart(mbp);
175: return mbp.getContentID();
176: }
177:
178: /**
179: * Does the work of actually sending the email.
180: *
181: * @exception MessagingException, if there was an error.
182: */
183: public void send() throws MessagingException {
184: MimeBodyPart msgText = null;
185: MimeBodyPart msgHtml = null;
186:
187: if (StringUtils.isNotEmpty(text)
188: && StringUtils.isNotEmpty(html)) {
189: // The message in text and HTML form.
190: MimeMultipart msg = getHtmlContent();
191: msg.setSubType("alternative");
192: main.setContent(msg);
193:
194: msgText = new MimeBodyPart();
195: msgHtml = new MimeBodyPart();
196: msg.addBodyPart(msgText);
197: msg.addBodyPart(msgHtml);
198:
199: } else if (StringUtils.isNotEmpty(text)) {
200: // just text so the text part is the main part
201: msgText = main;
202: } else if (StringUtils.isNotEmpty(html)) {
203: // just HTML so the html part is the main part
204: msgHtml = main;
205: } else {
206: msgText = main;
207: text = "NO BODY";
208: }
209:
210: if (msgText != null) {
211: // add the text
212: if (charset != null) {
213: msgText.setText(text, charset);
214: } else {
215: msgText.setText(text);
216: }
217: }
218:
219: if (msgHtml != null) {
220: // add the html
221: if (charset != null) {
222: msgHtml.setContent(html, TEXT_HTML + ";charset="
223: + charset);
224: } else {
225: msgHtml.setContent(html, TEXT_HTML);
226: }
227: }
228:
229: super.send();
230: }
231: }
|