View Javadoc

1   /*
2    * $Id: MailUtils.java 19191 2010-08-25 21:05:23Z tcarlson $
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.util.Map;
18  
19  import javax.mail.Address;
20  import javax.mail.MessagingException;
21  import javax.mail.Multipart;
22  import javax.mail.Part;
23  import javax.mail.internet.AddressException;
24  import javax.mail.internet.InternetAddress;
25  
26  /**
27   * Contains javax.mail helpers.
28   */
29  public class MailUtils
30  {
31      public static String internetAddressesToString(InternetAddress[] addresses)
32      {
33          if (addresses == null || addresses.length == 0)
34          {
35              return StringUtils.EMPTY;
36          }
37  
38          StringBuffer buf = new StringBuffer(80);
39  
40          for (int i = 0; i < addresses.length; i++)
41          {
42              InternetAddress address = addresses[i];
43              buf.append(address.getAddress());
44              // all except the last one
45              if (i < addresses.length - 1)
46              {
47                  buf.append(", ");
48              }
49          }
50  
51          return buf.toString();
52      }
53  
54      public static String internetAddressesToString(InternetAddress address)
55      {
56          return internetAddressesToString(new InternetAddress[]{address});
57      }
58  
59      public static String mailAddressesToString(Address[] addresses)
60      {
61          if (addresses == null || addresses.length == 0)
62          {
63              return StringUtils.EMPTY;
64          }
65  
66          StringBuffer buf = new StringBuffer(80);
67  
68          for (int i = 0; i < addresses.length; i++)
69          {
70              Address address = addresses[i];
71              buf.append(address.toString());
72              // all except the last one
73              if (i < addresses.length - 1)
74              {
75                  buf.append(", ");
76              }
77          }
78  
79          return buf.toString();
80      }
81  
82      public static String mailAddressesToString(Address address)
83      {
84          return mailAddressesToString(new Address[]{address});
85      }
86  
87      public static InternetAddress[] stringToInternetAddresses(String address) throws AddressException
88      {
89          if (StringUtils.isNotBlank(address))
90          {
91              return InternetAddress.parse(address, false);
92          }
93          else
94          {
95              throw new IllegalArgumentException(CoreMessages.objectIsNull("Email address").toString());
96          }
97      }
98  
99      public static void getAttachments(Multipart content, Map attachments) throws MessagingException, IOException
100     {
101         int x = 0;
102         for(int i=0; i < content.getCount(); i++)
103         {
104             Part p = content.getBodyPart(i);
105             if(p.getContentType().indexOf("multipart/mixed") > -1)
106             {
107                 Multipart m = (Multipart)p.getContent();
108                 getAttachments(m, attachments);
109             }
110             else
111             {
112                 String key;
113                 if(StringUtils.isNotEmpty(p.getDescription()))
114                 {
115                     key = p.getDescription();
116                 }
117                 else if(StringUtils.isNotEmpty(p.getFileName()))
118                 {
119                     key = p.getFileName();
120                 }
121                 else if(StringUtils.isNotEmpty(p.getDisposition()))
122                 {
123                     key = p.getDisposition();
124                 }
125                 else
126                 {
127                     key = String.valueOf(x++);
128                 }
129                 attachments.put(key, p);
130             }
131         }
132     }
133 
134     /**
135      * Check whether a property name has the format associated with a list
136      * of header values
137      *
138      * @param name A property name
139      * @return true if the name is associated with a list of header values
140      *         (more exactly, if it starts with HEADER_LIST_PREFIX, which gives an
141      *         invalid header name according to RFC822).
142      */
143     public static boolean isListHeader(String name)
144     {
145         return null != name && name.startsWith(MailMuleMessageFactory.HEADER_LIST_PREFIX);
146     }
147 
148     /**
149      * Convert a header name to the property name associated with a list of
150      * header values (ie prepend the prefix)
151      *
152      * @param header A header name
153      * @return The associated list property name (ie with HEADER_LIST_PREFIX prepended)
154      */
155     public static String toListHeader(String header)
156     {
157         if (isListHeader(header))
158         {
159             return header;
160         }
161         else
162         {
163             return MailMuleMessageFactory.HEADER_LIST_PREFIX + header;
164         }
165     }
166 }