KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > template > TemplateHtmlEmail


1 package org.apache.fulcrum.template;
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 JavaDoc;
58 import java.util.Hashtable JavaDoc;
59
60 import javax.mail.MessagingException JavaDoc;
61
62 import org.apache.commons.mail.HtmlEmail;
63
64 /**
65  * This is a simple class for sending html email from within the TemplateService.
66  * Essentially, the bodies (text and html) of the email are a TemplateService
67  * TemplateContext objects. The beauty of this is that you can send email
68  * from within your TemplateService template or from your business logic in
69  * your Java code. The body of the email is just a TemplateService template
70  * so you can use all the template functionality of your TemplateService within
71  * your emails!
72  *
73  * <p>This class allows you to send HTML email with embedded content
74  * and/or with attachments. You can access the TemplateHtmlEmail
75  * instance within your templates trough the <code>$mail</code>
76  * Velocity variable.
77  * <p><code>TemplateHtmlEmail myEmail= new TemplateHtmlEmail(context);<br>
78  * context.put("mail", theMessage);</code>
79  *
80  *
81  * <p>The templates should be located under your TemplateService template
82  * directory.
83  *
84  * <p>This class extends the HtmlEmail class. Thus, it uses the
85  * JavaMail API and also depends on having the mail.host property
86  * set in the System.getProperties().
87  *
88  * @author <a HREF="mailto:A.Schild@aarboard.ch">Andre Schild</a>
89  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
90  * @version $Id: TemplateHtmlEmail.java,v 1.1 2004/11/12 10:26:06 epugh Exp $
91  */

92 public class TemplateHtmlEmail
93     extends HtmlEmail
94 {
95     /**
96      * The html template to process, relative to VM's template
97      * directory.
98      */

99     private String JavaDoc htmlTemplate = null;
100
101     /**
102      * A Context object which stores the information
103      * needed to construct the email.
104      */

105     private TemplateContext context = null;
106
107     /**
108      * The text template to process, relative to VM's template
109      * directory.
110      */

111     private String JavaDoc textTemplate = null;
112
113     /** The map of embedded files. */
114     private Hashtable JavaDoc embmap = null;
115
116     /**
117      * Constructor, sets the TemplateContext object.
118      *
119      * @param data A TemplateContext object.
120      * @exception MessagingException.
121      */

122     public TemplateHtmlEmail(TemplateContext context)
123         throws MessagingException JavaDoc
124     {
125         super.init();
126         this.context = context;
127         embmap = new Hashtable JavaDoc();
128     }
129
130     /**
131      * Set the HTML template for the mail. This is the TemplateService
132      * template to execute for the HTML part. Path is relative to the
133      * TemplateService templates directory.
134      *
135      * @param template A String.
136      * @return A TemplateHtmlEmail (self).
137      */

138     public TemplateHtmlEmail setHtmlTemplate(String JavaDoc template)
139     {
140         this.htmlTemplate = template;
141         return this;
142     }
143
144     /**
145      * Set the text template for the mail. This is the TemplateService
146      * template to execute for the text part. Path is relative to the
147      * TemplateService templates directory
148      *
149      * @param template A String.
150      * @return A TemplateHtmlEmail (self).
151      */

152     public TemplateHtmlEmail setTextTemplate(String JavaDoc template)
153     {
154         this.textTemplate = template;
155         return this;
156     }
157
158     /**
159      * Actually send the mail.
160      *
161      * @exception MessagingException.
162      */

163     public void send()
164         throws MessagingException JavaDoc
165     {
166         context.put("mail",this);
167
168         String JavaDoc htmlbody = "";
169         String JavaDoc textbody = "";
170
171         // Process the templates.
172
try
173         {
174             if(htmlTemplate != null)
175             {
176                 htmlbody = TurbineTemplate.handleRequest(
177                     context, htmlTemplate);
178             }
179             
180             if(textTemplate != null)
181             {
182                 textbody = TurbineTemplate.handleRequest(
183                     context, textTemplate);
184             }
185         }
186         catch( Exception JavaDoc e)
187         {
188             throw new MessagingException JavaDoc("Cannot parse template", e);
189         }
190
191         setHtmlMsg(htmlbody);
192         setTextMsg(textbody);
193
194         super.send();
195     }
196
197     /**
198      * Embed a file in the mail. The file can be referenced through
199      * its Content-ID. This function also registers the CID in an
200      * internal map, so the embedded file can be referenced more than
201      * once by using the getCid() function. This may be useful in a
202      * template.
203      *
204      * <p>Example of template:
205      *
206      * <code><pre width="80">
207      * &lt;html&gt;
208      * &lt;!-- $mail.embed("http://server/border.gif","border.gif"); --&gt;
209      * &lt;img SRC=$mail.getCid("border.gif")&gt;
210      * &lt;p&gt;This is your content
211      * &lt;img SRC=$mail.getCid("border.gif")&gt;
212      * &lt;/html&gt;
213      * </pre></code>
214      *
215      * @param surl A String.
216      * @param name A String.
217      * @return A String with the cid of the embedded file.
218      * @exception MessagingException.
219      * @see HtmlEmail#embed(URL surl, String name) embed.
220      */

221     public String JavaDoc embed(String JavaDoc surl,
222                         String JavaDoc name)
223         throws MessagingException JavaDoc
224     {
225         String JavaDoc cid ="";
226         try
227         {
228             URL JavaDoc url = new URL JavaDoc(surl);
229             cid = super.embed(url, name);
230             embmap.put(name,cid);
231         }
232         catch( Exception JavaDoc e )
233         {
234 // Log.error("cannot embed "+surl+": ", e);
235
}
236         return cid;
237     }
238
239     /**
240      * Get the cid of an embedded file.
241      *
242      * @param filename A String.
243      * @return A String with the cid of the embedded file.
244      * @see #embed(String surl, String name) embed.
245      */

246     public String JavaDoc getCid(String JavaDoc filename)
247     {
248         String JavaDoc cid = (String JavaDoc)embmap.get(filename);
249         return "cid:"+cid;
250     }
251 }
252  
253
Popular Tags