Source Code Cross Referenced for MailHandler.java in  » Development » jLo » org » jzonic » jlo » handler » 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 » Development » jLo » org.jzonic.jlo.handler 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Created on 21.07.2003
003:         *
004:         * To change the template for this generated file go to
005:         * Window - Preferences - Java - Code Generation - Code and Comments
006:         */
007:        package org.jzonic.jlo.handler;
008:
009:        import org.jzonic.jlo.LogRecord;
010:        import org.jzonic.jlo.error.ErrorHandler;
011:
012:        import javax.mail.*;
013:        import javax.mail.internet.*;
014:        import java.util.Date;
015:        import java.util.Map;
016:        import java.util.Properties;
017:        import java.util.StringTokenizer;
018:
019:        /**
020:         * @author Andreas Mecky andreas.mecky@xcom.de
021:         * @author Terry Dye terry.dye@xcom.de
022:         *
023:         * This handler can be used to send the log message as email.
024:         * The following parameters are required:<br/>
025:         * <parameter name="smtphost" value="name of the smtp server"/><br/>
026:         * <parameter name="sender" value="the mail address of the sender"/><br/>
027:         * <parameter name="recipient" value="list of recievers"/><br/>
028:         * this one is optional:<br/>
029:         * <parameter name="subject" value="the subject of the mail"/><br/>
030:         * If not set the mail will be send with the subject "no subject".<br/>
031:         * These parameters are required if the smtp server requires "smtp after pop"<br/>
032:         * <parameter name="user" value="the user name"/><br/>
033:         * <parameter name="pwd" value="the password for this user"/><br/>
034:         * <parameter name="popserver" value="the name of the pop3 server"/><br/>
035:         * <parameter name="authentification" value="true"/><br/>
036:         *
037:         */
038:        public class MailHandler extends AbstractHandler {
039:
040:            private String smtpHost;
041:            private String popHost;
042:            private String username;
043:            private String pwd;
044:            private boolean authentification = false;
045:            private String from;
046:            private String to;
047:            private String subject;
048:
049:            public MailHandler(String configName) {
050:                super (configName);
051:            }
052:
053:            /* (non-Javadoc)
054:             * @see org.jzonic.jlo.handler.Handler#publish(java.lang.String)
055:             */
056:            public void publish(String msg) {
057:                sendEmail(from, to, null, subject, msg);
058:
059:            }
060:
061:            /* (non-Javadoc)
062:             * @see org.jzonic.jlo.handler.Handler#publish(org.jzonic.jlo.LogRecord)
063:             */
064:            public void publish(LogRecord lr) {
065:                publish(lr.getMessage());
066:
067:            }
068:
069:            /* (non-Javadoc)
070:             * @see org.jzonic.jlo.handler.Handler#setParameter(java.util.Hashtable)
071:             */
072:            public void setParameter(Map parameters) {
073:                // smtphost
074:                if (parameters.containsKey("smtphost")) {
075:                    smtpHost = (String) parameters.get("smtphost");
076:                }
077:                // from
078:                if (parameters.containsKey("sender")) {
079:                    from = (String) parameters.get("sender");
080:                }
081:                // to
082:                if (parameters.containsKey("recipient")) {
083:                    to = (String) parameters.get("recipient");
084:                }
085:                // subject
086:                if (parameters.containsKey("subject")) {
087:                    subject = (String) parameters.get("subject");
088:                } else {
089:                    subject = "No subject";
090:                }
091:                // authentification
092:                if (parameters.containsKey("authentification")) {
093:                    String tmp = (String) parameters.get("authentification");
094:                    if (tmp.equalsIgnoreCase("true")) {
095:                        authentification = true;
096:                    }
097:                }
098:                // user
099:                if (parameters.containsKey("user")) {
100:                    username = (String) parameters.get("user");
101:                }
102:                // pwd
103:                if (parameters.containsKey("pwd")) {
104:                    pwd = (String) parameters.get("pwd");
105:                }
106:                // popserver
107:                if (parameters.containsKey("popserver")) {
108:                    popHost = (String) parameters.get("popserver");
109:                }
110:
111:            }
112:
113:            private void sendEmail(String from, String recipients,
114:                    String copies, String subject, String bodyText) {
115:                try {
116:                    Properties props = System.getProperties();
117:                    props.put("mail.smtp.host", smtpHost);
118:                    if (authentification) {
119:                        props.put("mail.smtp.auth", "true");
120:                    }
121:                    Session session = Session.getDefaultInstance(props, null);
122:                    if (authentification) {
123:                        // Log onto pop server first as security protocol may require this
124:                        Session pSession = Session.getInstance(props, null);
125:                        Store store = pSession.getStore("pop3");
126:                        store.connect(popHost, username, pwd);
127:                        store.close();
128:                    }
129:                    // Create a mime message
130:                    MimeMessage mail = new MimeMessage(session);
131:                    mail.setFrom(new InternetAddress(from));
132:
133:                    StringTokenizer st = new StringTokenizer(recipients, ",");
134:                    InternetAddress[] recList = new InternetAddress[st
135:                            .countTokens()];
136:                    for (int r = 0; st.hasMoreTokens(); r++)
137:                        recList[r] = new InternetAddress(st.nextToken().trim());
138:                    mail.setRecipients(Message.RecipientType.TO, recList);
139:
140:                    if (copies != null) {
141:                        st = new StringTokenizer(copies, ",");
142:                        InternetAddress[] copyList = new InternetAddress[st
143:                                .countTokens()];
144:                        for (int c = 0; st.hasMoreTokens(); c++) {
145:                            copyList[c] = new InternetAddress(st.nextToken()
146:                                    .trim());
147:                        }
148:                        mail.setRecipients(Message.RecipientType.CC, copyList);
149:                    }
150:                    mail.setSubject(subject);
151:                    mail.setText(bodyText);
152:                    mail.setSentDate(new Date());
153:
154:                    // Send the message
155:                    Transport trans = session.getTransport("smtp");
156:                    trans.connect(smtpHost, username, pwd);
157:                    trans.sendMessage(mail, mail.getAllRecipients());
158:                    trans.close();
159:                } catch (Exception e) {
160:                    ErrorHandler.reportError("Cannot send email", e);
161:                }
162:            }
163:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.