Source Code Cross Referenced for SendMail.java in  » Database-JDBC-Connection-Pool » Primrose » uk » org » primrose » 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 » Database JDBC Connection Pool » Primrose » uk.org.primrose 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *	Library name : Primrose - A Java Database Connection Pool.
003:         *	Published by Ben Keeping, http://primrose.org.uk .
004:         *	Copyright (C) 2004 Ben Keeping, primrose.org.uk
005:         *	Email: Use "Contact Us Form" on website
006:         *
007:         *	This library is free software; you can redistribute it and/or
008:         *	modify it under the terms of the GNU Lesser General Public
009:         *	License as published by the Free Software Foundation; either
010:         *	version 2.1 of the License, or (at your option) any later version.
011:         *
012:         *	This library is distributed in the hope that it will be useful,
013:         *	but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015:         *	Lesser General Public License for more details.
016:         *
017:         *	You should have received a copy of the GNU Lesser General Public
018:         *	License along with this library; if not, write to the Free Software
019:         *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020:         */package uk.org.primrose;
021:
022:        import javax.mail.*;
023:        import javax.mail.internet.*;
024:        import java.util.*;
025:        import java.io.*;
026:        import javax.activation.*;
027:
028:        /**
029:         * @author Ben Keeping
030:         *
031:         * Generic sendMail class, allows for sending emails with attachements.
032:         */
033:        public class SendMail {
034:            static String mxServer;
035:            String toAddress, fromAddress;
036:            String subject, text;
037:            MimeBodyPart mbp = null;
038:            Multipart mp = new MimeMultipart();
039:            Logger logger = null;
040:
041:            /**
042:             * Construct an email. <BR>
043:             * If the mxServer is unknown, then the static method SendMail.nslookup() can be called to retrieve the domain's mx server. <BR>
044:             * Attachments are optional. <BR>
045:             * @param String mxServer - The Mail eXchange server to send the mail to.
046:             * @param String toAddress - The recipient.
047:             * @param String fromAddress - The sender - can be anyting as long as looks like an email address - eg 'me@somewhere.com'.
048:             * @param String subject - The mail's subject.
049:             * @param String text - The body of the mail.
050:             */
051:            public SendMail(Logger logger, String mxServer, String toAddress,
052:                    String fromAddress, String subject, String text) {
053:                SendMail.mxServer = mxServer;
054:                this .toAddress = toAddress;
055:                this .fromAddress = fromAddress;
056:                this .subject = subject;
057:                this .text = text;
058:                this .logger = logger;
059:
060:            }
061:
062:            /**
063:             * Add an attachment to the mail (from a file on disk).
064:             * @param File file - the File object to attach.
065:             */
066:            public void attach(File file) {
067:                try {
068:                    mbp = new MimeBodyPart();
069:                    mbp.setFileName(file.getName());
070:                    mbp
071:                            .setDataHandler(new DataHandler(new FileDataSource(
072:                                    file)));
073:                    mp.addBodyPart(mbp);
074:                } catch (MessagingException me) {
075:                    logger.printStackTrace(me);
076:                }
077:            }
078:
079:            class SendThread extends Thread {
080:                public void run() {
081:                    Properties props = System.getProperties();
082:                    props.put("mail.smtp.host", mxServer);
083:                    Session session = Session.getDefaultInstance(props, null);
084:
085:                    try {
086:                        Message msg = new MimeMessage(session);
087:                        msg.setFrom(new InternetAddress(fromAddress));
088:                        msg.setRecipients(Message.RecipientType.TO,
089:                                InternetAddress.parse(toAddress, false));
090:                        msg.setSubject(subject);
091:                        msg.setHeader("X-Mailer", "JavaMail");
092:
093:                        // If there have been attachments (one or more), then set the text/body
094:                        // as a MimeBodyPart, else set it on the Message. If this isn't done,
095:                        // then either text or an attachment is sent - not both !
096:                        if (mbp != null) {
097:                            MimeBodyPart mbp2 = new MimeBodyPart();
098:                            mbp2.setText(text);
099:                            mp.addBodyPart(mbp2);
100:                            msg.setContent(mp);
101:                        } else {
102:                            msg.setText(text);
103:                        }
104:
105:                        Transport.send(msg);
106:                    } catch (AddressException ae) {
107:                        logger.printStackTrace(ae);
108:                    } catch (MessagingException me) {
109:                        logger.printStackTrace(me);
110:                    }
111:                }
112:            }
113:
114:            /**
115:             * Send a message using the contstructor properties.
116:             * If there is also an attachment to send, add it too.
117:             */
118:            public void send() {
119:                new SendThread().start();
120:            }
121:
122:            /**
123:             * Given a domain name like 'hotmail.com', perform an OS nslookup call,
124:             * and loop it, looking for the word 'exchanger' in the line. On Linux and Windoze
125:             * the mx mail server is always the last word/token in the line, so set it as such.
126:             * This pays no attention to the preference of which mx server to use, but could (and should !)
127:             * be built in really. Still, never mind.
128:             * @param String domain - the domain to lookup.
129:             */
130:            public String nslookup(String domain) {
131:                String mailserver = null;
132:                try {
133:                    Process p = Runtime.getRuntime().exec(
134:                            "nslookup -type=mx " + domain);
135:                    BufferedReader br = new BufferedReader(
136:                            new InputStreamReader(p.getInputStream()));
137:
138:                    boolean gotMxLine = false;
139:                    String line = null;
140:                    String token = null;
141:
142:                    while ((line = br.readLine()) != null) {
143:                        gotMxLine = false;
144:                        //System.out.println(line);
145:                        StringTokenizer st = new StringTokenizer(line);
146:                        while (st.hasMoreTokens()) {
147:                            token = st.nextToken();
148:                            if (token.equals("exchanger")) {
149:                                gotMxLine = true;
150:                            }
151:                            if (gotMxLine) {
152:                                mailserver = token;
153:                            }
154:                        }
155:
156:                    }
157:                } catch (IOException ioe) {
158:                    logger.printStackTrace(ioe);
159:                    return null;
160:                }
161:
162:                System.out.println("Mail Server to use is :: " + mailserver);
163:                return mailserver;
164:            }
165:
166:            /**
167:             * Method main.
168:             * @param args
169:             * Usuage :: <mxServer> <toAddress> <fromAddress> <subject> <text>
170:             */
171:            /*
172:            public static void main(String args[]) {
173:            	if (args.length < 5) {
174:            		System.out.println("Usuage :: <mxServer> <toAddress> <fromAddress> <subject> <text>");
175:            		System.exit(1);
176:            	}
177:            	String msgText = "";
178:                        for (int i = 4; i < args.length; i++) {
179:            		msgText += (" " +args[i]);
180:            	}
181:
182:                new SendMail(args[0], args[1], args[2], args[3], msgText).send();
183:            }
184:             */
185:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.