KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > mail > HtmlEmail


1 package org.apache.turbine.util.mail;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.net.URL JavaDoc;
20
21 import javax.activation.DataHandler JavaDoc;
22 import javax.activation.URLDataSource JavaDoc;
23
24 import javax.mail.MessagingException JavaDoc;
25 import javax.mail.internet.MimeBodyPart JavaDoc;
26 import javax.mail.internet.MimeMultipart JavaDoc;
27
28 import org.apache.commons.lang.StringUtils;
29
30 import org.apache.ecs.Document;
31 import org.apache.ecs.ElementContainer;
32 import org.apache.ecs.html.Body;
33 import org.apache.ecs.html.Html;
34 import org.apache.ecs.html.PRE;
35
36 /**
37  * An HTML multipart email.
38  *
39  * <p>This class is used to send HTML formatted email. A text message
40  * can also be set for HTML unaware email clients, such as text-based
41  * email clients.
42  *
43  * <p>This class also inherits from MultiPartEmail, so it is easy to
44  * add attachents to the email.
45  *
46  * <p>To send an email in HTML, one should create a HtmlEmail, then
47  * use the setFrom, addTo, etc. methods. The HTML content can be set
48  * with the setHtmlMsg method. The alternate text content can be set
49  * with setTextMsg.
50  *
51  * <p>Either the text or HTML can be omitted, in which case the "main"
52  * part of the multipart becomes whichever is supplied rather than a
53  * multipart/alternative.
54  *
55  * @author <a HREF="mailto:unknown">Regis Koenig</a>
56  * @author <a HREF="mailto:sean@informage.net">Sean Legassick</a>
57  * @version $Id: HtmlEmail.java,v 1.7.2.2 2004/05/20 03:20:18 seade Exp $
58  * @deprecated Use org.apache.commons.mail.HtmlEmail instead.
59  */

60 public class HtmlEmail extends MultiPartEmail
61 {
62     protected MimeMultipart JavaDoc htmlContent;
63
64     protected String JavaDoc text;
65     protected String JavaDoc html;
66
67     /**
68      * Basic constructor.
69      *
70      * @exception MessagingException.
71      */

72     public HtmlEmail()
73             throws MessagingException JavaDoc
74     {
75         this.init();
76     }
77
78     /**
79      * Instantiates a new MimeMultipart object if it isn't already
80      * instantiated.
81      *
82      * @return A MimeMultipart object
83      */

84     public MimeMultipart JavaDoc getHtmlContent()
85     {
86         if (htmlContent == null)
87         {
88             htmlContent = new MimeMultipart JavaDoc();
89         }
90         return htmlContent;
91     }
92
93     /**
94      * Set the text content.
95      *
96      * @param text A String.
97      * @return An HtmlEmail.
98      * @exception MessagingException.
99      */

100     public HtmlEmail setTextMsg(String JavaDoc text)
101             throws MessagingException JavaDoc
102     {
103         this.text = text;
104         return this;
105     }
106
107     /**
108      * Set the HTML content.
109      *
110      * @param html A String.
111      * @return An HtmlEmail.
112      * @exception MessagingException.
113      */

114     public HtmlEmail setHtmlMsg(String JavaDoc html)
115             throws MessagingException JavaDoc
116     {
117         this.html = html;
118         return this;
119     }
120
121     /**
122      * Set the HTML content based on an ECS document.
123      *
124      * @param doc A Document.
125      * @return An HtmlEmail.
126      * @exception MessagingException.
127      */

128     public HtmlEmail setHtmlMsg(Document doc)
129             throws MessagingException JavaDoc
130     {
131         return setHtmlMsg(doc.toString());
132     }
133
134     /**
135      * Set the message.
136      *
137      * <p>This method overrides the MultiPartEmail setMsg() method in
138      * order to send an HTML message instead of a full text message in
139      * the mail body. The message is formatted in HTML for the HTML
140      * part of the message, it is let as is in the alternate text
141      * part.
142      *
143      * @param msg A String.
144      * @return An Email.
145      * @exception MessagingException.
146      */

147     public Email setMsg(String JavaDoc msg)
148             throws MessagingException JavaDoc
149     {
150         setTextMsg(msg);
151         setHtmlMsg(new ElementContainer(new Html(new Body()
152                 .addElement(new PRE(msg)))).toString());
153         return this;
154     }
155
156     /**
157      * Embeds an URL in the HTML.
158      *
159      * <p>This method allows to embed a file located by an URL into
160      * the mail body. It allows, for instance, to add inline images
161      * to the email. Inline files may be referenced with a
162      * <code>cid:xxxxxx</code> URL, where xxxxxx is the Content-ID
163      * returned by the embed function.
164      *
165      * <p>Example of use:<br><code><pre>
166      * HtmlEmail he = new HtmlEmail();
167      * he.setHtmlMsg("&lt;html&gt;&lt;img SRC=cid:"+embed("file:/my/image.gif","image.gif")+"&gt;&lt;/html&gt;");
168      * // code to set the others email fields (not shown)
169      * </pre></code>
170      *
171      * @param url The URL of the file.
172      * @param name The name that will be set in the filename header
173      * field.
174      * @return A String with the Content-ID of the file.
175      * @exception MessagingException.
176      */

177     public String JavaDoc embed(URL JavaDoc url, String JavaDoc name)
178             throws MessagingException JavaDoc
179     {
180         MimeBodyPart JavaDoc mbp = new MimeBodyPart JavaDoc();
181
182         mbp.setDataHandler(new DataHandler JavaDoc(new URLDataSource JavaDoc(url)));
183         mbp.setFileName(name);
184         mbp.setDisposition("inline");
185         String JavaDoc cid = org.apache.turbine.util.GenerateUniqueId.getIdentifier();
186         mbp.addHeader("Content-ID", cid);
187
188         getHtmlContent().addBodyPart(mbp);
189         return mbp.getContentID();
190     }
191
192     /**
193      * Does the work of actually sending the email.
194      *
195      * @exception MessagingException, if there was an error.
196      */

197     public void send()
198             throws MessagingException JavaDoc
199     {
200         MimeBodyPart JavaDoc msgText = null;
201         MimeBodyPart JavaDoc msgHtml = null;
202
203         if (StringUtils.isNotEmpty(text) && StringUtils.isNotEmpty(html))
204         {
205             // The message in text and HTML form.
206
MimeMultipart JavaDoc msg = getHtmlContent();
207             msg.setSubType("alternative");
208             main.setContent(msg);
209
210             msgText = new MimeBodyPart JavaDoc();
211             msgHtml = new MimeBodyPart JavaDoc();
212             msg.addBodyPart(msgText);
213             msg.addBodyPart(msgHtml);
214
215         }
216         else if (StringUtils.isNotEmpty(text))
217         {
218             // just text so the text part is the main part
219
msgText = main;
220         }
221         else if (StringUtils.isNotEmpty(html))
222         {
223             // just HTML so the html part is the main part
224
msgHtml = main;
225         }
226         else
227         {
228             msgText = main;
229             text = "NO BODY";
230         }
231
232         if (msgText != null)
233         {
234             // add the text
235
if (charset != null)
236             {
237                 msgText.setText(text, charset);
238             }
239             else
240             {
241                 msgText.setText(text);
242             }
243         }
244
245         if (msgHtml != null)
246         {
247             // add the html
248
if (charset != null)
249             {
250                 msgHtml.setContent(html, TEXT_HTML + ";charset=" + charset);
251             }
252             else
253             {
254                 msgHtml.setContent(html, TEXT_HTML);
255             }
256         }
257
258         super.send();
259     }
260 }
261
Popular Tags