001: /*
002: * $Id: MailUtils.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.email;
012:
013: import org.mule.config.i18n.CoreMessages;
014: import org.mule.util.StringUtils;
015:
016: import java.io.IOException;
017: import java.util.Map;
018:
019: import javax.mail.Address;
020: import javax.mail.MessagingException;
021: import javax.mail.Multipart;
022: import javax.mail.Part;
023: import javax.mail.internet.AddressException;
024: import javax.mail.internet.InternetAddress;
025:
026: /**
027: * Contains javax.mail helpers.
028: */
029: public class MailUtils {
030:
031: public static String internetAddressesToString(
032: InternetAddress[] addresses) {
033: if (addresses == null || addresses.length == 0) {
034: return StringUtils.EMPTY;
035: }
036:
037: StringBuffer buf = new StringBuffer(80);
038:
039: for (int i = 0; i < addresses.length; i++) {
040: InternetAddress address = addresses[i];
041: buf.append(address.getAddress());
042: // all except the last one
043: if (i < addresses.length - 1) {
044: buf.append(", ");
045: }
046: }
047:
048: return buf.toString();
049: }
050:
051: public static String internetAddressesToString(
052: InternetAddress address) {
053: return internetAddressesToString(new InternetAddress[] { address });
054: }
055:
056: public static String mailAddressesToString(Address[] addresses) {
057: if (addresses == null || addresses.length == 0) {
058: return StringUtils.EMPTY;
059: }
060:
061: StringBuffer buf = new StringBuffer(80);
062:
063: for (int i = 0; i < addresses.length; i++) {
064: Address address = addresses[i];
065: buf.append(address.toString());
066: // all except the last one
067: if (i < addresses.length - 1) {
068: buf.append(", ");
069: }
070: }
071:
072: return buf.toString();
073: }
074:
075: public static String mailAddressesToString(Address address) {
076: return mailAddressesToString(new Address[] { address });
077: }
078:
079: public static InternetAddress[] stringToInternetAddresses(
080: String address) throws AddressException {
081: if (StringUtils.isNotBlank(address)) {
082: return InternetAddress.parse(address, false);
083: } else {
084: throw new IllegalArgumentException(CoreMessages
085: .objectIsNull("Email address").toString());
086: }
087: }
088:
089: public static void getAttachments(Multipart content, Map attachments)
090: throws MessagingException, IOException {
091: int x = 0;
092: for (int i = 0; i < content.getCount(); i++) {
093: Part p = content.getBodyPart(i);
094: if (p.getContentType().indexOf("multipart/mixed") > -1) {
095: Multipart m = (Multipart) p.getContent();
096: getAttachments(m, attachments);
097: } else {
098: String key;
099: if (StringUtils.isNotEmpty(p.getDescription())) {
100: key = p.getDescription();
101: } else if (StringUtils.isNotEmpty(p.getFileName())) {
102: key = p.getFileName();
103: } else if (StringUtils.isNotEmpty(p.getDisposition())) {
104: key = p.getDisposition();
105: } else {
106: key = String.valueOf(x++);
107: }
108: attachments.put(key, p);
109: }
110: }
111: }
112: }
|