1 16 package org.apache.cocoon.mail; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Date ; 21 import java.util.List ; 22 23 import javax.mail.MessagingException ; 24 import javax.mail.internet.AddressException ; 25 import javax.mail.internet.InternetAddress ; 26 import javax.mail.internet.MimeMessage ; 27 import javax.mail.internet.MimeMultipart ; 28 import javax.mail.internet.MimePart ; 29 30 37 public class MimeMessageUtil { 38 41 public final static String SENDER_NOT_AVAILABLE = "-not available-"; 42 45 public final static String NO_SUBJECT = "-none-"; 46 47 48 54 public static String getSender(MimeMessage msg) { 55 String sender = null; 56 try { 57 InternetAddress [] from = (InternetAddress []) msg.getFrom(); 58 if (from != null && from.length > 0) { 59 sender = from[0].getPersonal(); 60 if (sender == null) { 61 sender = from[0].getAddress(); 62 } 63 } 64 if (sender == null) { 65 sender = SENDER_NOT_AVAILABLE; 66 } 67 } catch (AddressException e) { 68 sender = SENDER_NOT_AVAILABLE; 69 } catch (MessagingException e) { 70 sender = SENDER_NOT_AVAILABLE; 71 } 72 if (sender == null || sender.trim().equals("")) { 73 sender = SENDER_NOT_AVAILABLE; 74 } 75 return sender; 76 } 77 78 79 85 public static String getSenderEmail(MimeMessage msg) { 86 String senderEmail = null; 87 try { 88 InternetAddress [] from = (InternetAddress []) msg.getFrom(); 89 if (from != null && from.length > 0) { 90 senderEmail = from[0].getAddress(); 91 } 92 } catch (AddressException e) { 93 senderEmail = SENDER_NOT_AVAILABLE; 94 } catch (MessagingException e) { 95 senderEmail = SENDER_NOT_AVAILABLE; 96 } 97 if (senderEmail == null || senderEmail.trim().equals("")) { 98 senderEmail = SENDER_NOT_AVAILABLE; 99 } 100 return senderEmail; 101 } 102 103 104 110 public static String getSubject(MimeMessage msg) { 111 String subject = null; 112 try { 113 subject = msg.getSubject(); 114 } catch (MessagingException e) { 115 subject = NO_SUBJECT; 116 } 117 if (subject == null || subject.trim().equals("")) { 118 subject = NO_SUBJECT; 119 } 120 return subject; 121 } 122 123 124 130 public static Date getDate(MimeMessage msg) { 131 Date date = null; 132 try { 133 date = msg.getReceivedDate(); 134 } catch (MessagingException messagingexception) { 135 138 } 139 if (date == null) { 140 try { 141 date = msg.getSentDate(); 142 } catch (MessagingException messagingexception) { 143 146 } 147 } 148 return date; 149 } 150 151 152 158 public static String getID(MimeMessage msg) { 159 String id = null; 160 try { 161 id = msg.getMessageID(); 162 } catch (MessagingException messagingexception) { 163 166 } 167 return id; 168 } 169 170 171 177 private static void flattenMessageHelper 178 (MimePart part, ContentTypePreference ctPref, List l) { 179 try { 180 if (part.isMimeType("multipart/alternative")) { 181 MimeMultipart mp = (MimeMultipart ) part.getContent(); 182 MimePart bestPart = null; 183 int ctMax = 0; 184 for (int i = 0; i < mp.getCount(); i++) { 185 MimePart p = (MimePart ) mp.getBodyPart(i); 186 int ctPrefN = ctPref.preference(part); 187 if (ctPrefN > ctMax) { 188 ctMax = ctPrefN; 189 bestPart = p; 190 } 191 } 192 if (bestPart != null) { 193 l.add(bestPart); 194 } 195 } else if (part.isMimeType("multipart/*")) { 196 MimeMultipart mp = (MimeMultipart ) part.getContent(); 197 for (int i = 0; i < mp.getCount(); i++) { 198 flattenMessageHelper((MimePart ) mp.getBodyPart(i), 199 ctPref, l); 200 } 201 } else if (part.isMimeType("message/rfc822")) { 202 flattenMessageHelper((MimePart ) part.getContent(), ctPref, 203 l); 204 } else if (ctPref.preference(part) > 0) { 205 l.add(part); 206 } 207 } catch (MessagingException e) { 208 211 } catch (IOException ioexception) { 212 215 } 216 } 217 218 219 226 public static MimePart [] flattenMessage(MimeMessage message, 227 ContentTypePreference ctPref) { 228 List parts = new ArrayList (); 229 flattenMessageHelper(message, ctPref, parts); 230 return (MimePart []) parts.toArray(new MimePart [0]); 231 } 232 } 233 234 | Popular Tags |