001
014
015 package com.liferay.util.mail;
016
017 import com.liferay.portal.kernel.util.ArrayUtil;
018 import com.liferay.portal.kernel.util.FileUtil;
019 import com.liferay.portal.kernel.util.StringBundler;
020 import com.liferay.portal.kernel.util.StringPool;
021
022 import java.io.IOException;
023 import java.io.InputStream;
024
025 import javax.mail.Address;
026 import javax.mail.MessagingException;
027 import javax.mail.Part;
028 import javax.mail.internet.InternetAddress;
029
030
034 public class JavaMailUtil {
035
036 public static byte[] getBytes(Part part)
037 throws IOException, MessagingException {
038
039 InputStream is = part.getInputStream();
040
041 return FileUtil.getBytes(is);
042 }
043
044 public static String toUnicodeString(Address[] addresses) {
045 return toUnicodeString((InternetAddress[])addresses);
046 }
047
048 public static String toUnicodeString(InternetAddress[] addresses) {
049 if (ArrayUtil.isEmpty(addresses)) {
050 return StringPool.BLANK;
051 }
052
053 StringBundler sb = new StringBundler(addresses.length * 2 - 1);
054
055 for (int i = 0; i < addresses.length; i++) {
056 if (addresses[i] != null) {
057 sb.append(addresses[i].toUnicodeString());
058 }
059
060 if ((i + 1) != addresses.length) {
061 sb.append(", ");
062 }
063 }
064
065 return sb.toString();
066 }
067
068 }