View Javadoc

1   /*
2    * $Id: MailUtils.java 21591 2011-03-21 06:32:30Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.email;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.util.StringUtils;
15  
16  import java.io.IOException;
17  import java.io.UnsupportedEncodingException;
18  import java.util.Map;
19  
20  import javax.mail.Address;
21  import javax.mail.MessagingException;
22  import javax.mail.Multipart;
23  import javax.mail.Part;
24  import javax.mail.internet.AddressException;
25  import javax.mail.internet.InternetAddress;
26  import javax.mail.internet.MimeUtility;
27  
28  /**
29   * Contains javax.mail helpers.
30   */
31  public class MailUtils
32  {
33      public static String internetAddressesToString(InternetAddress[] addresses)
34      {
35          if (addresses == null || addresses.length == 0)
36          {
37              return StringUtils.EMPTY;
38          }
39  
40          StringBuffer buf = new StringBuffer(80);
41  
42          for (int i = 0; i < addresses.length; i++)
43          {
44              InternetAddress address = addresses[i];
45              buf.append(address.getAddress());
46              // all except the last one
47              if (i < addresses.length - 1)
48              {
49                  buf.append(", ");
50              }
51          }
52  
53          return buf.toString();
54      }
55  
56      public static String internetAddressesToString(InternetAddress address)
57      {
58          return internetAddressesToString(new InternetAddress[]{address});
59      }
60  
61      public static String mailAddressesToString(Address[] addresses)
62      {
63          if (addresses == null || addresses.length == 0)
64          {
65              return StringUtils.EMPTY;
66          }
67  
68          StringBuffer buf = new StringBuffer(80);
69  
70          for (int i = 0; i < addresses.length; i++)
71          {
72              Address address = addresses[i];
73              buf.append(address.toString());
74              // all except the last one
75              if (i < addresses.length - 1)
76              {
77                  buf.append(", ");
78              }
79          }
80  
81          return buf.toString();
82      }
83  
84      public static String mailAddressesToString(Address address)
85      {
86          return mailAddressesToString(new Address[]{address});
87      }
88  
89      public static InternetAddress[] stringToInternetAddresses(String address) throws AddressException
90      {
91          if (StringUtils.isNotBlank(address))
92          {
93              return InternetAddress.parse(address, false);
94          }
95          else
96          {
97              throw new IllegalArgumentException(CoreMessages.objectIsNull("Email address").toString());
98          }
99      }
100 
101     public static void getAttachments(Multipart content, Map<String, Part> attachments) throws MessagingException, IOException
102     {
103         int x = 0;
104         for(int i = 0; i < content.getCount(); i++)
105         {
106             Part part = content.getBodyPart(i);
107             if (part.getContentType().indexOf("multipart/mixed") > -1)
108             {
109                 Multipart m = (Multipart) part.getContent();
110                 getAttachments(m, attachments);
111             }
112             else
113             {
114                 String key;
115                 if (StringUtils.isNotEmpty(part.getDescription()))
116                 {
117                     key = part.getDescription();
118                 }
119                 else if (StringUtils.isNotEmpty(part.getFileName()))
120                 {
121                     try
122                     {
123                         key = MimeUtility.decodeText(part.getFileName());
124                     }
125                     catch (UnsupportedEncodingException e)
126                     {
127                         key = part.getFileName();
128                     }
129                 }
130                 else if (StringUtils.isNotEmpty(part.getDisposition()))
131                 {
132                     key = part.getDisposition();
133                 }
134                 else
135                 {
136                     key = String.valueOf(x++);
137                 }
138 
139                 key = getAttachmentName(key, attachments);
140 
141                 attachments.put(key, part);
142             }
143         }
144     }
145 
146 
147     /**
148      * Check whether an attachment with the same name already exists,
149      * using a counter for override protection.
150      *
151      * @param key A attachment name
152      * @param attachments Map with attachments
153      * @return attachment name
154      */
155     public static String getAttachmentName(String key, Map<String, Part> attachments)
156     {
157         if (attachments.containsKey(key))
158         {
159             int x = 0;
160             while (attachments.containsKey(x + "_" + key))
161             {
162                 x++;
163             }
164             return x + "_" + key;
165 
166         } else
167         {
168             return key;
169         }
170     }
171 
172     /**
173      * Check whether a property name has the format associated with a list
174      * of header values
175      *
176      * @param name A property name
177      * @return true if the name is associated with a list of header values
178      *         (more exactly, if it starts with HEADER_LIST_PREFIX, which gives an
179      *         invalid header name according to RFC822).
180      */
181     public static boolean isListHeader(String name)
182     {
183         return null != name && name.startsWith(MailMuleMessageFactory.HEADER_LIST_PREFIX);
184     }
185 
186     /**
187      * Convert a header name to the property name associated with a list of
188      * header values (ie prepend the prefix)
189      *
190      * @param header A header name
191      * @return The associated list property name (ie with HEADER_LIST_PREFIX prepended)
192      */
193     public static String toListHeader(String header)
194     {
195         if (isListHeader(header))
196         {
197             return header;
198         }
199         else
200         {
201             return MailMuleMessageFactory.HEADER_LIST_PREFIX + header;
202         }
203     }
204 }