KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > message > impl > MailUtil


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.communication.message.impl;
6
7 import javax.mail.*;
8 import javax.mail.Address JavaDoc;
9 import javax.mail.Flags JavaDoc;
10 import javax.mail.Flags.Flag;
11 import javax.mail.internet.InternetAddress JavaDoc;
12 import org.exoplatform.services.communication.message.MessageHeader;
13
14
15
16 /**
17  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
18  * @since Sep 11, 2004
19  * @version $Id: MailUtil.java,v 1.4 2004/10/14 23:29:22 tuan08 Exp $
20  */

21 public class MailUtil {
22   static public boolean hasSystemFlag(Flags JavaDoc flags, Flags.Flag JavaDoc flag) {
23     Flags.Flag JavaDoc[] sf = flags.getSystemFlags();
24     for (int i = 0; i < sf.length; i++) {
25       if(sf[i] == flag) return true ;
26     }
27     return false ;
28   }
29   
30   static public boolean hasUserFlag(Flags JavaDoc flags, String JavaDoc flag) {
31     String JavaDoc[] uf = flags.getUserFlags();
32     for (int i = 0; i < uf.length; i++) {
33       if(uf[i].equals(flag)) return true ;
34     }
35     return false ;
36   }
37   
38   static public String JavaDoc convertAddressToString(Address JavaDoc[] addrs) {
39     if (addrs == null || addrs.length == 0)
40       return "Not Available <root@localhost>";
41     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
42     int last = addrs.length - 1;
43     for (int i = 0; i < addrs.length; i++) {
44       InternetAddress JavaDoc addr = (InternetAddress JavaDoc) addrs[i];
45       String JavaDoc name = addr.getPersonal();
46       String JavaDoc email = addr.getAddress();
47       if (name == null)
48         name = email;
49       sb.append(name);
50       sb.append(" <").append(email).append(">");
51       if (i != last) {
52         sb.append(',');
53       }
54     }
55     return sb.toString();
56   }
57   
58   static public org.exoplatform.services.communication.message.Message createMessage(Message msg) throws Exception JavaDoc {
59     org.exoplatform.services.communication.message.Message message =
60       new org.exoplatform.services.communication.message.impl.MessageImpl() ;
61     message.setFrom(convertAddressToString(msg.getFrom()));
62     message.setSubject(msg.getSubject());
63     java.util.Date JavaDoc rdate = msg.getReceivedDate();
64     if (rdate == null) rdate = msg.getSentDate();
65     message.setReceivedDate(rdate) ;
66     message.setTo(convertAddressToString(msg.getRecipients(Message.RecipientType.TO)));
67     message.setCC(convertAddressToString(msg.getRecipients(Message.RecipientType.CC)));
68     
69     setMessageFlags(message, msg.getFlags());
70     Object JavaDoc body = msg.getContent();
71     String JavaDoc bodyStr = null ;
72     if (body instanceof String JavaDoc) {
73       bodyStr = (String JavaDoc) body;
74     } else if (body instanceof Multipart) {
75       Multipart multipart = (Multipart) body;
76       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
77       for (int i = 0; i < multipart.getCount(); i++) {
78         BodyPart bodypart = multipart.getBodyPart(i);
79         Object JavaDoc content = bodypart.getContent();
80         if (content instanceof String JavaDoc) {
81           sb.append('\n').append((String JavaDoc) content);
82         }
83         
84       }
85       bodyStr = sb.toString();
86     }
87     message.setBody(bodyStr) ;
88     return message ;
89   }
90  
91   private static void setMessageFlags(org.exoplatform.services.communication.message.Message message, Flags JavaDoc flags) {
92      Flag[] flagsArray = flags.getSystemFlags();
93      for (int i = 0; i < flagsArray.length; i++) {
94       Flag flag = flagsArray[i];
95       if(Flags.Flag.ANSWERED == flag) {
96         message.addFlag(MessageHeader.ANSWERED_FLAG);
97       } else if(Flags.Flag.DELETED == flag) {
98         message.addFlag(MessageHeader.DELETED_FLAG);
99       }else if(Flags.Flag.DRAFT == flag) {
100         message.addFlag(MessageHeader.DRAFT_FLAG);
101       }else if(Flags.Flag.FLAGGED == flag) {
102         message.addFlag(MessageHeader.FLAGGED_FLAG);
103       } else if(Flags.Flag.RECENT == flag) {
104         message.addFlag(MessageHeader.RECENT_FLAG);
105       } else if(Flags.Flag.SEEN == flag) {
106         message.addFlag(MessageHeader.SEEN_FLAG);
107       }else if(Flags.Flag.USER == flag) {
108         message.addFlag(MessageHeader.USER_FLAG);
109       }
110     }
111     String JavaDoc[] userFlags = flags.getUserFlags();
112     for (int i = 0; i < userFlags.length; i++) {
113       message.addFlag(userFlags[i]);
114     }
115   }
116
117
118   static public void printFolderSupportFlag(Folder folder) {
119     Flags JavaDoc supportFlags = folder.getPermanentFlags() ;
120     Flags.Flag JavaDoc[] sf = supportFlags.getSystemFlags();
121     for (int i = 0; i < sf.length; i++) {
122       if(Flags.Flag.ANSWERED == sf[i]) {
123         System.out.println("flag answer") ;
124       } else if(Flags.Flag.DELETED == sf[i]) {
125         System.out.println("flag delete") ;
126       }else if(Flags.Flag.DRAFT == sf[i]) {
127         System.out.println("flag draft") ;
128       }else if(Flags.Flag.FLAGGED == sf[i]) {
129         System.out.println("flag flag") ;
130       } else if(Flags.Flag.RECENT == sf[i]) {
131         System.out.println("flag recent") ;
132       } else if(Flags.Flag.SEEN == sf[i]) {
133         System.out.println("flag seen") ;
134       }else if(Flags.Flag.USER == sf[i]) {
135         System.out.println("flag user") ;
136       } else {
137         System.out.println("") ;
138       }
139     }
140   }
141 }
Popular Tags