1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util.mail;
16  
17  import com.liferay.portal.kernel.util.FileUtil;
18  import com.liferay.portal.kernel.util.StringBundler;
19  import com.liferay.portal.kernel.util.StringPool;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import javax.mail.Address;
25  import javax.mail.MessagingException;
26  import javax.mail.Part;
27  import javax.mail.internet.InternetAddress;
28  
29  /**
30   * <a href="JavaMailUtil.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class JavaMailUtil {
35  
36      public static byte[] getBytes(Part part)
37          throws IOException, MessagingException {
38  
39          InputStream is = part.getInputStream();
40  
41          try {
42              return FileUtil.getBytes(is);
43          }
44          finally {
45              is.close();
46          }
47      }
48  
49      public static String toUnicodeString(Address[] addresses) {
50          return toUnicodeString((InternetAddress[])addresses);
51      }
52  
53      public static String toUnicodeString(InternetAddress[] addresses) {
54          if ((addresses == null) || (addresses.length == 0)) {
55              return StringPool.BLANK;
56          }
57  
58          StringBundler sb = new StringBundler(addresses.length * 2 - 1);
59  
60          for (int i = 0; i < addresses.length; i++) {
61              if (addresses[i] != null) {
62                  sb.append(addresses[i].toUnicodeString());
63              }
64  
65              if ((i + 1) != addresses.length) {
66                  sb.append(", ");
67              }
68          }
69  
70          return sb.toString();
71      }
72  
73  }