View Javadoc

1   /*
2    * $Id: MailUtils.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.providers.email;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.util.StringUtils;
15  
16  import java.util.Map;
17  import java.io.IOException;
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  
32      public static String internetAddressesToString(InternetAddress[] addresses)
33      {
34          if (addresses == null || addresses.length == 0)
35          {
36              return StringUtils.EMPTY;
37          }
38  
39          StringBuffer buf = new StringBuffer(80);
40  
41          for (int i = 0; i < addresses.length; i++)
42          {
43              InternetAddress address = addresses[i];
44              buf.append(address.getAddress());
45              // all except the last one
46              if (i < addresses.length - 1)
47              {
48                  buf.append(", ");
49              }
50          }
51  
52          return buf.toString();
53      }
54  
55      public static String internetAddressesToString(InternetAddress address)
56      {
57          return internetAddressesToString(new InternetAddress[]{address});
58      }
59  
60      public static String mailAddressesToString(Address[] addresses)
61      {
62          if (addresses == null || addresses.length == 0)
63          {
64              return StringUtils.EMPTY;
65          }
66  
67          StringBuffer buf = new StringBuffer(80);
68  
69          for (int i = 0; i < addresses.length; i++)
70          {
71              Address address = addresses[i];
72              buf.append(address.toString());
73              // all except the last one
74              if (i < addresses.length - 1)
75              {
76                  buf.append(", ");
77              }
78          }
79  
80          return buf.toString();
81      }
82  
83      public static String mailAddressesToString(Address address)
84      {
85          return mailAddressesToString(new Address[]{address});
86      }
87  
88      public static InternetAddress[] stringToInternetAddresses(String address) throws AddressException
89      {
90          if (StringUtils.isNotBlank(address))
91          {
92              return InternetAddress.parse(address, false);
93          }
94          else
95          {
96              throw new IllegalArgumentException(CoreMessages.objectIsNull("Email address").toString());
97          }
98      }
99  
100     public static void getAttachments(Multipart content, Map attachments) throws MessagingException, IOException
101     {
102         int x = 0;
103         for(int i=0; i < content.getCount(); i++)
104         {
105             Part p = content.getBodyPart(i);
106             if(p.getContentType().indexOf("multipart/mixed") > -1)
107             {
108                 Multipart m = (Multipart)p.getContent();
109                 getAttachments(m, attachments);
110             }
111             else
112             {
113                 String key;
114                 if(StringUtils.isNotEmpty(p.getDescription()))
115                 {
116                     key = p.getDescription();
117                 }
118                 else if(StringUtils.isNotEmpty(p.getFileName()))
119                 {
120                     key = p.getFileName();
121                 }
122                 else if(StringUtils.isNotEmpty(p.getDisposition()))
123                 {
124                     key = p.getDisposition();
125                 }
126                 else
127                 {
128                     key = String.valueOf(x++);
129                 }
130                 attachments.put(key, p);
131             }
132         }
133     }
134 }