| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.mail.util; |
| 18 | |
|
| 19 | |
import javax.activation.DataHandler; |
| 20 | |
import javax.activation.DataSource; |
| 21 | |
import javax.mail.Message; |
| 22 | |
import javax.mail.MessagingException; |
| 23 | |
import javax.mail.Multipart; |
| 24 | |
import javax.mail.Part; |
| 25 | |
import javax.mail.internet.InternetAddress; |
| 26 | |
import javax.mail.internet.MimeBodyPart; |
| 27 | |
import javax.mail.internet.MimeMessage; |
| 28 | |
import javax.mail.internet.MimePart; |
| 29 | |
import javax.mail.internet.MimeUtility; |
| 30 | |
import javax.mail.util.ByteArrayDataSource; |
| 31 | |
import java.io.BufferedInputStream; |
| 32 | |
import java.io.BufferedOutputStream; |
| 33 | |
import java.io.ByteArrayOutputStream; |
| 34 | |
import java.io.IOException; |
| 35 | |
import java.io.InputStream; |
| 36 | |
import java.io.UnsupportedEncodingException; |
| 37 | |
import java.util.ArrayList; |
| 38 | |
import java.util.Arrays; |
| 39 | |
import java.util.List; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public class MimeMessageParser |
| 49 | |
{ |
| 50 | |
|
| 51 | |
private final MimeMessage mimeMessage; |
| 52 | |
|
| 53 | |
|
| 54 | |
private String plainContent; |
| 55 | |
|
| 56 | |
|
| 57 | |
private String htmlContent; |
| 58 | |
|
| 59 | |
|
| 60 | |
private final List<DataSource> attachmentList; |
| 61 | |
|
| 62 | |
|
| 63 | |
private boolean isMultiPart; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public MimeMessageParser(final MimeMessage message) |
| 71 | 16 | { |
| 72 | 16 | attachmentList = new ArrayList<DataSource>(); |
| 73 | 16 | this.mimeMessage = message; |
| 74 | 16 | this.isMultiPart = false; |
| 75 | 16 | } |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public MimeMessageParser parse() throws Exception |
| 84 | |
{ |
| 85 | 16 | this.parse(null, mimeMessage); |
| 86 | 16 | return this; |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
public List<javax.mail.Address> getTo() throws Exception |
| 94 | |
{ |
| 95 | 6 | javax.mail.Address[] recipients = this.mimeMessage.getRecipients(Message.RecipientType.TO); |
| 96 | 6 | return recipients != null ? Arrays.asList(recipients) : new ArrayList<javax.mail.Address>(); |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public List<javax.mail.Address> getCc() throws Exception |
| 104 | |
{ |
| 105 | 6 | javax.mail.Address[] recipients = this.mimeMessage.getRecipients(Message.RecipientType.CC); |
| 106 | 6 | return recipients != null ? Arrays.asList(recipients) : new ArrayList<javax.mail.Address>(); |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
public List<javax.mail.Address> getBcc() throws Exception |
| 114 | |
{ |
| 115 | 6 | javax.mail.Address[] recipients = this.mimeMessage.getRecipients(Message.RecipientType.BCC); |
| 116 | 6 | return recipients != null ? Arrays.asList(recipients) : new ArrayList<javax.mail.Address>(); |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public String getFrom() throws Exception |
| 124 | |
{ |
| 125 | 5 | javax.mail.Address[] addresses = this.mimeMessage.getFrom(); |
| 126 | 5 | if ((addresses == null) || (addresses.length == 0)) |
| 127 | |
{ |
| 128 | 0 | return null; |
| 129 | |
} |
| 130 | |
else |
| 131 | |
{ |
| 132 | 5 | return ((InternetAddress) addresses[0]).getAddress(); |
| 133 | |
} |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
public String getReplyTo() throws Exception |
| 141 | |
{ |
| 142 | 5 | javax.mail.Address[] addresses = this.mimeMessage.getReplyTo(); |
| 143 | 5 | if ((addresses == null) || (addresses.length == 0)) |
| 144 | |
{ |
| 145 | 0 | return null; |
| 146 | |
} |
| 147 | |
else |
| 148 | |
{ |
| 149 | 5 | return ((InternetAddress) addresses[0]).getAddress(); |
| 150 | |
} |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
public String getSubject() throws Exception |
| 158 | |
{ |
| 159 | 6 | return this.mimeMessage.getSubject(); |
| 160 | |
} |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
protected void parse(Multipart parent, MimePart part) |
| 171 | |
throws MessagingException, IOException |
| 172 | |
{ |
| 173 | 67 | if (part.isMimeType("text/plain") && (plainContent == null)) |
| 174 | |
{ |
| 175 | 7 | plainContent = (String) part.getContent(); |
| 176 | |
} |
| 177 | |
else |
| 178 | |
{ |
| 179 | 60 | if (part.isMimeType("text/html") && (htmlContent == null)) |
| 180 | |
{ |
| 181 | 12 | htmlContent = (String) part.getContent(); |
| 182 | |
} |
| 183 | |
else |
| 184 | |
{ |
| 185 | 48 | if (part.isMimeType("multipart/*")) |
| 186 | |
{ |
| 187 | 30 | this.isMultiPart = true; |
| 188 | 30 | Multipart mp = (Multipart) part.getContent(); |
| 189 | 30 | int count = mp.getCount(); |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | 81 | for (int i = 0; i < count; i++) |
| 194 | |
{ |
| 195 | 51 | parse(mp, (MimeBodyPart) mp.getBodyPart(i)); |
| 196 | |
} |
| 197 | 30 | } |
| 198 | |
else |
| 199 | |
{ |
| 200 | 18 | this.attachmentList.add(createDataSource(parent, part)); |
| 201 | |
} |
| 202 | |
} |
| 203 | |
} |
| 204 | 67 | } |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
protected DataSource createDataSource(Multipart parent, MimePart part) |
| 216 | |
throws MessagingException, IOException |
| 217 | |
{ |
| 218 | 18 | DataHandler dataHandler = part.getDataHandler(); |
| 219 | 18 | DataSource dataSource = dataHandler.getDataSource(); |
| 220 | 18 | String contentType = getBaseMimeType(dataSource.getContentType()); |
| 221 | 18 | byte[] content = this.getContent(dataSource.getInputStream()); |
| 222 | 18 | ByteArrayDataSource result = new ByteArrayDataSource(content, contentType); |
| 223 | 18 | String dataSourceName = getDataSourceName(part, dataSource); |
| 224 | |
|
| 225 | 18 | result.setName(dataSourceName); |
| 226 | 18 | return result; |
| 227 | |
} |
| 228 | |
|
| 229 | |
|
| 230 | |
public MimeMessage getMimeMessage() |
| 231 | |
{ |
| 232 | 6 | return mimeMessage; |
| 233 | |
} |
| 234 | |
|
| 235 | |
|
| 236 | |
public boolean isMultipart() |
| 237 | |
{ |
| 238 | 6 | return isMultiPart; |
| 239 | |
} |
| 240 | |
|
| 241 | |
|
| 242 | |
public String getPlainContent() |
| 243 | |
{ |
| 244 | 6 | return plainContent; |
| 245 | |
} |
| 246 | |
|
| 247 | |
|
| 248 | |
public List<DataSource> getAttachmentList() |
| 249 | |
{ |
| 250 | 21 | return attachmentList; |
| 251 | |
} |
| 252 | |
|
| 253 | |
|
| 254 | |
public String getHtmlContent() |
| 255 | |
{ |
| 256 | 10 | return htmlContent; |
| 257 | |
} |
| 258 | |
|
| 259 | |
|
| 260 | |
public boolean hasPlainContent() |
| 261 | |
{ |
| 262 | 6 | return this.plainContent != null; |
| 263 | |
} |
| 264 | |
|
| 265 | |
|
| 266 | |
public boolean hasHtmlContent() |
| 267 | |
{ |
| 268 | 6 | return this.htmlContent != null; |
| 269 | |
} |
| 270 | |
|
| 271 | |
|
| 272 | |
public boolean hasAttachments() |
| 273 | |
{ |
| 274 | 5 | return this.attachmentList.size() > 0; |
| 275 | |
} |
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
public DataSource findAttachmentByName(String name) |
| 284 | |
{ |
| 285 | |
DataSource dataSource; |
| 286 | |
|
| 287 | 4 | for (int i = 0; i < getAttachmentList().size(); i++) |
| 288 | |
{ |
| 289 | 4 | dataSource = getAttachmentList().get(i); |
| 290 | 4 | if (name.equalsIgnoreCase(dataSource.getName())) |
| 291 | |
{ |
| 292 | 3 | return dataSource; |
| 293 | |
} |
| 294 | |
} |
| 295 | |
|
| 296 | 0 | return null; |
| 297 | |
} |
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
protected String getDataSourceName(Part part, DataSource dataSource) |
| 309 | |
throws MessagingException, UnsupportedEncodingException |
| 310 | |
{ |
| 311 | 18 | String result = dataSource.getName(); |
| 312 | |
|
| 313 | 18 | if (result == null || result.length() == 0) |
| 314 | |
{ |
| 315 | 2 | result = part.getFileName(); |
| 316 | |
} |
| 317 | |
|
| 318 | 18 | if (result != null && result.length() > 0) |
| 319 | |
{ |
| 320 | 17 | result = MimeUtility.decodeText(result); |
| 321 | |
} |
| 322 | |
else |
| 323 | |
{ |
| 324 | 1 | result = null; |
| 325 | |
} |
| 326 | |
|
| 327 | 18 | return result; |
| 328 | |
} |
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
private byte[] getContent(InputStream is) |
| 338 | |
throws IOException |
| 339 | |
{ |
| 340 | |
int ch; |
| 341 | |
byte[] result; |
| 342 | |
|
| 343 | 18 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 344 | 18 | BufferedInputStream isReader = new BufferedInputStream(is); |
| 345 | 18 | BufferedOutputStream osWriter = new BufferedOutputStream(os); |
| 346 | |
|
| 347 | 192740 | while ((ch = isReader.read()) != -1) |
| 348 | |
{ |
| 349 | 192722 | osWriter.write(ch); |
| 350 | |
} |
| 351 | |
|
| 352 | 18 | osWriter.flush(); |
| 353 | 18 | result = os.toByteArray(); |
| 354 | 18 | osWriter.close(); |
| 355 | |
|
| 356 | 18 | return result; |
| 357 | |
} |
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
private String getBaseMimeType(String fullMimeType) |
| 366 | |
{ |
| 367 | 18 | int pos = fullMimeType.indexOf(';'); |
| 368 | 18 | if (pos >= 0) |
| 369 | |
{ |
| 370 | 11 | return fullMimeType.substring(0, pos); |
| 371 | |
} |
| 372 | |
else |
| 373 | |
{ |
| 374 | 7 | return fullMimeType; |
| 375 | |
} |
| 376 | |
} |
| 377 | |
} |