Java Encode encodeAddresses(String string, String charset)

Here you can find the source of encodeAddresses(String string, String charset)

Description

Encode UTF strings into mail addresses.

License

Open Source License

Parameter

Parameter Description
string DOCUMENT ME!
charset DOCUMENT ME!

Exception

Parameter Description
MessagingException DOCUMENT ME!

Return

DOCUMENT ME!

Declaration

public static InternetAddress[] encodeAddresses(String string, String charset) throws MessagingException 

Method Source Code

//package com.java2s;
/*//from  www. j a v a2s  .c  o  m
 ** $Id: MessageUtilities.java,v 1.9 2006/06/30 13:00:37 durot Exp $
 **
 ** Copyright (c) 2000-2003 Jeff Gay
 ** on behalf of ICEMail.org <http://www.icemail.org>
 ** Copyright (c) 1998-2000 by Timothy Gerard Endres
 **
 ** This program is free software.
 **
 ** You may redistribute it and/or modify it under the terms of the GNU
 ** General Public License as published by the Free Software Foundation.
 ** Version 2 of the license should be included with this distribution in
 ** the file LICENSE, as well as License.html. If the license is not
 ** included with this distribution, you may find a copy at the FSF web
 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
 **
 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
 ** REDISTRIBUTION OF THIS SOFTWARE.
 */

import java.io.UnsupportedEncodingException;

import javax.mail.MessagingException;

import javax.mail.internet.InternetAddress;

public class Main {
    /**
     * Encode UTF strings into mail addresses.
     *
     * @param string DOCUMENT ME!
     * @param charset DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     *
     * @throws MessagingException DOCUMENT ME!
     */
    public static InternetAddress[] encodeAddresses(String string, String charset) throws MessagingException {
        if (string == null) {
            return null;
        }

        // parse the string into the internet addresses
        // NOTE: these will NOT be character encoded
        InternetAddress[] xaddresses = InternetAddress.parse(string);

        // now encode each to the given character set
        if (charset != null) {
            for (int xindex = 0; xindex < xaddresses.length; xindex++) {
                String xpersonal = xaddresses[xindex].getPersonal();

                try {
                    if (xpersonal != null) {
                        if (charset != null) {
                            xaddresses[xindex].setPersonal(xpersonal, charset);
                        } else {
                            xaddresses[xindex].setPersonal(xpersonal, "ISO-8859-1");
                        }
                    }
                } catch (UnsupportedEncodingException xex) {
                    throw new MessagingException(xex.toString());
                }
            }
        }

        return xaddresses;
    }
}

Related

  1. encode(Object obj)
  2. encode(Serializable s)
  3. encode(T obj)
  4. encode2Encode(String original, String encode1, String encode2)
  5. encode_u8(String what)
  6. encodeArguments(final String arg)
  7. encodeArray(String[] sourceArray, String sysCharset, String charset)
  8. encodeAsModifiedUTF8(String str)
  9. encodeAttribute(String value)