Source Code Cross Referenced for Mailer.java in  » ERP-CRM-Financial » Kuali-Financial-System » edu » iu » uis » eden » 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 » ERP CRM Financial » Kuali Financial System » edu.iu.uis.eden.mail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2005-2006 The Kuali Foundation.
003:         * 
004:         * 
005:         * Licensed under the Educational Community License, Version 1.0 (the "License");
006:         * you may not use this file except in compliance with the License.
007:         * You may obtain a copy of the License at
008:         * 
009:         * http://www.opensource.org/licenses/ecl1.php
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:        package edu.iu.uis.eden.mail;
018:
019:        import java.util.Properties;
020:
021:        import javax.activation.DataHandler;
022:        import javax.mail.Address;
023:        import javax.mail.Authenticator;
024:        import javax.mail.Message;
025:        import javax.mail.MessagingException;
026:        import javax.mail.NoSuchProviderException;
027:        import javax.mail.PasswordAuthentication;
028:        import javax.mail.Session;
029:        import javax.mail.Transport;
030:        import javax.mail.internet.AddressException;
031:        import javax.mail.internet.InternetAddress;
032:        import javax.mail.internet.MimeMessage;
033:
034:        import edu.iu.uis.eden.util.ByteArrayDataSource;
035:
036:        /**
037:         * Maintains a Java Mail session and can be used for sending emails.
038:         *
039:         * @author ewestfal
040:         */
041:        public class Mailer {
042:
043:            protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
044:                    .getLogger(getClass());
045:
046:            private Properties configProperties;
047:            private Authenticator authenticator;
048:
049:            private Session currentSession;
050:
051:            /**
052:             * Create a AuthenticatedMailer with the default values for authenticated
053:             * use of mail-relay.iu.edu.
054:             * 
055:             * The Kerberos principle and and password should be a system principle
056:             * rather than a user principle.
057:             * 
058:             * @param senderAddress
059:             *            return address for the mail being sent
060:             * @param kbPrincipleName
061:             *            kerberos principle name used to authenticate to mail-relay
062:             * @param kbPrinciplePassword
063:             *            kerberos principle password used to authenticate to mail-relay
064:             */
065:            public Mailer(Properties configProperties,
066:                    Authenticator authenticator) {
067:                this .configProperties = configProperties;
068:                this .authenticator = authenticator;
069:            }
070:
071:            public Mailer(Properties configProperties) {
072:                this (configProperties, null);
073:            }
074:
075:            public Mailer(Properties configProperties, String username,
076:                    String password) {
077:                this (configProperties, new SimpleAuthenticator(username,
078:                        password));
079:            }
080:
081:            /**
082:             * A method to configure the Mailer properties. This class will default to a
083:             * set of properties but this method allows the calling program to set the
084:             * specific properties.
085:             * 
086:             * @param mailConfigProperties
087:             *            Proeprties object containing configuration information
088:             */
089:            public void setConfig(Properties configProperties) {
090:                this .configProperties = configProperties;
091:            }
092:
093:            /**
094:             * Sends an email to the given recipients.
095:             * 
096:             * @param recipients
097:             *            list of addresses to which the message is sent
098:             * @param subject
099:             *            subject of the message
100:             * @param messageBody
101:             *            body of the message
102:             * @param ccRecipients
103:             *            list of addresses which are to be cc'd on the message
104:             * @param bccRecipients
105:             *            list of addresses which are to be bcc'd on the message
106:             *
107:             * @throws AddressException
108:             * @throws MessagingException
109:             */
110:            public void sendMessage(String sender, Address[] recipients,
111:                    String subject, String messageBody, Address[] ccRecipients,
112:                    Address[] bccRecipients, boolean htmlMessage)
113:                    throws AddressException, MessagingException {
114:                Session session = getCurrentSession();
115:                session.setDebug(LOG.isDebugEnabled());
116:                Message message = new MimeMessage(session);
117:
118:                // From Address
119:                message.setFrom(new InternetAddress(sender));
120:
121:                // To Address
122:                if (recipients != null && recipients.length > 0) {
123:                    message.addRecipients(Message.RecipientType.TO, recipients);
124:                } else {
125:                    LOG.warn("No recipients indicated");
126:                }
127:
128:                // CC Address
129:                if (ccRecipients != null && ccRecipients.length > 0) {
130:                    message.addRecipients(Message.RecipientType.CC,
131:                            ccRecipients);
132:                }
133:
134:                // BCC Address
135:                if (bccRecipients != null && bccRecipients.length > 0) {
136:                    message.addRecipients(Message.RecipientType.BCC,
137:                            bccRecipients);
138:                }
139:
140:                // The Subject
141:                message.setSubject(subject);
142:                if (subject == null || "".equals(subject)) {
143:                    LOG.warn("Empty subject being sent");
144:                }
145:
146:                // Now the message body.
147:
148:                if (htmlMessage) {
149:                    prepareHtmlMessage(messageBody, message);
150:                } else {
151:                    message.setText(messageBody);
152:                    if (messageBody == null || "".equals(messageBody)) {
153:                        LOG.warn("Empty message body being sent.");
154:                    }
155:                }
156:
157:                // Finally, send the message!
158:                Transport.send(message);
159:
160:            }
161:
162:            /**
163:             * Send a message to the designated recipient with the specified subject and
164:             * message. This is a convience class for simple message addressing
165:             * 
166:             * @param recipient
167:             *            the email address to which the message is sent
168:             * @param subject
169:             *            subject for the message
170:             * @param messageBody
171:             *            body of the message to to be sent
172:             * @param ccRecipient
173:             *            email address of a cc recipient
174:             */
175:            public void sendMessage(String sender, String recipient,
176:                    String subject, String messageBody, boolean htmlMessage)
177:                    throws AddressException, MessagingException {
178:                final Address[] NO_RECIPIENTS = null;
179:                Address[] recipients = { new InternetAddress(recipient) };
180:                sendMessage(sender, recipients, subject, messageBody,
181:                        NO_RECIPIENTS, NO_RECIPIENTS, htmlMessage);
182:            }
183:
184:            /**
185:             * @return current properties used to configure the mail session
186:             */
187:            public Properties getConfig() {
188:                return configProperties;
189:            }
190:
191:            public Authenticator getAuthenticator() {
192:                return authenticator;
193:            }
194:
195:            /**
196:             * This allows direct access to the Mail Session. While this offers more
197:             * flexibility, the AuthenticatedMailer will no longer be responsible for
198:             * management of the this session.
199:             * 
200:             * @return get the current session. If current session is null it creates a
201:             *         new one.
202:             */
203:            public Session getCurrentSession() throws NoSuchProviderException {
204:                if (this .currentSession == null
205:                        || !this .currentSession.getTransport().isConnected()) {
206:                    this .currentSession = Session.getInstance(configProperties,
207:                            authenticator);
208:                }
209:                return currentSession;
210:            }
211:
212:            private void prepareHtmlMessage(String messageText, Message message)
213:                    throws MessagingException {
214:                message.setDataHandler(new DataHandler(new ByteArrayDataSource(
215:                        messageText, "text/html")));
216:            }
217:
218:            /**
219:             * SimpleAuthenticator is used to do simple authentication when the SMTP
220:             * server requires it.
221:             */
222:            private static class SimpleAuthenticator extends
223:                    javax.mail.Authenticator {
224:
225:                private final PasswordAuthentication passwordAuthentication;
226:
227:                private SimpleAuthenticator(String username, String password) {
228:                    this .passwordAuthentication = new PasswordAuthentication(
229:                            username, password);
230:                }
231:
232:                public PasswordAuthentication getPasswordAuthentication() {
233:                    return passwordAuthentication;
234:                }
235:
236:            }
237:
238:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.