1 package org.apache.turbine.util.mail;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.net.URL;
58 import javax.activation.DataHandler;
59 import javax.activation.URLDataSource;
60 import javax.mail.MessagingException;
61 import javax.mail.internet.MimeBodyPart;
62 import javax.mail.internet.MimeMultipart;
63 import org.apache.ecs.Document;
64 import org.apache.ecs.ElementContainer;
65 import org.apache.ecs.html.Body;
66 import org.apache.ecs.html.Html;
67 import org.apache.ecs.html.PRE;
68 import org.apache.turbine.util.StringUtils;
69
70 /***
71 * An HTML multipart email.
72 *
73 * <p>This class is used to send HTML formatted email. A text message
74 * can also be set for HTML unaware email clients, such as text-based
75 * email clients.
76 *
77 * <p>This class also inherits from MultiPartEmail, so it is easy to
78 * add attachents to the email.
79 *
80 * <p>To send an email in HTML, one should create a HtmlEmail, then
81 * use the setFrom, addTo, etc. methods. The HTML content can be set
82 * with the setHtmlMsg method. The alternate text content can be set
83 * with setTextMsg.
84 *
85 * <p>Either the text or HTML can be omitted, in which case the "main"
86 * part of the multipart becomes whichever is supplied rather than a
87 * multipart/alternative.
88 *
89 * @author <a href="mailto:unknown">Regis Koenig</a>
90 * @author <a href="mailto:sean@informage.net">Sean Legassick</a>
91 * @version $Id: HtmlEmail.java,v 1.2 2002/07/11 16:53:20 mpoeschl Exp $
92 */
93 public class HtmlEmail extends MultiPartEmail
94 {
95 protected MimeMultipart htmlContent;
96
97 protected String text;
98 protected String html;
99
100 /***
101 * Basic constructor.
102 *
103 * @exception MessagingException.
104 */
105 public HtmlEmail()
106 throws MessagingException
107 {
108 this.init();
109 }
110
111
112 /***
113 * Instantiates a new MimeMultipart object if it isn't already
114 * instantiated.
115 *
116 * @return A MimeMultipart object
117 */
118 public MimeMultipart getHtmlContent()
119 {
120 if (htmlContent == null)
121 {
122 htmlContent = new MimeMultipart();
123 }
124 return htmlContent;
125 }
126
127 /***
128 * Set the text content.
129 *
130 * @param text A String.
131 * @return An HtmlEmail.
132 * @exception MessagingException.
133 */
134 public HtmlEmail setTextMsg(String text)
135 throws MessagingException
136 {
137 this.text = text;
138 return this;
139 }
140
141 /***
142 * Set the HTML content.
143 *
144 * @param html A String.
145 * @return An HtmlEmail.
146 * @exception MessagingException.
147 */
148 public HtmlEmail setHtmlMsg(String html)
149 throws MessagingException
150 {
151 this.html = html;
152 return this;
153 }
154
155 /***
156 * Set the HTML content based on an ECS document.
157 *
158 * @param doc A Document.
159 * @return An HtmlEmail.
160 * @exception MessagingException.
161 */
162 public HtmlEmail setHtmlMsg(Document doc)
163 throws MessagingException
164 {
165 return setHtmlMsg(doc.toString());
166 }
167
168 /***
169 * Set the message.
170 *
171 * <p>This method overrides the MultiPartEmail setMsg() method in
172 * order to send an HTML message instead of a full text message in
173 * the mail body. The message is formatted in HTML for the HTML
174 * part of the message, it is let as is in the alternate text
175 * part.
176 *
177 * @param msg A String.
178 * @return An Email.
179 * @exception MessagingException.
180 */
181 public Email setMsg(String msg)
182 throws MessagingException
183 {
184 setTextMsg(msg);
185 setHtmlMsg(new ElementContainer(new Html(new Body()
186 .addElement(new PRE(msg)))).toString());
187 return this;
188 }
189
190 /***
191 * Embeds an URL in the HTML.
192 *
193 * <p>This method allows to embed a file located by an URL into
194 * the mail body. It allows, for instance, to add inline images
195 * to the email. Inline files may be referenced with a
196 * <code>cid:xxxxxx</code> URL, where xxxxxx is the Content-ID
197 * returned by the embed function.
198 *
199 * <p>Example of use:<br><code><pre>
200 * HtmlEmail he = new HtmlEmail();
201 * he.setHtmlMsg("<html><img src=cid:"+embed("file:/my/image.gif","image.gif")+"></html>");
202 * // code to set the others email fields (not shown)
203 * </pre></code>
204 *
205 * @param url The URL of the file.
206 * @param name The name that will be set in the filename header
207 * field.
208 * @return A String with the Content-ID of the file.
209 * @exception MessagingException.
210 */
211 public String embed(URL url, String name)
212 throws MessagingException
213 {
214 MimeBodyPart mbp = new MimeBodyPart();
215
216 mbp.setDataHandler(new DataHandler(new URLDataSource(url)));
217 mbp.setFileName(name);
218 mbp.setDisposition("inline");
219 String cid = org.apache.turbine.util.GenerateUniqueId.getIdentifier();
220 mbp.addHeader("Content-ID", cid);
221
222 getHtmlContent().addBodyPart(mbp);
223 return mbp.getContentID();
224 }
225
226 /***
227 * Does the work of actually sending the email.
228 *
229 * @exception MessagingException, if there was an error.
230 */
231 public void send()
232 throws MessagingException
233 {
234 MimeBodyPart msgText = null;
235 MimeBodyPart msgHtml = null;
236
237 if (StringUtils.isValid(text) && StringUtils.isValid(html))
238 {
239 // The message in text and HTML form.
240 MimeMultipart msg = getHtmlContent();
241 msg.setSubType("alternative");
242 main.setContent(msg);
243
244 msgText = new MimeBodyPart();
245 msgHtml = new MimeBodyPart();
246 msg.addBodyPart(msgText);
247 msg.addBodyPart(msgHtml);
248
249 }
250 else if (StringUtils.isValid(text))
251 {
252 // just text so the text part is the main part
253 msgText = main;
254 }
255 else if (StringUtils.isValid(html))
256 {
257 // just HTML so the html part is the main part
258 msgHtml = main;
259 }
260 else
261 {
262 msgText = main;
263 text = "NO BODY";
264 }
265
266 if (msgText != null)
267 {
268 // add the text
269 if ( charset != null )
270 {
271 msgText.setText(text, charset);
272 }
273 else
274 {
275 msgText.setText(text);
276 }
277 }
278
279 if (msgHtml != null)
280 {
281 // add the html
282 if (charset != null)
283 {
284 msgHtml.setContent(html, TEXT_HTML + ";charset=" + charset);
285 }
286 else
287 {
288 msgHtml.setContent(html, TEXT_HTML);
289 }
290 }
291
292 super.send();
293 }
294 }
This page was automatically generated by Maven