Source Code Cross Referenced for HtmlEmail.java in  » Project-Management » turbine » org » apache » turbine » util » mail » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Project Management » turbine » org.apache.turbine.util.mail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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("&lt;html&gt;&lt;img src=cid:"+embed("file:/my/image.gif","image.gif")+"&gt;&lt;/html&gt;");
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.