KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > MessageInfo


1 /*
2  * @(#)MessageInfo.java 1.4 02/04/04
3  *
4  * Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  *
38  */

39
40 package demo;
41
42 import java.text.*;
43 import java.util.*;
44 import javax.mail.*;
45 import javax.mail.internet.*;
46
47 /**
48  * Used to store message information.
49  */

50 public class MessageInfo {
51     private Message message;
52     
53     
54     /**
55      * Returns the bcc field.
56      */

57     public String JavaDoc getBcc() throws MessagingException {
58         return formatAddresses(
59             message.getRecipients(Message.RecipientType.BCC));
60     }
61     
62     /**
63      * Returns the body of the message (if it's plain text).
64      */

65     public String JavaDoc getBody() throws MessagingException, java.io.IOException JavaDoc {
66         Object JavaDoc content = message.getContent();
67         if (message.isMimeType("text/plain")) {
68             return (String JavaDoc)content;
69         } else if (message.isMimeType("multipart/alternative")) {
70         Multipart mp = (Multipart)message.getContent();
71             int numParts = mp.getCount();
72             for (int i = 0; i < numParts; ++i) {
73                 if (mp.getBodyPart(i).isMimeType("text/plain"))
74                     return (String JavaDoc)mp.getBodyPart(i).getContent();
75             }
76             return "";
77         } else if (message.isMimeType("multipart/*")) {
78         Multipart mp = (Multipart)content;
79             if (mp.getBodyPart(0).isMimeType("text/plain"))
80                 return (String JavaDoc)mp.getBodyPart(0).getContent();
81             else
82                 return "";
83         } else
84             return "";
85     }
86     
87     /**
88      * Returns the cc field.
89      */

90     public String JavaDoc getCc() throws MessagingException {
91         return formatAddresses(
92             message.getRecipients(Message.RecipientType.CC));
93     }
94     
95     /**
96      * Returns the date the message was sent (or received if the sent date
97      * is null.
98      */

99     public String JavaDoc getDate() throws MessagingException {
100         Date date;
101         SimpleDateFormat df = new SimpleDateFormat("EE M/d/yy");
102         if ((date = message.getSentDate()) != null)
103             return (df.format(date));
104         else if ((date = message.getReceivedDate()) != null)
105             return (df.format(date));
106         else
107             return "";
108      }
109        
110     /**
111      * Returns the from field.
112      */

113     public String JavaDoc getFrom() throws MessagingException {
114         return formatAddresses(message.getFrom());
115     }
116
117     /**
118      * Returns the address to reply to.
119      */

120     public String JavaDoc getReplyTo() throws MessagingException {
121     Address[] a = message.getReplyTo();
122     if (a.length > 0)
123         return ((InternetAddress)a[0]).getAddress();
124     else
125         return "";
126     }
127     
128     /**
129      * Returns the javax.mail.Message object.
130      */

131     public Message getMessage() {
132         return message;
133     }
134     
135     /**
136      * Returns the message number.
137      */

138     public String JavaDoc getNum() {
139         return (Integer.toString(message.getMessageNumber()));
140     }
141     
142     /**
143      * Returns the received date field.
144      */

145     public String JavaDoc getReceivedDate() throws MessagingException {
146         if (hasReceivedDate())
147             return (message.getReceivedDate().toString());
148         else
149             return "";
150     }
151     
152     /**
153      * Returns the sent date field.
154      */

155     public String JavaDoc getSentDate() throws MessagingException {
156         if (hasSentDate())
157             return (message.getSentDate().toString());
158         else
159             return "";
160     }
161     
162     /**
163      * Returns the subject field.
164      */

165     public String JavaDoc getSubject() throws MessagingException {
166         if (hasSubject())
167             return message.getSubject();
168         else
169             return "";
170     }
171     
172     /**
173      * Returns the to field.
174      */

175     public String JavaDoc getTo() throws MessagingException {
176         return formatAddresses(
177             message.getRecipients(Message.RecipientType.TO));
178     }
179     
180     /**
181      * Method for checking if the message has attachments.
182      */

183     public boolean hasAttachments() throws java.io.IOException JavaDoc,
184                                            MessagingException {
185         boolean hasAttachments = false;
186         if (message.isMimeType("multipart/*")) {
187         Multipart mp = (Multipart)message.getContent();
188             if (mp.getCount() > 1)
189                 hasAttachments = true;
190         }
191             
192         return hasAttachments;
193     }
194     
195     /**
196      * Method for checking if the message has a bcc field.
197      */

198     public boolean hasBcc() throws MessagingException {
199         return (message.getRecipients(Message.RecipientType.BCC) != null);
200     }
201     
202     /**
203      * Method for checking if the message has a cc field.
204      */

205     public boolean hasCc() throws MessagingException {
206         return (message.getRecipients(Message.RecipientType.CC) != null);
207     }
208     
209     /**
210      * Method for checking if the message has a date field.
211      */

212     public boolean hasDate() throws MessagingException {
213         return (hasSentDate() || hasReceivedDate());
214     }
215     
216     /**
217      * Method for checking if the message has a from field.
218      */

219     public boolean hasFrom() throws MessagingException {
220         return (message.getFrom() != null);
221     }
222     
223     /**
224      * Method for checking if the message has the desired mime type.
225      */

226     public boolean hasMimeType(String JavaDoc mimeType) throws MessagingException {
227         return message.isMimeType(mimeType);
228     }
229     
230     /**
231      * Method for checking if the message has a received date field.
232      */

233     public boolean hasReceivedDate() throws MessagingException {
234         return (message.getReceivedDate() != null);
235     }
236     
237     /**
238      * Method for checking if the message has a sent date field.
239      */

240     public boolean hasSentDate() throws MessagingException {
241         return (message.getSentDate() != null);
242     }
243     
244     /**
245      * Method for checking if the message has a subject field.
246      */

247     public boolean hasSubject() throws MessagingException {
248         return (message.getSubject() != null);
249     }
250     
251     /**
252      * Method for checking if the message has a to field.
253      */

254     public boolean hasTo() throws MessagingException {
255         return (message.getRecipients(Message.RecipientType.TO) != null);
256     }
257     
258     /**
259      * Method for mapping a message to this MessageInfo class.
260      */

261     public void setMessage(Message message) {
262         this.message = message;
263     }
264
265     /**
266      * Utility method for formatting msg header addresses.
267      */

268     private String JavaDoc formatAddresses(Address[] addrs) {
269         if (addrs == null)
270             return "";
271         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc(getDisplayAddress(addrs[0]));
272         for (int i = 1; i < addrs.length; i++) {
273             strBuf.append(", ").append(getDisplayAddress(addrs[i]));
274         }
275         return strBuf.toString();
276     }
277     
278     /**
279      * Utility method which returns a string suitable for msg header display.
280      */

281     private String JavaDoc getDisplayAddress(Address a) {
282         String JavaDoc pers = null;
283         String JavaDoc addr = null;
284         if (a instanceof InternetAddress &&
285            ((pers = ((InternetAddress)a).getPersonal()) != null)) {
286         addr = pers + " "+"&lt;"+((InternetAddress)a).getAddress()+"&gt;";
287         } else
288             addr = a.toString();
289         return addr;
290     }
291 }
292
293
Popular Tags