Source Code Cross Referenced for SendMailFactory.java in  » Sevlet-Container » apache-tomcat-6.0.14 » org » apache » naming » factory » 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 » Sevlet Container » apache tomcat 6.0.14 » org.apache.naming.factory 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.naming.factory;
019:
020:        import java.security.AccessController;
021:        import java.security.PrivilegedAction;
022:        import java.util.Hashtable;
023:        import java.util.Properties;
024:        import java.util.Enumeration;
025:        import javax.mail.Session;
026:        import javax.mail.internet.InternetAddress;
027:        import javax.mail.internet.MimeMessage;
028:        import javax.mail.internet.MimePart;
029:        import javax.mail.internet.MimePartDataSource;
030:        import javax.naming.Name;
031:        import javax.naming.Context;
032:        import javax.naming.Reference;
033:        import javax.naming.RefAddr;
034:        import javax.naming.spi.ObjectFactory;
035:
036:        /**
037:         * Factory class that creates a JNDI named javamail MimePartDataSource
038:         * object which can be used for sending email using SMTP.
039:         * <p>
040:         * Can be configured in the DefaultContext or Context scope
041:         * of your server.xml configuration file.
042:         * <p>
043:         * Example:
044:         * <p>
045:         * <pre>
046:         * &lt;Resource name="mail/send" auth="CONTAINER"
047:         *           type="javax.mail.internet.MimePartDataSource"/>
048:         * &lt;ResourceParams name="mail/send">
049:         *   &lt;parameter>&lt;name>factory&lt;/name>
050:         *     &lt;value>org.apache.naming.factory.SendMailFactory&lt;/value>
051:         *   &lt;/parameter>
052:         *   &lt;parameter>&lt;name>mail.smtp.host&lt;/name>
053:         *     &lt;value>your.smtp.host&lt;/value>
054:         *   &lt;/parameter>
055:         *   &lt;parameter>&lt;name>mail.smtp.user&lt;/name>
056:         *     &lt;value>someuser&lt;/value>
057:         *   &lt;/parameter>
058:         *   &lt;parameter>&lt;name>mail.from&lt;/name>
059:         *     &lt;value>someuser@some.host&lt;/value>
060:         *   &lt;/parameter>
061:         *   &lt;parameter>&lt;name>mail.smtp.sendpartial&lt;/name>
062:         *     &lt;value>true&lt;/value>
063:         *   &lt;/parameter>
064:         *  &lt;parameter>&lt;name>mail.smtp.dsn.notify&lt;/name>
065:         *     &lt;value>FAILURE&lt;/value>
066:         *   &lt;/parameter>
067:         *   &lt;parameter>&lt;name>mail.smtp.dsn.ret&lt;/name>
068:         *     &lt;value>FULL&lt;/value>
069:         *   &lt;/parameter>
070:         * &lt;/ResourceParams>
071:         * </pre>
072:         *
073:         * @author Glenn Nielsen Rich Catlett
074:         */
075:
076:        public class SendMailFactory implements  ObjectFactory {
077:            // The class name for the javamail MimeMessageDataSource
078:            protected final String DataSourceClassName = "javax.mail.internet.MimePartDataSource";
079:
080:            public Object getObjectInstance(Object RefObj, Name Nm,
081:                    Context Ctx, Hashtable Env) throws Exception {
082:                final Reference Ref = (Reference) RefObj;
083:
084:                // Creation of the DataSource is wrapped inside a doPrivileged
085:                // so that javamail can read its default properties without
086:                // throwing Security Exceptions
087:                if (Ref.getClassName().equals(DataSourceClassName)) {
088:                    return AccessController
089:                            .doPrivileged(new PrivilegedAction() {
090:                                public Object run() {
091:                                    // set up the smtp session that will send the message
092:                                    Properties props = new Properties();
093:                                    // enumeration of all refaddr
094:                                    Enumeration list = Ref.getAll();
095:                                    // current refaddr to be set
096:                                    RefAddr refaddr;
097:                                    // set transport to smtp
098:                                    props
099:                                            .put("mail.transport.protocol",
100:                                                    "smtp");
101:
102:                                    while (list.hasMoreElements()) {
103:                                        refaddr = (RefAddr) list.nextElement();
104:
105:                                        // set property
106:                                        props.put(refaddr.getType(),
107:                                                (String) refaddr.getContent());
108:                                    }
109:                                    MimeMessage message = new MimeMessage(
110:                                            Session.getInstance(props));
111:                                    try {
112:                                        String from = (String) Ref.get(
113:                                                "mail.from").getContent();
114:                                        message.setFrom(new InternetAddress(
115:                                                from));
116:                                        message.setSubject("");
117:                                    } catch (Exception e) {
118:                                    }
119:                                    MimePartDataSource mds = new MimePartDataSource(
120:                                            (MimePart) message);
121:                                    return mds;
122:                                }
123:                            });
124:                } else { // We can't create an instance of the DataSource
125:                    return null;
126:                }
127:            }
128:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.