]>
git.argeo.org Git - gpl/argeo-slc.git/blob - mail/EmailUtils.java
1 package org
.argeo
.slc
.mail
;
5 import javax
.mail
.Address
;
6 import javax
.mail
.Flags
;
7 import javax
.mail
.Message
;
8 import javax
.mail
.MessagingException
;
9 import javax
.mail
.internet
.InternetAddress
;
10 import javax
.mail
.internet
.MimeMessage
;
12 /** Utilities around emails. */
13 public class EmailUtils
{
14 public final static String INBOX
= "Inbox";
15 public final static String INBOX_UPPER_CASE
= "INBOX";
16 public final static String MESSAGE_ID
= "Message-ID";
18 public static String
getMessageId(Message msg
) {
20 return msg
instanceof MimeMessage ?
((MimeMessage
) msg
).getMessageID() : "<N/A>";
21 } catch (MessagingException e
) {
22 throw new IllegalStateException("Cannot extract message id from " + msg
, e
);
26 public static String
describe(Message msg
) {
28 return "Message " + msg
.getMessageNumber() + " " + msg
.getSentDate().toInstant() + " " + getMessageId(msg
);
29 } catch (MessagingException e
) {
30 throw new IllegalStateException("Cannot describe " + msg
, e
);
34 static void setHeadersFromFlags(MimeMessage msg
, Flags flags
) {
36 StringBuilder status
= new StringBuilder();
37 if (flags
.contains(Flags
.Flag
.SEEN
))
39 if (!flags
.contains(Flags
.Flag
.RECENT
))
41 if (status
.length() > 0)
42 msg
.setHeader("Status", status
.toString());
44 msg
.removeHeader("Status");
47 String s
= msg
.getHeader("X-Status", null);
48 // is it a SIMS 2.0 format X-Status header?
49 sims
= s
!= null && s
.length() == 4 && s
.indexOf('$') >= 0;
50 //status.setLength(0);
51 if (flags
.contains(Flags
.Flag
.DELETED
))
55 if (flags
.contains(Flags
.Flag
.FLAGGED
))
59 if (flags
.contains(Flags
.Flag
.ANSWERED
))
63 if (flags
.contains(Flags
.Flag
.DRAFT
))
67 if (status
.length() > 0)
68 msg
.setHeader("X-Status", status
.toString());
70 msg
.removeHeader("X-Status");
72 String
[] userFlags
= flags
.getUserFlags();
73 if (userFlags
.length
> 0) {
75 for (int i
= 0; i
< userFlags
.length
; i
++)
76 status
.append(userFlags
[i
]).append(' ');
77 status
.setLength(status
.length() - 1); // smash trailing space
78 msg
.setHeader("X-Keywords", status
.toString());
80 if (flags
.contains(Flags
.Flag
.DELETED
)) {
81 s
= msg
.getHeader("X-Dt-Delete-Time", null);
83 // XXX - should be time
84 msg
.setHeader("X-Dt-Delete-Time", "1");
86 } catch (MessagingException e
) {
91 protected static String
getUnixFrom(MimeMessage msg
) {
97 if ((afrom
= msg
.getFrom()) == null ||
98 !(afrom
[0] instanceof InternetAddress
) ||
99 (from
= ((InternetAddress
)afrom
[0]).getAddress()) == null)
101 if ((ddate
= msg
.getReceivedDate()) == null ||
102 (ddate
= msg
.getSentDate()) == null)
104 } catch (MessagingException e
) {
108 date
= ddate
.toString();
109 // date is of the form "Sat Aug 12 02:30:00 PDT 1995"
110 // need to strip out the timezone
111 return "From " + from
+ " " +
112 date
.substring(0, 20) + date
.substring(24);
116 private EmailUtils() {