Source Code Cross Referenced for POPMessage.java in  » Forum » JForum-2.1.8 » net » jforum » api » integration » mail » pop » 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 » Forum » JForum 2.1.8 » net.jforum.api.integration.mail.pop 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Created on 21/08/2006 22:14:04
003:         */
004:        package net.jforum.api.integration.mail.pop;
005:
006:        import java.io.BufferedReader;
007:        import java.io.IOException;
008:        import java.io.InputStream;
009:        import java.io.InputStreamReader;
010:        import java.util.Date;
011:        import java.util.Enumeration;
012:        import java.util.HashMap;
013:        import java.util.Map;
014:
015:        import javax.mail.Header;
016:        import javax.mail.Message;
017:        import javax.mail.MessagingException;
018:        import javax.mail.Multipart;
019:        import javax.mail.Part;
020:        import javax.mail.internet.InternetAddress;
021:
022:        import net.jforum.exceptions.MailException;
023:
024:        /**
025:         * Represents a pop message. 
026:         * @author Rafael Steil
027:         * @version $Id: POPMessage.java,v 1.6 2006/10/09 00:54:08 rafaelsteil Exp $
028:         */
029:        public class POPMessage {
030:            private static final String IN_REPLY_TO = "In-Reply-To";
031:            private static final String REFERENCES = "References";
032:
033:            private String subject;
034:            private Object message;
035:            private String messageContents;
036:            private String sender;
037:            private String replyTo;
038:            private String references;
039:            private String inReplyTo;
040:            private String contentType;
041:            private String listEmail;
042:            private Date sendDate;
043:            private Map headers;
044:
045:            /**
046:             * Creates a new instance based on a {@link Message}
047:             * @param message the message to convert from.
048:             */
049:            public POPMessage(Message message) {
050:                this .extract(message);
051:            }
052:
053:            /**
054:             * Given a {@link Message}, converts it to our internal format
055:             * @param message the message to convert
056:             */
057:            private void extract(Message message) {
058:                try {
059:                    this .subject = message.getSubject();
060:
061:                    this .message = message.getContent();
062:                    this .contentType = message.getContentType();
063:                    this .sender = ((InternetAddress) message.getFrom()[0])
064:                            .getAddress();
065:                    this .listEmail = ((InternetAddress) message
066:                            .getAllRecipients()[0]).getAddress();
067:                    this .sendDate = message.getSentDate();
068:
069:                    if (message.getReplyTo().length > 0) {
070:                        this .replyTo = ((InternetAddress) message.getReplyTo()[0])
071:                                .getAddress();
072:                    } else {
073:                        this .replyTo = this .sender;
074:                    }
075:
076:                    this .headers = new HashMap();
077:
078:                    for (Enumeration e = message.getAllHeaders(); e
079:                            .hasMoreElements();) {
080:                        Header header = (Header) e.nextElement();
081:                        this .headers.put(header.getName(), header.getValue());
082:                    }
083:
084:                    if (this .headers.containsKey(IN_REPLY_TO)) {
085:                        this .inReplyTo = this .headers.get(IN_REPLY_TO)
086:                                .toString();
087:                    }
088:
089:                    if (this .headers.containsKey(REFERENCES)) {
090:                        this .references = this .headers.get(REFERENCES)
091:                                .toString();
092:                    }
093:
094:                    this .extractMessageContents(message);
095:                } catch (Exception e) {
096:
097:                }
098:            }
099:
100:            private void extractMessageContents(Message m)
101:                    throws MessagingException {
102:                Part messagePart = m;
103:
104:                if (this .message instanceof  Multipart) {
105:                    messagePart = ((Multipart) this .message).getBodyPart(0);
106:                }
107:
108:                if (contentType.startsWith("text/html")
109:                        || contentType.startsWith("text/plain")) {
110:                    InputStream is = null;
111:                    BufferedReader reader = null;
112:
113:                    try {
114:                        is = messagePart.getInputStream();
115:                        is.reset();
116:                        reader = new BufferedReader(new InputStreamReader(is));
117:
118:                        StringBuffer sb = new StringBuffer(512);
119:                        int c = 0;
120:                        char[] ch = new char[2048];
121:
122:                        while ((c = reader.read(ch)) != -1) {
123:                            sb.append(ch, 0, c);
124:                        }
125:
126:                        this .messageContents = sb.toString();
127:                    } catch (IOException e) {
128:                        throw new MailException(e);
129:                    } finally {
130:                        if (reader != null) {
131:                            try {
132:                                reader.close();
133:                            } catch (Exception e) {
134:                            }
135:                        }
136:                        if (is != null) {
137:                            try {
138:                                is.close();
139:                            } catch (Exception e) {
140:                            }
141:                        }
142:                    }
143:                }
144:            }
145:
146:            public String getListEmail() {
147:                return this .listEmail;
148:            }
149:
150:            /**
151:             * @return the contentType
152:             */
153:            public String getContentType() {
154:                return this .contentType;
155:            }
156:
157:            /**
158:             * @return the headers
159:             */
160:            public Map getHeaders() {
161:                return this .headers;
162:            }
163:
164:            /**
165:             * @return the inReplyTo
166:             */
167:            public String getInReplyTo() {
168:                return this .inReplyTo;
169:            }
170:
171:            /**
172:             * @return the message
173:             */
174:            public String getMessage() {
175:                return this .messageContents;
176:            }
177:
178:            /**
179:             * @return the references
180:             */
181:            public String getReferences() {
182:                return this .references;
183:            }
184:
185:            /**
186:             * @return the replyTo
187:             */
188:            public String getReplyTo() {
189:                return this .replyTo;
190:            }
191:
192:            /**
193:             * @return the sendDate
194:             */
195:            public Date getSendDate() {
196:                return this .sendDate;
197:            }
198:
199:            /**
200:             * @return the sender
201:             */
202:            public String getSender() {
203:                return this .sender;
204:            }
205:
206:            /**
207:             * @return the subject
208:             */
209:            public String getSubject() {
210:                return this .subject;
211:            }
212:
213:            /**
214:             * @param contentType the contentType to set
215:             */
216:            public void setContentType(String contentType) {
217:                this .contentType = contentType;
218:            }
219:
220:            /**
221:             * @param headers the headers to set
222:             */
223:            public void setHeaders(Map headers) {
224:                this .headers = headers;
225:            }
226:
227:            /**
228:             * @param inReplyTo the inReplyTo to set
229:             */
230:            public void setInReplyTo(String inReplyTo) {
231:                this .inReplyTo = inReplyTo;
232:            }
233:
234:            /**
235:             * @param message the message to set
236:             */
237:            public void setMessage(Object message) {
238:                this .message = message;
239:            }
240:
241:            /**
242:             * @param references the references to set
243:             */
244:            public void setReferences(String references) {
245:                this .references = references;
246:            }
247:
248:            /**
249:             * @param replyTo the replyTo to set
250:             */
251:            public void setReplyTo(String replyTo) {
252:                this .replyTo = replyTo;
253:            }
254:
255:            /**
256:             * @param sendDate the sendDate to set
257:             */
258:            public void setSendDate(Date sendDate) {
259:                this .sendDate = sendDate;
260:            }
261:
262:            /**
263:             * @param sender the sender to set
264:             */
265:            public void setSender(String sender) {
266:                this .sender = sender;
267:            }
268:
269:            /**
270:             * @param subject the subject to set
271:             */
272:            public void setSubject(String subject) {
273:                this .subject = subject;
274:            }
275:
276:            /**
277:             * @see java.lang.Object#toString()
278:             */
279:            public String toString() {
280:                return new StringBuffer().append('[').append(", subject=")
281:                        .append(this .subject).append(", sender=").append(
282:                                this .sender).append(", replyTo=").append(
283:                                this .replyTo).append(", references=").append(
284:                                this .references).append(", inReplyTo=").append(
285:                                this .inReplyTo).append(", contentType=")
286:                        .append(this .contentType).append(", date=").append(
287:                                this .sendDate).append(", content=").append(
288:                                this .messageContents).append(", headers=")
289:                        .append(this .headers).append(']').toString();
290:            }
291:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.