1 5 package org.exoplatform.services.communication.message.impl; 6 7 import javax.mail.*; 8 import javax.mail.Address ; 9 import javax.mail.Flags ; 10 import javax.mail.Flags.Flag; 11 import javax.mail.internet.InternetAddress ; 12 import org.exoplatform.services.communication.message.MessageHeader; 13 14 15 16 21 public class MailUtil { 22 static public boolean hasSystemFlag(Flags flags, Flags.Flag flag) { 23 Flags.Flag [] 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 flags, String flag) { 31 String [] 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 convertAddressToString(Address [] addrs) { 39 if (addrs == null || addrs.length == 0) 40 return "Not Available <root@localhost>"; 41 StringBuffer sb = new StringBuffer (); 42 int last = addrs.length - 1; 43 for (int i = 0; i < addrs.length; i++) { 44 InternetAddress addr = (InternetAddress ) addrs[i]; 45 String name = addr.getPersonal(); 46 String 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 { 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 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 body = msg.getContent(); 71 String bodyStr = null ; 72 if (body instanceof String ) { 73 bodyStr = (String ) body; 74 } else if (body instanceof Multipart) { 75 Multipart multipart = (Multipart) body; 76 StringBuffer sb = new StringBuffer (); 77 for (int i = 0; i < multipart.getCount(); i++) { 78 BodyPart bodypart = multipart.getBodyPart(i); 79 Object content = bodypart.getContent(); 80 if (content instanceof String ) { 81 sb.append('\n').append((String ) 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 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 [] 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 supportFlags = folder.getPermanentFlags() ; 120 Flags.Flag [] 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 |