KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > actions > email > Mail


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.actions.email;
27
28 import javax.mail.Address JavaDoc;
29 import javax.mail.Flags JavaDoc;
30 import java.util.Date JavaDoc;
31
32 public class Mail
33 {
34     String JavaDoc from, to, subject, body, flags, xmailer, mime;
35     boolean unread;
36     Date JavaDoc sendDate;
37     Object JavaDoc content;
38
39     javax.mail.Message JavaDoc m;
40
41     public Mail(javax.mail.Message JavaDoc inMail) throws Exception JavaDoc
42     {
43         m = inMail;
44
45         {
46             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
47             boolean first = true;
48             Address JavaDoc[] a;
49
50             if ((a = m.getFrom()) != null)
51             {
52                 for (int j = 0; j < a.length; j++)
53                 {
54                     if (first)
55                         first = false;
56                     else
57                         s.append(", ");
58                     s.append(a[j].toString());
59                 }
60             }
61
62             from = s.toString();
63         }
64
65         {
66             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
67             Address JavaDoc[] a;
68
69             boolean first = true;
70             if ((a = m.getRecipients(javax.mail.Message.RecipientType.TO)) != null)
71             {
72                 for (int j = 0; j < a.length; j++)
73                 {
74                     if (first)
75                         first = false;
76                     else
77                         s.append(", ");
78                     s.append(a[j].toString());
79                 }
80             }
81
82             to = s.toString();
83         }
84
85         subject = m.getSubject();
86         sendDate = m.getSentDate();
87         body = m.getContent().toString(); // FIXME this is not the proper way to handle this
88
mime = m.getDataHandler().getContentType();
89         content = m.getContent();
90
91         /*if (m.getContent() instanceof javax.mail.internet.MimeMultipart)
92         {
93             StringBuffer sb = new StringBuffer();
94             javax.mail.internet.MimeMultipart foo = (javax.mail.internet.MimeMultipart)m.getContent();
95             for (int i = 0; i != foo.getCount(); i++)
96             {
97                 javax.mail.Part part = foo.getBodyPart(i);
98         sb.append(part.getContentType());
99                 sb.append(" ");
100
101                 if (part.isMimeType("image/jpeg"))// || part.isMimeType("application/smil"))
102                 {
103                     java.io.OutputStream out = new java.io.FileOutputStream(new java.io.File("c:\\tmp", part.getFileName()));
104                     Utils.writeStreamToStream(part.getInputStream(), out);
105                     out.close();
106                 }
107                 else if (part.isMimeType("text/plain") && "mms.txt".equals(part.getFileName()))
108                 {
109                     sb.append(part.getContent());
110                 }
111
112                 sb.append(" <br /><br />\n");
113             }
114
115             test = sb;
116         }*/

117
118         {
119             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
120             Flags JavaDoc flags = m.getFlags();
121             Flags.Flag JavaDoc[] sf = flags.getSystemFlags(); // get the system flags
122

123             boolean first = true;
124             for (int i = 0; i < sf.length; i++)
125             {
126                 Flags.Flag JavaDoc f = sf[i];
127                 if (f == Flags.Flag.ANSWERED)
128                     s.append("\\Answered");
129                 else if (f == Flags.Flag.DELETED)
130                     s.append("\\Deleted");
131                 else if (f == Flags.Flag.DRAFT)
132                     s.append("\\Draft");
133                 else if (f == Flags.Flag.FLAGGED)
134                     s.append("\\Flagged");
135                 else if (f == Flags.Flag.RECENT)
136                     s.append("\\Recent");
137                 else if (f == Flags.Flag.SEEN)
138                     s.append("\\Seen");
139                 else
140                     continue; // skip it
141
if (first)
142                     first = false;
143                 else
144                     s.append(", ");
145             }
146
147             String JavaDoc[] uf = flags.getUserFlags(); // get the user flag strings
148
for (int i = 0; i < uf.length; i++)
149             {
150                 if (first)
151                     first = false;
152                 else
153                     s.append(", ");
154                 s.append(uf[i]);
155             }
156
157             this.flags = s.toString();
158         }
159
160         {
161             String JavaDoc[] hdrs = m.getHeader("X-Mailer");
162             if (hdrs != null)
163                 xmailer = hdrs[0];
164             else
165                 xmailer = "n/a";
166         }
167
168         unread = isNew(m);
169     }
170
171     public static boolean isNew(javax.mail.Message JavaDoc m) throws Exception JavaDoc
172     {
173         Flags JavaDoc flags = m.getFlags();
174         Flags.Flag JavaDoc[] sf = flags.getSystemFlags(); // get the system flags
175

176         for (int i = 0; i < sf.length; i++)
177         {
178             Flags.Flag JavaDoc f = sf[i];
179             if (f == Flags.Flag.RECENT)
180                 return true;
181         }
182         return false;
183     }
184
185     public String JavaDoc getFrom() { return from; }
186     public String JavaDoc getTo() { return to; }
187     public String JavaDoc getSubject() { return subject; }
188     public String JavaDoc getBody() { return body; }
189     public String JavaDoc getFlags() { return flags; }
190     public String JavaDoc getXmailer() { return xmailer; }
191     public String JavaDoc getMime() { return mime; }
192
193     public Object JavaDoc getContent() { return content; }
194
195     public boolean getUnread() { return unread; }
196     public Date JavaDoc getSendDate() { return sendDate; }
197     public int getMessageNumber() { return m.getMessageNumber(); }
198     public String JavaDoc getFolder() { return m.getFolder().getName(); }
199
200     public javax.mail.Message JavaDoc getMessage() { return m; }
201 }
Popular Tags