1 39 40 package demo; 41 42 import java.text.*; 43 import java.util.*; 44 import javax.mail.*; 45 import javax.mail.internet.*; 46 47 50 public class MessageInfo { 51 private Message message; 52 53 54 57 public String getBcc() throws MessagingException { 58 return formatAddresses( 59 message.getRecipients(Message.RecipientType.BCC)); 60 } 61 62 65 public String getBody() throws MessagingException, java.io.IOException { 66 Object content = message.getContent(); 67 if (message.isMimeType("text/plain")) { 68 return (String )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 )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 )mp.getBodyPart(0).getContent(); 81 else 82 return ""; 83 } else 84 return ""; 85 } 86 87 90 public String getCc() throws MessagingException { 91 return formatAddresses( 92 message.getRecipients(Message.RecipientType.CC)); 93 } 94 95 99 public String 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 113 public String getFrom() throws MessagingException { 114 return formatAddresses(message.getFrom()); 115 } 116 117 120 public String 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 131 public Message getMessage() { 132 return message; 133 } 134 135 138 public String getNum() { 139 return (Integer.toString(message.getMessageNumber())); 140 } 141 142 145 public String getReceivedDate() throws MessagingException { 146 if (hasReceivedDate()) 147 return (message.getReceivedDate().toString()); 148 else 149 return ""; 150 } 151 152 155 public String getSentDate() throws MessagingException { 156 if (hasSentDate()) 157 return (message.getSentDate().toString()); 158 else 159 return ""; 160 } 161 162 165 public String getSubject() throws MessagingException { 166 if (hasSubject()) 167 return message.getSubject(); 168 else 169 return ""; 170 } 171 172 175 public String getTo() throws MessagingException { 176 return formatAddresses( 177 message.getRecipients(Message.RecipientType.TO)); 178 } 179 180 183 public boolean hasAttachments() throws java.io.IOException , 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 198 public boolean hasBcc() throws MessagingException { 199 return (message.getRecipients(Message.RecipientType.BCC) != null); 200 } 201 202 205 public boolean hasCc() throws MessagingException { 206 return (message.getRecipients(Message.RecipientType.CC) != null); 207 } 208 209 212 public boolean hasDate() throws MessagingException { 213 return (hasSentDate() || hasReceivedDate()); 214 } 215 216 219 public boolean hasFrom() throws MessagingException { 220 return (message.getFrom() != null); 221 } 222 223 226 public boolean hasMimeType(String mimeType) throws MessagingException { 227 return message.isMimeType(mimeType); 228 } 229 230 233 public boolean hasReceivedDate() throws MessagingException { 234 return (message.getReceivedDate() != null); 235 } 236 237 240 public boolean hasSentDate() throws MessagingException { 241 return (message.getSentDate() != null); 242 } 243 244 247 public boolean hasSubject() throws MessagingException { 248 return (message.getSubject() != null); 249 } 250 251 254 public boolean hasTo() throws MessagingException { 255 return (message.getRecipients(Message.RecipientType.TO) != null); 256 } 257 258 261 public void setMessage(Message message) { 262 this.message = message; 263 } 264 265 268 private String formatAddresses(Address[] addrs) { 269 if (addrs == null) 270 return ""; 271 StringBuffer strBuf = new StringBuffer (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 281 private String getDisplayAddress(Address a) { 282 String pers = null; 283 String addr = null; 284 if (a instanceof InternetAddress && 285 ((pers = ((InternetAddress)a).getPersonal()) != null)) { 286 addr = pers + " "+"<"+((InternetAddress)a).getAddress()+">"; 287 } else 288 addr = a.toString(); 289 return addr; 290 } 291 } 292 293 | Popular Tags |