Source Code Cross Referenced for EMailSender.java in  » Content-Management-System » contineo » org » contineo » core » communication » 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 » Content Management System » contineo » org.contineo.core.communication 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.contineo.core.communication;
002:
003:        import java.util.Collection;
004:        import java.util.Iterator;
005:        import java.util.Properties;
006:
007:        import javax.activation.DataHandler;
008:        import javax.activation.DataSource;
009:        import javax.activation.FileDataSource;
010:        import javax.mail.Address;
011:        import javax.mail.Multipart;
012:        import javax.mail.Session;
013:        import javax.mail.Transport;
014:        import javax.mail.internet.InternetAddress;
015:        import javax.mail.internet.MimeBodyPart;
016:        import javax.mail.internet.MimeMessage;
017:        import javax.mail.internet.MimeMultipart;
018:
019:        import org.apache.commons.lang.StringUtils;
020:        import org.contineo.util.Context;
021:        import org.contineo.util.config.SettingsConfig;
022:
023:        /**
024:         * SMTP E-Mail sender service 
025:         * 
026:         * @author Michael Scholz, Marco Meschieri
027:         */
028:        public class EMailSender {
029:
030:            private String host = "localhost";
031:
032:            private String defaultAddress = "contineo@acme.com";
033:
034:            private String username = "";
035:
036:            private String password = "";
037:
038:            private int port = 25;
039:
040:            public EMailSender() {
041:            }
042:
043:            public String getDefaultAddress() {
044:                return defaultAddress;
045:            }
046:
047:            public void setDefaultAddress(String fromAddress) {
048:                this .defaultAddress = fromAddress;
049:            }
050:
051:            public String getHost() {
052:                return host;
053:            }
054:
055:            public void setHost(String host) {
056:                this .host = host;
057:            }
058:
059:            public String getPassword() {
060:                return password;
061:            }
062:
063:            public void setPassword(String password) {
064:                this .password = password;
065:            }
066:
067:            public int getPort() {
068:                return port;
069:            }
070:
071:            public void setPort(int port) {
072:                this .port = port;
073:            }
074:
075:            public String getUsername() {
076:                return username;
077:            }
078:
079:            public void setUsername(String username) {
080:                this .username = username;
081:            }
082:
083:            /**
084:             * This method sends an email using the smtp-protocol. The email can be a
085:             * simple mail or a multipart mail containing up to 5 attachments.
086:             * 
087:             * @param account E-Mail account of the sender.
088:             * @param email E-Mail which should be sent.
089:             * @throws Exception
090:             */
091:            public void send(EMail email) throws Exception {
092:                Properties props = new Properties();
093:                props.put("mail.smtp.host", host);
094:                props.put("mail.smtp.auth", "true");
095:
096:                Session sess = Session.getDefaultInstance(props);
097:                javax.mail.Message message = new MimeMessage(sess);
098:                String frm = email.getAuthorAddress();
099:                if (StringUtils.isEmpty(frm))
100:                    frm = defaultAddress;
101:                InternetAddress from = new InternetAddress(frm);
102:                InternetAddress[] to = email.getAddresses();
103:                message.setFrom(from);
104:                message.setRecipients(javax.mail.Message.RecipientType.TO, to);
105:                message.setSubject(email.getSubject());
106:
107:                if (email.getAttachmentCount() > 0) {
108:                    SettingsConfig conf = (SettingsConfig) Context
109:                            .getInstance().getBean(SettingsConfig.class);
110:                    String userdir = conf.getValue("userdir");
111:                    String filename = userdir + "/mails/"
112:                            + email.getMessageId() + "/";
113:                    MimeBodyPart body = new MimeBodyPart();
114:                    body.setContent(email.getMessageText(), "text/plain");
115:
116:                    Multipart mpMessage = new MimeMultipart();
117:                    mpMessage.addBodyPart(body);
118:
119:                    Collection attachments = email.getAttachments().values();
120:                    Iterator iter = attachments.iterator();
121:
122:                    while (iter.hasNext()) {
123:                        Attachment att = (Attachment) iter.next();
124:                        DataSource fdSource = new FileDataSource(filename
125:                                + att.getFilename());
126:                        DataHandler fdHandler = new DataHandler(fdSource);
127:                        MimeBodyPart part = new MimeBodyPart();
128:                        part.setDataHandler(fdHandler);
129:                        part.setFileName(att.getFilename());
130:                        mpMessage.addBodyPart(part);
131:                    }
132:
133:                    message.setContent(mpMessage);
134:                } else {
135:                    message.setContent(email.getMessageText(), "text/plain");
136:                }
137:
138:                Transport trans = sess.getTransport("smtp");
139:                trans.connect(host, port, username, password);
140:
141:                Address[] adr = message.getAllRecipients();
142:                trans.sendMessage(message, adr);
143:                trans.close();
144:            }
145:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.