KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > factory > SendMailFactory


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

17
18 package org.apache.naming.factory;
19
20 import java.security.AccessController JavaDoc;
21 import java.security.PrivilegedAction JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import javax.mail.Session JavaDoc;
26 import javax.mail.internet.InternetAddress JavaDoc;
27 import javax.mail.internet.MimeMessage JavaDoc;
28 import javax.mail.internet.MimePart JavaDoc;
29 import javax.mail.internet.MimePartDataSource JavaDoc;
30 import javax.naming.Name JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.Reference JavaDoc;
33 import javax.naming.RefAddr JavaDoc;
34 import javax.naming.spi.ObjectFactory JavaDoc;
35
36 /**
37  * Factory class that creates a JNDI named javamail MimePartDataSource
38  * object which can be used for sending email using SMTP.
39  * <p>
40  * Can be configured in the DefaultContext or Context scope
41  * of your server.xml configuration file.
42  * <p>
43  * Example:
44  * <p>
45  * <pre>
46  * &lt;Resource name="mail/send" auth="CONTAINER"
47  * type="javax.mail.internet.MimePartDataSource"/>
48  * &lt;ResourceParams name="mail/send">
49  * &lt;parameter>&lt;name>factory&lt;/name>
50  * &lt;value>org.apache.naming.factory.SendMailFactory&lt;/value>
51  * &lt;/parameter>
52  * &lt;parameter>&lt;name>mail.smtp.host&lt;/name>
53  * &lt;value>your.smtp.host&lt;/value>
54  * &lt;/parameter>
55  * &lt;parameter>&lt;name>mail.smtp.user&lt;/name>
56  * &lt;value>someuser&lt;/value>
57  * &lt;/parameter>
58  * &lt;parameter>&lt;name>mail.from&lt;/name>
59  * &lt;value>someuser@some.host&lt;/value>
60  * &lt;/parameter>
61  * &lt;parameter>&lt;name>mail.smtp.sendpartial&lt;/name>
62  * &lt;value>true&lt;/value>
63  * &lt;/parameter>
64  * &lt;parameter>&lt;name>mail.smtp.dsn.notify&lt;/name>
65  * &lt;value>FAILURE&lt;/value>
66  * &lt;/parameter>
67  * &lt;parameter>&lt;name>mail.smtp.dsn.ret&lt;/name>
68  * &lt;value>FULL&lt;/value>
69  * &lt;/parameter>
70  * &lt;/ResourceParams>
71  * </pre>
72  *
73  * @author Glenn Nielsen Rich Catlett
74  */

75
76 public class SendMailFactory implements ObjectFactory JavaDoc
77 {
78     // The class name for the javamail MimeMessageDataSource
79
protected final String JavaDoc DataSourceClassName =
80     "javax.mail.internet.MimePartDataSource";
81
82     public Object JavaDoc getObjectInstance(Object JavaDoc RefObj, Name JavaDoc Nm, Context JavaDoc Ctx,
83                     Hashtable JavaDoc Env) throws Exception JavaDoc
84     {
85     final Reference JavaDoc Ref = (Reference JavaDoc)RefObj;
86
87     // Creation of the DataSource is wrapped inside a doPrivileged
88
// so that javamail can read its default properties without
89
// throwing Security Exceptions
90
if (Ref.getClassName().equals(DataSourceClassName)) {
91         return AccessController.doPrivileged( new PrivilegedAction JavaDoc()
92         {
93         public Object JavaDoc run() {
94                 // set up the smtp session that will send the message
95
Properties JavaDoc props = new Properties JavaDoc();
96             // enumeration of all refaddr
97
Enumeration JavaDoc list = Ref.getAll();
98             // current refaddr to be set
99
RefAddr JavaDoc refaddr;
100                 // set transport to smtp
101
props.put("mail.transport.protocol", "smtp");
102
103             while (list.hasMoreElements()) {
104             refaddr = (RefAddr JavaDoc)list.nextElement();
105
106             // set property
107
props.put(refaddr.getType(), (String JavaDoc)refaddr.getContent());
108             }
109             MimeMessage JavaDoc message = new MimeMessage JavaDoc(
110             Session.getInstance(props));
111             try {
112             String JavaDoc from = (String JavaDoc)Ref.get("mail.from").getContent();
113                 message.setFrom(new InternetAddress JavaDoc(from));
114                 message.setSubject("");
115             } catch (Exception JavaDoc e) {}
116             MimePartDataSource JavaDoc mds = new MimePartDataSource JavaDoc(
117             (MimePart JavaDoc)message);
118             return mds;
119         }
120         } );
121     }
122     else { // We can't create an instance of the DataSource
123
return null;
124     }
125     }
126 }
127
Popular Tags