Source Code Cross Referenced for SendMail.java in  » Development » jodd » jodd » 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 » Development » jodd » jodd.mail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
002:
003:        package jodd.mail;
004:
005:        import java.util.Date;
006:        import java.util.HashMap;
007:        import java.util.Iterator;
008:        import java.util.Properties;
009:
010:        import javax.mail.Authenticator;
011:        import javax.mail.Message;
012:        import javax.mail.MessagingException;
013:        import javax.mail.Multipart;
014:        import javax.mail.Session;
015:        import javax.mail.Transport;
016:        import javax.mail.internet.InternetAddress;
017:        import javax.mail.internet.MimeBodyPart;
018:        import javax.mail.internet.MimeMessage;
019:        import javax.mail.internet.MimeMultipart;
020:
021:        /**
022:         * Sends e-mails in an easy way.
023:         *
024:         * todo i18n
025:         */
026:        public class SendMail {
027:
028:            private static SmtpServer defaultSmtpServer;
029:
030:            /**
031:             * Sets default SMTP server.
032:             *
033:             * @param server default SMTP server
034:             */
035:            public static void setDefaultSmtpServer(SmtpServer server) {
036:                defaultSmtpServer = server;
037:            }
038:
039:            /**
040:             * Returns default SMTP server.
041:             *
042:             * @return default SMTP server
043:             */
044:            public static SmtpServer getDefaultSmtpServer() {
045:                return defaultSmtpServer;
046:            }
047:
048:            // ---------------------------------------------------------------- send
049:
050:            /**
051:             * Sends an email through default SMTP server.
052:             *
053:             * @param email      email
054:             *
055:             * @exception MessagingException
056:             */
057:            public static void send(Email email) throws MessagingException {
058:                send(email, null);
059:            }
060:
061:            /**
062:             * Sends an email through SMTP server.
063:             *
064:             * @param email      email
065:             * @param smtpServer SMTP server or null for default one
066:             *
067:             * @exception MessagingException
068:             */
069:            public static void send(Email email, SmtpServer smtpServer)
070:                    throws MessagingException {
071:                if (smtpServer == null) {
072:                    smtpServer = defaultSmtpServer;
073:                }
074:
075:                Properties props = new Properties();
076:                props.put("mail.smtp.host", smtpServer.getHost());
077:                Authenticator auth = null;
078:                if (smtpServer.getUsername() != null) {
079:                    props.put("mail.smtp.auth", "true");
080:                    auth = new SmtpSimpleAuthenticator(
081:                            smtpServer.getUsername(), smtpServer.getPassword());
082:                }
083:                Session session = Session.getInstance(props, auth);
084:
085:                Message msg = new MimeMessage(session);
086:                msg.setFrom(new InternetAddress(email.getFrom()));
087:
088:                int totalTo = email.getTo().length;
089:                InternetAddress[] address = new InternetAddress[totalTo];
090:                for (int i = 0; i < totalTo; i++) {
091:                    address[i] = new InternetAddress(email.getTo()[i]);
092:                }
093:                msg.setRecipients(Message.RecipientType.TO, address);
094:
095:                if (email.getCc() != null) {
096:                    int totalCc = email.getCc().length;
097:                    address = new InternetAddress[totalCc];
098:                    for (int i = 0; i < totalCc; i++) {
099:                        address[i] = new InternetAddress(email.getCc()[i]);
100:                    }
101:                    msg.setRecipients(Message.RecipientType.CC, address);
102:                }
103:                if (email.getBcc() != null) {
104:                    int totalBcc = email.getBcc().length;
105:                    address = new InternetAddress[totalBcc];
106:                    for (int i = 0; i < totalBcc; i++) {
107:                        address[i] = new InternetAddress(email.getBcc()[i]);
108:                    }
109:                    msg.setRecipients(Message.RecipientType.BCC, address);
110:                }
111:
112:                msg.setSubject(email.getSubject());
113:                Date date = email.getSentDate();
114:                if (date == null) {
115:                    date = new Date();
116:                }
117:                msg.setSentDate(date);
118:                HashMap headers = email.getHeaders();
119:                Iterator it = headers.keySet().iterator();
120:                while (it.hasNext()) {
121:                    String name = (String) it.next();
122:                    String value = (String) headers.get(name);
123:                    msg.setHeader(name, value);
124:                }
125:
126:                MimeBodyPart messageData = new MimeBodyPart();
127:
128:                if (email.isHtmlMessage()) {
129:                    messageData.setContent(email.getMessage(), "text/html");
130:                } else {
131:                    //messageBodyPart.setContent(email.getMessage(), "text/plain; charset=\"Windows-1250\";");
132:                    //messageBodyPart.setText(email.getMessage(), "Windows-1250");
133:                    messageData.setText(email.getMessage());
134:                }
135:
136:                Multipart multipart = new MimeMultipart();
137:                multipart.addBodyPart(messageData);
138:
139:                int totalAttachments = email.getTotalAttachments();
140:                for (int i = 0; i < totalAttachments; i++) {
141:                    multipart.addBodyPart(email.getAttachmentBodyPart(i));
142:                }
143:
144:                msg.setContent(multipart);
145:                Transport.send(msg);
146:            }
147:
148:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.