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