Source Code Cross Referenced for MailEngine.java in  » Portal » liferay-portal-4.4.2 » com » liferay » util » 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 » Portal » liferay portal 4.4.2 » com.liferay.util.mail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003:         *
004:         * Permission is hereby granted, free of charge, to any person obtaining a copy
005:         * of this software and associated documentation files (the "Software"), to deal
006:         * in the Software without restriction, including without limitation the rights
007:         * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008:         * copies of the Software, and to permit persons to whom the Software is
009:         * furnished to do so, subject to the following conditions:
010:         *
011:         * The above copyright notice and this permission notice shall be included in
012:         * all copies or substantial portions of the Software.
013:         *
014:         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015:         * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016:         * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017:         * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018:         * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019:         * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020:         * SOFTWARE.
021:         */package com.liferay.util.mail;
022:
023:        import com.liferay.portal.kernel.jndi.PortalJNDIUtil;
024:        import com.liferay.portal.kernel.mail.MailMessage;
025:        import com.liferay.portal.kernel.util.GetterUtil;
026:        import com.liferay.portal.kernel.util.Validator;
027:
028:        import java.io.ByteArrayInputStream;
029:        import java.io.File;
030:
031:        import java.net.SocketException;
032:
033:        import java.util.Date;
034:
035:        import javax.activation.DataHandler;
036:        import javax.activation.DataSource;
037:        import javax.activation.FileDataSource;
038:
039:        import javax.mail.Message;
040:        import javax.mail.MessagingException;
041:        import javax.mail.Part;
042:        import javax.mail.SendFailedException;
043:        import javax.mail.Session;
044:        import javax.mail.Transport;
045:        import javax.mail.internet.AddressException;
046:        import javax.mail.internet.InternetAddress;
047:        import javax.mail.internet.MimeBodyPart;
048:        import javax.mail.internet.MimeMessage;
049:        import javax.mail.internet.MimeMultipart;
050:
051:        import javax.naming.NamingException;
052:
053:        import org.apache.commons.lang.time.StopWatch;
054:        import org.apache.commons.logging.Log;
055:        import org.apache.commons.logging.LogFactory;
056:
057:        /**
058:         * <a href="MailEngine.java.html"><b><i>View Source</i></b></a>
059:         *
060:         * @author Brian Wing Shun Chan
061:         * @author Brian Myunghun Kim
062:         * @author Jorge Ferrer
063:         * @author Neil Griffin
064:         *
065:         */
066:        public class MailEngine {
067:
068:            public static Session getSession() throws NamingException {
069:                return getSession(false);
070:            }
071:
072:            public static Session getSession(boolean cache)
073:                    throws NamingException {
074:                Session session = PortalJNDIUtil.getMailSession();
075:
076:                session.setDebug(_log.isDebugEnabled());
077:
078:                if (_log.isDebugEnabled()) {
079:                    session.getProperties().list(System.out);
080:                }
081:
082:                return session;
083:            }
084:
085:            public static void send(MailMessage mailMessage)
086:                    throws MailEngineException {
087:
088:                send(mailMessage.getFrom(), mailMessage.getTo(), mailMessage
089:                        .getCC(), mailMessage.getBCC(), mailMessage
090:                        .getSubject(), mailMessage.getBody(), mailMessage
091:                        .isHTMLFormat(), mailMessage.getReplyTo(), mailMessage
092:                        .getMessageId(), mailMessage.getInReplyTo(),
093:                        mailMessage.getAttachments());
094:            }
095:
096:            public static void send(String from, String to, String subject,
097:                    String body) throws MailEngineException {
098:
099:                try {
100:                    send(new InternetAddress(from), new InternetAddress(to),
101:                            subject, body);
102:                } catch (AddressException ae) {
103:                    throw new MailEngineException(ae);
104:                }
105:            }
106:
107:            public static void send(InternetAddress from, InternetAddress to,
108:                    String subject, String body) throws MailEngineException {
109:
110:                send(from, new InternetAddress[] { to }, null, null, subject,
111:                        body, false, null, null, null);
112:            }
113:
114:            public static void send(InternetAddress from, InternetAddress to,
115:                    String subject, String body, boolean htmlFormat)
116:                    throws MailEngineException {
117:
118:                send(from, new InternetAddress[] { to }, null, null, subject,
119:                        body, htmlFormat, null, null, null);
120:            }
121:
122:            public static void send(InternetAddress from, InternetAddress[] to,
123:                    String subject, String body) throws MailEngineException {
124:
125:                send(from, to, null, null, subject, body, false, null, null,
126:                        null);
127:            }
128:
129:            public static void send(InternetAddress from, InternetAddress[] to,
130:                    String subject, String body, boolean htmlFormat)
131:                    throws MailEngineException {
132:
133:                send(from, to, null, null, subject, body, htmlFormat, null,
134:                        null, null);
135:            }
136:
137:            public static void send(InternetAddress from, InternetAddress[] to,
138:                    InternetAddress[] cc, String subject, String body)
139:                    throws MailEngineException {
140:
141:                send(from, to, cc, null, subject, body, false, null, null, null);
142:            }
143:
144:            public static void send(InternetAddress from, InternetAddress[] to,
145:                    InternetAddress[] cc, String subject, String body,
146:                    boolean htmlFormat) throws MailEngineException {
147:
148:                send(from, to, cc, null, subject, body, htmlFormat, null, null,
149:                        null);
150:            }
151:
152:            public static void send(InternetAddress from, InternetAddress[] to,
153:                    InternetAddress[] cc, InternetAddress[] bcc,
154:                    String subject, String body) throws MailEngineException {
155:
156:                send(from, to, cc, bcc, subject, body, false, null, null, null);
157:            }
158:
159:            public static void send(InternetAddress from, InternetAddress[] to,
160:                    InternetAddress[] cc, InternetAddress[] bcc,
161:                    String subject, String body, boolean htmlFormat,
162:                    InternetAddress[] replyTo, String messageId,
163:                    String inReplyTo) throws MailEngineException {
164:
165:                send(from, to, cc, bcc, subject, body, htmlFormat, replyTo,
166:                        messageId, inReplyTo, null);
167:            }
168:
169:            public static void send(InternetAddress from, InternetAddress[] to,
170:                    InternetAddress[] cc, InternetAddress[] bcc,
171:                    String subject, String body, boolean htmlFormat,
172:                    InternetAddress[] replyTo, String messageId,
173:                    String inReplyTo, File[] attachments)
174:                    throws MailEngineException {
175:
176:                StopWatch stopWatch = null;
177:
178:                if (_log.isDebugEnabled()) {
179:                    stopWatch = new StopWatch();
180:
181:                    stopWatch.start();
182:
183:                    _log.debug("From: " + from);
184:                    _log.debug("To: " + to);
185:                    _log.debug("CC: " + cc);
186:                    _log.debug("BCC: " + bcc);
187:                    _log.debug("Subject: " + subject);
188:                    _log.debug("Body: " + body);
189:                    _log.debug("HTML Format: " + htmlFormat);
190:                    _log.debug("Reply to: " + replyTo);
191:                    _log.debug("Message ID: " + messageId);
192:                    _log.debug("In Reply To: " + inReplyTo);
193:
194:                    if (attachments != null) {
195:                        for (int i = 0; i < attachments.length; i++) {
196:                            File attachment = attachments[i];
197:
198:                            if (attachment != null) {
199:                                String path = attachment.getAbsolutePath();
200:
201:                                _log.debug("Attachment #" + (i + 1) + ": "
202:                                        + path);
203:                            }
204:                        }
205:                    }
206:                }
207:
208:                try {
209:                    Session session = getSession();
210:
211:                    Message msg = new LiferayMimeMessage(session);
212:
213:                    msg.setFrom(from);
214:                    msg.setRecipients(Message.RecipientType.TO, to);
215:
216:                    if (cc != null) {
217:                        msg.setRecipients(Message.RecipientType.CC, cc);
218:                    }
219:
220:                    if (bcc != null) {
221:                        msg.setRecipients(Message.RecipientType.BCC, bcc);
222:                    }
223:
224:                    msg.setSubject(subject);
225:
226:                    if ((attachments != null) && (attachments.length > 0)) {
227:                        MimeMultipart rootMultipart = new MimeMultipart(
228:                                _MULTIPART_TYPE_MIXED);
229:
230:                        MimeMultipart messageMultipart = new MimeMultipart(
231:                                _MULTIPART_TYPE_ALTERNATIVE);
232:
233:                        MimeBodyPart messageBodyPart = new MimeBodyPart();
234:
235:                        messageBodyPart.setContent(messageMultipart);
236:
237:                        rootMultipart.addBodyPart(messageBodyPart);
238:
239:                        if (htmlFormat) {
240:                            MimeBodyPart bodyPart = new MimeBodyPart();
241:
242:                            bodyPart.setContent(body, _TEXT_HTML);
243:
244:                            messageMultipart.addBodyPart(bodyPart);
245:                        } else {
246:                            MimeBodyPart bodyPart = new MimeBodyPart();
247:
248:                            bodyPart.setText(body);
249:
250:                            messageMultipart.addBodyPart(bodyPart);
251:                        }
252:
253:                        for (int i = 0; i < attachments.length; i++) {
254:                            File attachment = attachments[i];
255:
256:                            if (attachment != null) {
257:                                MimeBodyPart bodyPart = new MimeBodyPart();
258:
259:                                DataSource source = new FileDataSource(
260:                                        attachment);
261:
262:                                bodyPart.setDisposition(Part.ATTACHMENT);
263:                                bodyPart
264:                                        .setDataHandler(new DataHandler(source));
265:                                bodyPart.setFileName(attachment.getName());
266:
267:                                rootMultipart.addBodyPart(bodyPart);
268:                            }
269:                        }
270:
271:                        msg.setContent(rootMultipart);
272:
273:                        msg.saveChanges();
274:                    } else {
275:                        if (htmlFormat) {
276:                            msg.setContent(body, _TEXT_HTML);
277:                        } else {
278:                            msg.setContent(body, _TEXT_PLAIN);
279:                        }
280:                    }
281:
282:                    msg.setSentDate(new Date());
283:
284:                    if (replyTo != null) {
285:                        msg.setReplyTo(replyTo);
286:                    }
287:
288:                    if (messageId != null) {
289:                        msg.setHeader("Message-ID", messageId);
290:                    }
291:
292:                    if (inReplyTo != null) {
293:                        msg.setHeader("In-Reply-To", inReplyTo);
294:                        msg.setHeader("References", inReplyTo);
295:                    }
296:
297:                    _send(session, msg);
298:                } catch (SendFailedException sfe) {
299:                    _log.error(sfe);
300:                } catch (Exception e) {
301:                    throw new MailEngineException(e);
302:                }
303:
304:                if (_log.isDebugEnabled()) {
305:                    _log.debug("Sending mail takes " + stopWatch.getTime()
306:                            + " ms");
307:                }
308:            }
309:
310:            public static void send(byte[] msgByteArray)
311:                    throws MailEngineException {
312:                try {
313:                    Session session = getSession();
314:
315:                    Message msg = new MimeMessage(session,
316:                            new ByteArrayInputStream(msgByteArray));
317:
318:                    _send(session, msg);
319:                } catch (Exception e) {
320:                    throw new MailEngineException(e);
321:                }
322:            }
323:
324:            private static void _send(Session session, Message msg)
325:                    throws MessagingException {
326:
327:                try {
328:                    boolean smtpAuth = GetterUtil.getBoolean(session
329:                            .getProperty("mail.smtp.auth"), false);
330:                    String smtpHost = session.getProperty("mail.smtp.host");
331:                    String user = session.getProperty("mail.smtp.user");
332:                    String password = session.getProperty("mail.smtp.password");
333:
334:                    if (smtpAuth && Validator.isNotNull(user)
335:                            && Validator.isNotNull(password)) {
336:
337:                        Transport transport = session.getTransport("smtp");
338:
339:                        transport.connect(smtpHost, user, password);
340:
341:                        transport.sendMessage(msg, msg.getAllRecipients());
342:
343:                        transport.close();
344:                    } else {
345:                        Transport.send(msg);
346:                    }
347:                } catch (MessagingException me) {
348:                    if (me.getNextException() instanceof  SocketException) {
349:                        if (_log.isWarnEnabled()) {
350:                            _log
351:                                    .warn("Failed to connect to a valid mail server. Please "
352:                                            + "make sure one is properly configured. "
353:                                            + me.getMessage());
354:                        }
355:                    }
356:                }
357:            }
358:
359:            private static final String _MULTIPART_TYPE_ALTERNATIVE = "alternative";
360:
361:            private static final String _MULTIPART_TYPE_MIXED = "mixed";
362:
363:            private static final String _TEXT_HTML = "text/html;charset=\"UTF-8\"";
364:
365:            private static final String _TEXT_PLAIN = "text/plain;charset=\"UTF-8\"";
366:
367:            private static Log _log = LogFactory.getLog(MailEngine.class);
368:
369:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.