Source Code Cross Referenced for MailProvider.java in  » Report » openreports » org » efs » openreports » providers » 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 » Report » openreports » org.efs.openreports.providers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) 2003 Erik Swenson - erik@oreports.com
003:         * 
004:         * This program is free software; you can redistribute it and/or modify it under the terms of the
005:         * GNU General Public License as published by the Free Software Foundation; either version 2 of the
006:         * License, or (at your option) any later version.
007:         * 
008:         * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
009:         * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
010:         * the GNU General Public License for more details.
011:         * 
012:         * You should have received a copy of the GNU General Public License along with this program; if
013:         * not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
014:         * 02111-1307, USA.
015:         *  
016:         */
017:
018:        package org.efs.openreports.providers;
019:
020:        import java.util.ArrayList;
021:        import java.util.Date;
022:        import java.util.Properties;
023:        import java.util.StringTokenizer;
024:
025:        import javax.activation.DataHandler;
026:        import javax.activation.DataSource;
027:        import javax.activation.FileDataSource;
028:        import javax.mail.*;
029:        import javax.mail.Message.RecipientType;
030:        import javax.mail.internet.*;
031:
032:        import org.apache.log4j.Logger;
033:
034:        import org.efs.openreports.objects.MailMessage;
035:        import org.efs.openreports.objects.ORProperty;
036:        import org.efs.openreports.util.ByteArrayDataSource;
037:        import org.efs.openreports.util.SMTPAuthenticator;
038:
039:        public class MailProvider {
040:            protected static Logger log = Logger.getLogger(MailProvider.class
041:                    .getName());
042:
043:            private PropertiesProvider propertiesProvider;
044:
045:            private String mailHost;
046:            private boolean useMailAuthenticator;
047:            private String userName;
048:            private String password;
049:            private Session mailSession;
050:
051:            public MailProvider(PropertiesProvider propertiesProvider)
052:                    throws ProviderException {
053:                this .propertiesProvider = propertiesProvider;
054:                init();
055:            }
056:
057:            protected void init() throws ProviderException {
058:                mailHost = null;
059:                useMailAuthenticator = false;
060:                userName = null;
061:                password = null;
062:
063:                ORProperty property = propertiesProvider
064:                        .getProperty(ORProperty.MAIL_SMTP_HOST);
065:                if (property != null)
066:                    mailHost = property.getValue();
067:
068:                property = propertiesProvider
069:                        .getProperty(ORProperty.MAIL_SMTP_AUTH);
070:                if (property != null)
071:                    useMailAuthenticator = Boolean.valueOf(property.getValue())
072:                            .booleanValue();
073:
074:                property = propertiesProvider
075:                        .getProperty(ORProperty.MAIL_AUTH_USER);
076:                if (property != null)
077:                    userName = property.getValue();
078:
079:                property = propertiesProvider
080:                        .getProperty(ORProperty.MAIL_AUTH_PASSWORD);
081:                if (property != null)
082:                    password = property.getValue();
083:
084:                log.info("Created: Use Mail Authenticator = "
085:                        + useMailAuthenticator);
086:            }
087:
088:            public void sendMail(MailMessage mail) throws ProviderException {
089:                try {
090:                    if (mailSession == null) {
091:                        // create a session
092:                        Properties mailProps = new Properties();
093:                        mailProps.put("mail.smtp.host", mailHost);
094:
095:                        if (mail.getBounceAddress() != null
096:                                && mail.getBounceAddress().trim().length() > 0) {
097:                            mailProps.setProperty("mail.smtp.from", mail
098:                                    .getBounceAddress());
099:                        }
100:
101:                        if (useMailAuthenticator) {
102:                            mailSession = Session.getInstance(mailProps,
103:                                    new SMTPAuthenticator(userName, password));
104:                        } else {
105:                            mailSession = Session.getInstance(mailProps, null);
106:                        }
107:                    }
108:
109:                    // create multipart
110:                    Multipart multipart = new MimeMultipart();
111:
112:                    // add text part
113:                    if (mail.getText() != null) {
114:                        MimeBodyPart mbpText = new MimeBodyPart();
115:                        mbpText.setText(mail.getText());
116:                        multipart.addBodyPart(mbpText);
117:                    }
118:
119:                    // add file attachments
120:                    ArrayList<String> attachments = mail.getAttachments();
121:                    for (int i = 0; i < attachments.size(); i++) {
122:                        String fileAttachment = attachments.get(i);
123:                        FileDataSource source = new FileDataSource(
124:                                fileAttachment);
125:
126:                        MimeBodyPart mbpAttachment = new MimeBodyPart();
127:                        mbpAttachment.setDataHandler(new DataHandler(source));
128:                        mbpAttachment.setFileName(fileAttachment);
129:
130:                        multipart.addBodyPart(mbpAttachment);
131:                    }
132:
133:                    // add byteArrayAttachment
134:                    if (mail.getByteArrayDataSource() != null) {
135:                        String contentType = mail.getByteArrayDataSource()
136:                                .getContentType();
137:                        if (contentType != null
138:                                && contentType.equals("text/html")) {
139:                            Multipart htmlMP = new MimeMultipart("related");
140:                            MimeBodyPart htmlBP = new MimeBodyPart();
141:                            htmlBP.setDataHandler(new DataHandler(mail
142:                                    .getByteArrayDataSource()));
143:                            htmlMP.addBodyPart(htmlBP);
144:
145:                            // Add images
146:                            ArrayList<ByteArrayDataSource> images = mail
147:                                    .getHtmlImageDataSources();
148:                            for (int i = 0; i < images.size(); i++) {
149:                                DataSource imageDS = images.get(i);
150:
151:                                MimeBodyPart imageBodyPart = new MimeBodyPart();
152:                                imageBodyPart.setFileName(imageDS.getName());
153:                                imageBodyPart.setText(imageDS.getName());
154:                                imageBodyPart.setDataHandler(new DataHandler(
155:                                        imageDS));
156:                                imageBodyPart.setHeader("Content-ID", "<"
157:                                        + imageDS.getName() + ">");
158:                                imageBodyPart
159:                                        .setDisposition(javax.mail.Part.INLINE);
160:
161:                                htmlMP.addBodyPart(imageBodyPart);
162:                            }
163:
164:                            BodyPart completeHtmlBP = new MimeBodyPart();
165:                            completeHtmlBP.setContent(htmlMP);
166:                            multipart.addBodyPart(completeHtmlBP);
167:                        } else {
168:                            MimeBodyPart mbpAttachment = new MimeBodyPart();
169:                            mbpAttachment.setDataHandler(new DataHandler(mail
170:                                    .getByteArrayDataSource()));
171:                            mbpAttachment.setFileName(mail
172:                                    .getByteArrayDataSource().getName());
173:
174:                            multipart.addBodyPart(mbpAttachment);
175:                        }
176:                    }
177:
178:                    // create message
179:                    Message msg = new MimeMessage(mailSession);
180:                    msg.setFrom(new InternetAddress(mail.getSender()));
181:                    msg.setSubject(mail.getSubject());
182:                    msg.setContent(multipart);
183:                    msg.setSentDate(new Date());
184:
185:                    ArrayList<String> recipients = mail.getRecipients();
186:                    for (int i = 0; i < recipients.size(); i++) {
187:                        RecipientType recipientType = RecipientType.TO;
188:
189:                        StringTokenizer tokenizer = new StringTokenizer(
190:                                recipients.get(i), ":");
191:                        if (tokenizer.countTokens() == 2) {
192:                            String type = tokenizer.nextToken();
193:                            if ("TO".equalsIgnoreCase(type)) {
194:                                recipientType = RecipientType.TO;
195:                            } else if ("CC".equalsIgnoreCase(type)) {
196:                                recipientType = RecipientType.CC;
197:                            } else if ("BCC".equalsIgnoreCase(type)) {
198:                                recipientType = RecipientType.BCC;
199:                            }
200:                        }
201:
202:                        msg.addRecipient(recipientType, new InternetAddress(
203:                                tokenizer.nextToken()));
204:                    }
205:
206:                    Transport.send(msg);
207:                } catch (Exception e) {
208:                    e.printStackTrace();
209:                    log.error(e.toString());
210:                    throw new ProviderException(e.getMessage());
211:                }
212:            }
213:
214:            public void setMailHost(String mailHost) {
215:                this .mailHost = mailHost;
216:            }
217:
218:            public void setPassword(String password) {
219:                this .password = password;
220:            }
221:
222:            public void setUseMailAuthenticator(boolean useMailAuthenticator) {
223:                this .useMailAuthenticator = useMailAuthenticator;
224:            }
225:
226:            public void setUserName(String userName) {
227:                this .userName = userName;
228:            }
229:
230:            public void setPropertiesProvider(
231:                    PropertiesProvider propertiesProvider) {
232:                this .propertiesProvider = propertiesProvider;
233:            }
234:
235:            public void setMailSession(Session mailSession) {
236:                this.mailSession = mailSession;
237:            }
238:
239:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.