001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
031     * @author Brian Wing Shun Chan
032     * @see com.liferay.petra.mail.JavaMailUtil
033     */
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    }