Java Tutorial/Email/Mime

Материал из Java эксперт
Перейти к: навигация, поиск

A convenience class which handles conversions between MIME charset names and Java encoding names.

import java.util.*;
/**
 * MIME2Java is a convenience class which handles conversions between MIME charset names
 * and Java encoding names.
 * <p>The supported XML encodings are the intersection of XML-supported code sets and those
 * supported in JDK 1.1.
 * <p>MIME charset names are used on <var>xmlEncoding</var> parameters to methods such
 * as <code>TXDocument#setEncoding</code> and <code>DTD#setEncoding</code>.
 * <p>Java encoding names are used on <var>encoding</var> parameters to
 * methods such as <code>TXDocument#printWithFormat</code> and <code>DTD#printExternal</code>.
 * <P>
 * <TABLE BORDER="0" WIDTH="100%">
 *  <TR>
 *      <TD WIDTH="33%">
 *          <P ALIGN="CENTER"><B>Common Name</B>
 *      </TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER"><B>Use this name in XML files</B>
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER"><B>Name Type</B>
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER"><B>Xerces converts to this Java Encoder Name</B>
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">8 bit Unicode</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">UTF-8
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">UTF8
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">ISO Latin 1</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ISO-8859-1
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">ISO-8859-1
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">ISO Latin 2</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ISO-8859-2
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">ISO-8859-2
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">ISO Latin 3</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ISO-8859-3
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">ISO-8859-3
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">ISO Latin 4</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ISO-8859-4
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">ISO-8859-4
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">ISO Latin Cyrillic</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ISO-8859-5
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">ISO-8859-5
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">ISO Latin Arabic</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ISO-8859-6
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">ISO-8859-6
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">ISO Latin Greek</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ISO-8859-7
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">ISO-8859-7
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">ISO Latin Hebrew</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ISO-8859-8
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">ISO-8859-8
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">ISO Latin 5</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ISO-8859-9
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">ISO-8859-9
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: US</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-us
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp037
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Canada</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-ca
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp037
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Netherlands</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-nl
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp037
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Denmark</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-dk
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp277
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Norway</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-no
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp277
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Finland</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-fi
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp278
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Sweden</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-se
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp278
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Italy</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-it
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp280
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Spain, Latin America</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-es
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp284
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Great Britain</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-gb
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp285
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: France</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-fr
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp297
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Arabic</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-ar1
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp420
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Hebrew</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-he
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp424
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Switzerland</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-ch
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp500
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Roece</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-roece
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp870
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Yogoslavia</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-yu
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp870
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Iceland</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-is
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp871
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">EBCDIC: Urdu</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">ebcdic-cp-ar2
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">IANA
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">cp918
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">Chinese for PRC, mixed 1/2 byte</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">gb2312
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">GB2312
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">Extended Unix Code, packed for Japanese</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">euc-jp
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">eucjis
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">Japanese: iso-2022-jp</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">iso-2020-jp
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">JIS
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">Japanese: Shift JIS</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">Shift_JIS
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">SJIS
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">Chinese: Big5</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">Big5
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">Big5
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">Extended Unix Code, packed for Korean</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">euc-kr
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">iso2022kr
 *      </TD>
 *  </TR>
 *  <TR>
 *      <TD WIDTH="33%">Cyrillic</TD>
 *      <TD WIDTH="15%">
 *          <P ALIGN="CENTER">koi8-r
 *      </TD>
 *      <TD WIDTH="12%">
 *          <P ALIGN="CENTER">MIME
 *      </TD>
 *      <TD WIDTH="31%">
 *          <P ALIGN="CENTER">koi8-r
 *      </TD>
 *  </TR>
 * </TABLE>
 *
 * @version $Revision: 515 $ $Date: 2008-03-17 22:02:23 +0100 (Mon, 17 Mar 2008) $
 * @author TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;
 */
public class MIME2Java {
    static private Hashtable s_enchash;
    static private Hashtable s_revhash;
    static {
        s_enchash = new Hashtable();
        //    <preferred MIME name>, <Java encoding name>
        s_enchash.put("UTF-8", "UTF8");
        s_enchash.put("US-ASCII",        "8859_1");    // ?
        s_enchash.put("ISO-8859-1",      "8859_1");
        s_enchash.put("ISO-8859-2",      "8859_2");
        s_enchash.put("ISO-8859-3",      "8859_3");
        s_enchash.put("ISO-8859-4",      "8859_4");
        s_enchash.put("ISO-8859-5",      "8859_5");
        s_enchash.put("ISO-8859-6",      "8859_6");
        s_enchash.put("ISO-8859-7",      "8859_7");
        s_enchash.put("ISO-8859-8",      "8859_8");
        s_enchash.put("ISO-8859-9",      "8859_9");
        s_enchash.put("ISO-2022-JP",     "JIS");
        s_enchash.put("SHIFT_JIS",       "SJIS");
        s_enchash.put("EUC-JP",          "EUCJIS");
        s_enchash.put("GB2312",          "GB2312");
        s_enchash.put("BIG5",            "Big5");
        s_enchash.put("EUC-KR",          "KSC5601");
        s_enchash.put("ISO-2022-KR",     "ISO2022KR");
        s_enchash.put("KOI8-R",          "KOI8_R");
        s_enchash.put("EBCDIC-CP-US",    "CP037");
        s_enchash.put("EBCDIC-CP-CA",    "CP037");
        s_enchash.put("EBCDIC-CP-NL",    "CP037");
        s_enchash.put("EBCDIC-CP-DK",    "CP277");
        s_enchash.put("EBCDIC-CP-NO",    "CP277");
        s_enchash.put("EBCDIC-CP-FI",    "CP278");
        s_enchash.put("EBCDIC-CP-SE",    "CP278");
        s_enchash.put("EBCDIC-CP-IT",    "CP280");
        s_enchash.put("EBCDIC-CP-ES",    "CP284");
        s_enchash.put("EBCDIC-CP-GB",    "CP285");
        s_enchash.put("EBCDIC-CP-FR",    "CP297");
        s_enchash.put("EBCDIC-CP-AR1",   "CP420");
        s_enchash.put("EBCDIC-CP-HE",    "CP424");
        s_enchash.put("EBCDIC-CP-CH",    "CP500");
        s_enchash.put("EBCDIC-CP-ROECE", "CP870");
        s_enchash.put("EBCDIC-CP-YU",    "CP870");
        s_enchash.put("EBCDIC-CP-IS",    "CP871");
        s_enchash.put("EBCDIC-CP-AR2",   "CP918");
                                                // j:CNS11643 -> EUC-TW?
                                                // ISO-2022-CN? ISO-2022-CN-EXT?
        s_revhash = new Hashtable();
        //    <Java encoding name>, <preferred MIME name>
        s_revhash.put("UTF8", "UTF-8");
        //s_revhash.put("8859_1", "US-ASCII");    // ?
        s_revhash.put("8859_1", "ISO-8859-1");
        s_revhash.put("8859_2", "ISO-8859-2");
        s_revhash.put("8859_3", "ISO-8859-3");
        s_revhash.put("8859_4", "ISO-8859-4");
        s_revhash.put("8859_5", "ISO-8859-5");
        s_revhash.put("8859_6", "ISO-8859-6");
        s_revhash.put("8859_7", "ISO-8859-7");
        s_revhash.put("8859_8", "ISO-8859-8");
        s_revhash.put("8859_9", "ISO-8859-9");
        s_revhash.put("JIS", "ISO-2022-JP");
        s_revhash.put("SJIS", "Shift_JIS");
        s_revhash.put("EUCJIS", "EUC-JP");
        s_revhash.put("GB2312", "GB2312");
        s_revhash.put("BIG5", "Big5");
        s_revhash.put("KSC5601", "EUC-KR");
        s_revhash.put("ISO2022KR", "ISO-2022-KR");
        s_revhash.put("KOI8_R", "KOI8-R");
        s_revhash.put("CP037", "EBCDIC-CP-US");
        s_revhash.put("CP037", "EBCDIC-CP-CA");
        s_revhash.put("CP037", "EBCDIC-CP-NL");
        s_revhash.put("CP277", "EBCDIC-CP-DK");
        s_revhash.put("CP277", "EBCDIC-CP-NO");
        s_revhash.put("CP278", "EBCDIC-CP-FI");
        s_revhash.put("CP278", "EBCDIC-CP-SE");
        s_revhash.put("CP280", "EBCDIC-CP-IT");
        s_revhash.put("CP284", "EBCDIC-CP-ES");
        s_revhash.put("CP285", "EBCDIC-CP-GB");
        s_revhash.put("CP297", "EBCDIC-CP-FR");
        s_revhash.put("CP420", "EBCDIC-CP-AR1");
        s_revhash.put("CP424", "EBCDIC-CP-HE");
        s_revhash.put("CP500", "EBCDIC-CP-CH");
        s_revhash.put("CP870", "EBCDIC-CP-ROECE");
        s_revhash.put("CP870", "EBCDIC-CP-YU");
        s_revhash.put("CP871", "EBCDIC-CP-IS");
        s_revhash.put("CP918", "EBCDIC-CP-AR2");
    }
    private MIME2Java() {
    }
    /**
     * Convert a MIME charset name, also known as an XML encoding name, to a Java encoding name.
     * @param   mimeCharsetName Case insensitive MIME charset name: <code>UTF-8, US-ASCII, ISO-8859-1,
     *                          ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6,
     *                          ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-2022-JP, Shift_JIS,
     *                          EUC-JP, GB2312, Big5, EUC-KR, ISO-2022-KR, KOI8-R,
     *                          EBCDIC-CP-US, EBCDIC-CP-CA, EBCDIC-CP-NL, EBCDIC-CP-DK,
     *                          EBCDIC-CP-NO, EBCDIC-CP-FI, EBCDIC-CP-SE, EBCDIC-CP-IT,
     *                          EBCDIC-CP-ES, EBCDIC-CP-GB, EBCDIC-CP-FR, EBCDIC-CP-AR1,
     *                          EBCDIC-CP-HE, EBCDIC-CP-CH, EBCDIC-CP-ROECE, EBCDIC-CP-YU,
     *                          EBCDIC-CP-IS and EBCDIC-CP-AR2</code>.
     * @return                  Java encoding name, or <var>null</var> if <var>mimeCharsetName</var>
     *                          is unknown.
     * @see #reverse
     */
    public static String convert(String mimeCharsetName) {
        return (String)s_enchash.get(mimeCharsetName.toUpperCase());
    }
    /**
     * Convert a Java encoding name to MIME charset name.
     * Available values of <i>encoding</i> are "UTF8", "8859_1", "8859_2", "8859_3", "8859_4",
     * "8859_5", "8859_6", "8859_7", "8859_8", "8859_9", "JIS", "SJIS", "EUCJIS",
     * "GB2312", "BIG5", "KSC5601", "ISO2022KR",  "KOI8_R", "CP037", "CP277", "CP278",
     * "CP280", "CP284", "CP285", "CP297", "CP420", "CP424", "CP500", "CP870", "CP871" and "CP918".
     * @param   encoding    Case insensitive Java encoding name: <code>UTF8, 8859_1, 8859_2, 8859_3,
     *                      8859_4, 8859_5, 8859_6, 8859_7, 8859_8, 8859_9, JIS, SJIS, EUCJIS,
     *                      GB2312, BIG5, KSC5601, ISO2022KR, KOI8_R, CP037, CP277, CP278,
     *                      CP280, CP284, CP285, CP297, CP420, CP424, CP500, CP870, CP871
     *                      and CP918</code>.
     * @return              MIME charset name, or <var>null</var> if <var>encoding</var> is unknown.
     * @see #convert
     */
    public static String reverse(String encoding) {
        return (String)s_revhash.get(encoding.toUpperCase());
    }
}





Create Mime Message

import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    props.put("mail.host", "mail.cloud9.net");
    Session mailConnection = Session.getInstance(props, null);
    Message msg = new MimeMessage(mailConnection);
    Address a = new InternetAddress("a@a.ru", "A a");
    Address b = new InternetAddress("fake@jexp.ru");
    msg.setContent("Mail contect", "text/plain");
    msg.setFrom(a);
    msg.setRecipient(Message.RecipientType.TO, b);
    msg.setSubject("subject");
    Transport.send(msg);
  }
}





Frequently used MIME types

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/* $Id: MimeConstants.java 606580 2007-12-23 17:45:02Z jeremias $ */

/**
 * Frequently used MIME types for various file formats used by the XML Graphics project.
 */
public interface MimeConstants {
    /** Portable Document Format */
    String MIME_PDF             = "application/pdf";
    
    /** PostScript */
    String MIME_POSTSCRIPT      = "application/postscript";
    /** Encapsulated PostScript (same MIME type as PostScript) */
    String MIME_EPS             = MIME_POSTSCRIPT;
    
    /** HP"s PCL */
    String MIME_PCL             = "application/x-pcl";
    /** HP"s PCL (alternative MIME type) */
    String MIME_PCL_ALT         = "application/vnd.hp-PCL";
    
    /** IBM"s AFP */
    String MIME_AFP             = "application/x-afp";
    /** IBM"s AFP (alternative MIME type) */
    String MIME_AFP_ALT         = "application/vnd.ibm.modcap";
    
    /** Plain text */
    String MIME_PLAIN_TEXT      = "text/plain";
    
    /** Rich text format */
    String MIME_RTF             = "application/rtf";
    /** Rich text format (alternative 1) */
    String MIME_RTF_ALT1        = "text/richtext";
    /** Rich text format (alternative 2) */
    String MIME_RTF_ALT2        = "text/rtf";
    
    /** FrameMaker"s MIF */
    String MIME_MIF             = "application/mif";
    
    /** Structured Vector Graphics */
    String MIME_SVG             = "image/svg+xml";
    
    /** GIF images */
    String MIME_GIF             = "image/gif";
    /** PNG images */
    String MIME_PNG             = "image/png";
    /** JPEG images */
    String MIME_JPEG            = "image/jpeg";
    /** TIFF images */
    String MIME_TIFF            = "image/tiff";
       
    /** Proposed but non-registered MIME type for XSL-FO */
    String MIME_XSL_FO          = "text/xsl";
    
}





Known mime types

/*
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
 * 
 * Project: OpenSubsystems
 * 
 * $Id: MimeTypeConstants.java,v 1.6 2007/01/07 06:14:01 bastafidli Exp $
 * 
 * This program is free software; you can 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. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

import java.util.HashMap;
import java.util.Map;
/**
 * Known mime types.
 * Originally copied from HandlerContext.java taken from Jetty 3.0.4 
 * now updated from mime.properties from Jetty 5.1.10.
 * 
 * @version $Id: MimeTypeConstants.java,v 1.6 2007/01/07 06:14:01 bastafidli Exp $
 * @author Julo Legeny
 * @code.reviewer Miro Halas
 * @code.reviewed 1.4 2005/07/29 07:36:24 bastafidli
 */
public final class MimeTypeConstants 
{
   // Attributes ///////////////////////////////////////////////////////////////
   
   /**
    * Default Mime type
    */
   protected static String s_strDefaultMimeType = "text/html";
   /**
    * Hash map that will be stored types in
    */
   protected static Map s_mapMimeTypes = null;
   
   // Constructors /////////////////////////////////////////////////////////////
   
   /**
    * Static initializer;
    */
   static 
   {
      s_mapMimeTypes = new HashMap(161);
      s_mapMimeTypes.put("ai", "application/postscript");
      s_mapMimeTypes.put("aif", "audio/x-aiff");
      s_mapMimeTypes.put("aifc", "audio/x-aiff");
      s_mapMimeTypes.put("aiff", "audio/x-aiff");
      s_mapMimeTypes.put("asc", "text/plain");
      s_mapMimeTypes.put("asf", "video/x.ms.asf");
      s_mapMimeTypes.put("asx", "video/x.ms.asx");
      s_mapMimeTypes.put("au", "audio/basic");
      s_mapMimeTypes.put("avi", "video/x-msvideo");
      s_mapMimeTypes.put("bcpio", "application/x-bcpio");
      s_mapMimeTypes.put("bin", "application/octet-stream");
      s_mapMimeTypes.put("cab", "application/x-cabinet");
      s_mapMimeTypes.put("cdf", "application/x-netcdf");
      s_mapMimeTypes.put("class", "application/java-vm");
      s_mapMimeTypes.put("cpio", "application/x-cpio");
      s_mapMimeTypes.put("cpt", "application/mac-compactpro");
      s_mapMimeTypes.put("crt", "application/x-x509-ca-cert");
      s_mapMimeTypes.put("csh", "application/x-csh");
      s_mapMimeTypes.put("css", "text/css");
      s_mapMimeTypes.put("csv", "text/comma-separated-values");
      s_mapMimeTypes.put("dcr", "application/x-director");
      s_mapMimeTypes.put("dir", "application/x-director");
      s_mapMimeTypes.put("dll", "application/x-msdownload");
      s_mapMimeTypes.put("dms", "application/octet-stream");
      s_mapMimeTypes.put("doc", "application/msword");
      s_mapMimeTypes.put("dtd", "application/xml-dtd");
      s_mapMimeTypes.put("dvi", "application/x-dvi");
      s_mapMimeTypes.put("dxr", "application/x-director");
      s_mapMimeTypes.put("eps", "application/postscript");
      s_mapMimeTypes.put("etx", "text/x-setext");
      s_mapMimeTypes.put("exe", "application/octet-stream");
      s_mapMimeTypes.put("ez", "application/andrew-inset");
      s_mapMimeTypes.put("gif", "image/gif");
      s_mapMimeTypes.put("gtar", "application/x-gtar");
      s_mapMimeTypes.put("gz", "application/gzip");
      s_mapMimeTypes.put("gzip", "application/gzip");
      s_mapMimeTypes.put("hdf", "application/x-hdf");
      s_mapMimeTypes.put("htc", "text/x-component");
      s_mapMimeTypes.put("hqx", "application/mac-binhex40");
      s_mapMimeTypes.put("html", "text/html");
      s_mapMimeTypes.put("htm", "text/html");
      s_mapMimeTypes.put("ice", "x-conference/x-cooltalk");
      s_mapMimeTypes.put("ief", "image/ief");
      s_mapMimeTypes.put("iges", "model/iges");
      s_mapMimeTypes.put("igs", "model/iges");
      s_mapMimeTypes.put("jar", "application/java-archive");
      s_mapMimeTypes.put("java", "text/plain");
      s_mapMimeTypes.put("jnlp", "application/x-java-jnlp-file");
      s_mapMimeTypes.put("jpeg", "image/jpeg");
      s_mapMimeTypes.put("jpe", "image/jpeg");
      s_mapMimeTypes.put("jpg", "image/jpeg");
      s_mapMimeTypes.put("js", "application/x-javascript");
      s_mapMimeTypes.put("jsp", "text/plain");
      s_mapMimeTypes.put("kar", "audio/midi");
      s_mapMimeTypes.put("latex", "application/x-latex");
      s_mapMimeTypes.put("lha", "application/octet-stream");
      s_mapMimeTypes.put("lzh", "application/octet-stream");
      s_mapMimeTypes.put("man", "application/x-troff-man");
      s_mapMimeTypes.put("mathml", "application/mathml+xml");
      s_mapMimeTypes.put("me", "application/x-troff-me");
      s_mapMimeTypes.put("mesh", "model/mesh");
      s_mapMimeTypes.put("mid", "audio/midi");
      s_mapMimeTypes.put("midi", "audio/midi");
      s_mapMimeTypes.put("mif", "application/vnd.mif");
      s_mapMimeTypes.put("mol", "chemical/x-mdl-molfile");
      s_mapMimeTypes.put("movie", "video/x-sgi-movie");
      s_mapMimeTypes.put("mov", "video/quicktime");
      s_mapMimeTypes.put("mp2", "audio/mpeg");
      s_mapMimeTypes.put("mp3", "audio/mpeg");
      s_mapMimeTypes.put("mpeg", "video/mpeg");
      s_mapMimeTypes.put("mpe", "video/mpeg");
      s_mapMimeTypes.put("mpga", "audio/mpeg");
      s_mapMimeTypes.put("mpg", "video/mpeg");
      s_mapMimeTypes.put("ms", "application/x-troff-ms");
      s_mapMimeTypes.put("msh", "model/mesh");
      s_mapMimeTypes.put("msi", "application/octet-stream");
      s_mapMimeTypes.put("nc", "application/x-netcdf");
      s_mapMimeTypes.put("oda", "application/oda");
      s_mapMimeTypes.put("ogg", "application/ogg");
      s_mapMimeTypes.put("pbm", "image/x-portable-bitmap");
      s_mapMimeTypes.put("pdb", "chemical/x-pdb");
      s_mapMimeTypes.put("pdf", "application/pdf");
      s_mapMimeTypes.put("pgm", "image/x-portable-graymap");
      s_mapMimeTypes.put("pgn", "application/x-chess-pgn");
      s_mapMimeTypes.put("png", "image/png");
      s_mapMimeTypes.put("pnm", "image/x-portable-anymap");
      s_mapMimeTypes.put("ppm", "image/x-portable-pixmap");
      s_mapMimeTypes.put("ppt", "application/vnd.ms-powerpoint");
      s_mapMimeTypes.put("ps", "application/postscript");
      s_mapMimeTypes.put("qt", "video/quicktime");
      s_mapMimeTypes.put("ra", "audio/x-pn-realaudio");
      s_mapMimeTypes.put("ra", "audio/x-realaudio");
      s_mapMimeTypes.put("ram", "audio/x-pn-realaudio");
      s_mapMimeTypes.put("ras", "image/x-cmu-raster");
      s_mapMimeTypes.put("rdf", "application/rdf+xml");
      s_mapMimeTypes.put("rgb", "image/x-rgb");
      s_mapMimeTypes.put("rm", "audio/x-pn-realaudio");
      s_mapMimeTypes.put("roff", "application/x-troff");
      s_mapMimeTypes.put("rpm", "application/x-rpm");
      s_mapMimeTypes.put("rpm", "audio/x-pn-realaudio");
      s_mapMimeTypes.put("rtf", "application/rtf");
      s_mapMimeTypes.put("rtx", "text/richtext");
      s_mapMimeTypes.put("ser", "application/java-serialized-object");
      s_mapMimeTypes.put("sgml", "text/sgml");
      s_mapMimeTypes.put("sgm", "text/sgml");
      s_mapMimeTypes.put("sh", "application/x-sh");
      s_mapMimeTypes.put("shar", "application/x-shar");
      s_mapMimeTypes.put("silo", "model/mesh");
      s_mapMimeTypes.put("sit", "application/x-stuffit");
      s_mapMimeTypes.put("skd", "application/x-koan");
      s_mapMimeTypes.put("skm", "application/x-koan");
      s_mapMimeTypes.put("skp", "application/x-koan");
      s_mapMimeTypes.put("skt", "application/x-koan");
      s_mapMimeTypes.put("smi", "application/smil");
      s_mapMimeTypes.put("smil", "application/smil");
      s_mapMimeTypes.put("snd", "audio/basic");
      s_mapMimeTypes.put("spl", "application/x-futuresplash");
      s_mapMimeTypes.put("src", "application/x-wais-source");
      s_mapMimeTypes.put("sv4cpio", "application/x-sv4cpio");
      s_mapMimeTypes.put("sv4crc", "application/x-sv4crc");
      s_mapMimeTypes.put("svg", "image/svg+xml");
      s_mapMimeTypes.put("swf", "application/x-shockwave-flash");
      s_mapMimeTypes.put("t", "application/x-troff");
      s_mapMimeTypes.put("tar", "application/x-tar");
      s_mapMimeTypes.put("tar.gz", "application/x-gtar");
      s_mapMimeTypes.put("tcl", "application/x-tcl");
      s_mapMimeTypes.put("tex", "application/x-tex");
      s_mapMimeTypes.put("texi", "application/x-texinfo");
      s_mapMimeTypes.put("texinfo", "application/x-texinfo");
      s_mapMimeTypes.put("tgz", "application/x-gtar");
      s_mapMimeTypes.put("tiff", "image/tiff");
      s_mapMimeTypes.put("tif", "image/tiff");
      s_mapMimeTypes.put("tr", "application/x-troff");
      s_mapMimeTypes.put("tsv", "text/tab-separated-values");
      s_mapMimeTypes.put("txt", "text/plain");
      s_mapMimeTypes.put("ustar", "application/x-ustar");
      s_mapMimeTypes.put("vcd", "application/x-cdlink");
      s_mapMimeTypes.put("vrml", "model/vrml");
      s_mapMimeTypes.put("vxml", "application/voicexml+xml");
      s_mapMimeTypes.put("wav", "audio/x-wav");
      s_mapMimeTypes.put("wbmp", "image/vnd.wap.wbmp");
      s_mapMimeTypes.put("wmlc", "application/vnd.wap.wmlc");
      s_mapMimeTypes.put("wmlsc", "application/vnd.wap.wmlscriptc");
      s_mapMimeTypes.put("wmls", "text/vnd.wap.wmlscript");
      s_mapMimeTypes.put("wml", "text/vnd.wap.wml");
      s_mapMimeTypes.put("wrl", "model/vrml");
      s_mapMimeTypes.put("wtls-ca-certificate", "application/vnd.wap.wtls-ca-certificate");
      s_mapMimeTypes.put("xbm", "image/x-xbitmap");
      s_mapMimeTypes.put("xht", "application/xhtml+xml");
      s_mapMimeTypes.put("xhtml", "application/xhtml+xml");
      s_mapMimeTypes.put("xls", "application/vnd.ms-excel");
      s_mapMimeTypes.put("xml", "application/xml");
      s_mapMimeTypes.put("xpm", "image/x-xpixmap");
      s_mapMimeTypes.put("xpm", "image/x-xpixmap");
      s_mapMimeTypes.put("xsl", "application/xml");
      s_mapMimeTypes.put("xslt", "application/xslt+xml");
      s_mapMimeTypes.put("xul", "application/vnd.mozilla.xul+xml");
      s_mapMimeTypes.put("xwd", "image/x-xwindowdump");
      s_mapMimeTypes.put("xyz", "chemical/x-xyz");
      s_mapMimeTypes.put("z", "application/compress");
      s_mapMimeTypes.put("zip", "application/zip");
   }
   
   // Constructors /////////////////////////////////////////////////////////////
   
   /** 
    * Private constructor since this class cannot be instantiated
    */
   private MimeTypeConstants(
   )
   {
      // Do nothing
   }
   
   // Public methods ///////////////////////////////////////////////////////////
   
   /**
    * Method getting particular Mime type for the extension (key)
    * 
    * @param strKey - key value for returning Mime type
    * @return String
    */
   public static String getMimeType(
      String strKey
   )
   {
      String strMimeType = null;
      // get value for particular key
      strMimeType = s_mapMimeTypes.get(strKey).toString();
      if ((strMimeType == null) || (strMimeType.trim().length() == 0))
      {
         strMimeType = s_strDefaultMimeType;
      }
      
      return strMimeType;
   }
}





MIME type mappings

text/plain  txt text
text/html   html htm
text/xml    xml
text/css    css
text/rtf    rtf
application/rtf rtf
text/vnd.wap.wml    wml
image/jpeg  jpeg jpg jpe
image/png   png
image/gif   gif
image/tiff  tiff tif
image/svg+xml   svg svgz
image/svg-xml   svg
application/pdf pdf
model/vrml  wrl
application/smil    smil
text/javascript js
application/x-javascript    js
text/vbscript   vbs
application/zip zip
video/mpeg  mpeg mpg mpe
video/quicktime mov
audio/midi  mid
audio/mpeg  mp3
text/x-vcard    vcf
text/rdf    rdf
application/octet-stream





MIME types to use for various requests

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/**
 *
 * <p>Utility class for declaring MIME types to use for various requests and provide
 * utility manipulation methods.</p>
 * <p>Added Content-Encoding capability, with defaults
 *
 * @author 
 * @version $Id: MimeType.java 516448 2007-03-09 16:25:47Z ate $
 */
public class MimeType
{
    
    public static final MimeType HTML  = new MimeType("text/html", "UTF-8"); //FIXME: test
    public static final MimeType XHTML = new MimeType("text/xhtml");
    public static final MimeType WML   = new MimeType("text/vnd.wap.wml");
    public static final MimeType XML   = new MimeType("text/xml");
    public static final MimeType VXML  = new MimeType("text/vxml");
    
    /**
     * Standard ContentType String, with no encoding appended.
     */
    private String mimeType = "";
    /**
     * null value means default encoding.
     * Otherwise, charset to be used.
     */
    private String charSet = null;
    
    public MimeType(String mimeType)
    {
        if (mimeType == null)
        {
            throw new NullPointerException();
        }
        this.mimeType = mimeType;
    }
    
    /**
     *
     */
    public MimeType(String mimeType, String charSet)
    {
        if (mimeType == null)
        {
            throw new NullPointerException();
        }
        this.mimeType = mimeType;
        this.charSet = charSet;
    }
    
    /** Extracts from this MimeType a user-friendly identifying code
     * ie "html" for "text/html" or "wml" for "text/vnd.wap.wml"
     *
     * @return the simplified type
     */
    public String getCode()
    {
        String type = this.mimeType;
        // get everything after "/"
        type = type.substring(type.indexOf("/") + 1);
        // remove any dot in the name
        int idx = type.lastIndexOf(".");
        if (idx >= 0)
        {
            type = type.substring(idx + 1);
        }
        //remove anything before a "-"
        idx = type.lastIndexOf("-");
        if (idx >= 0)
        {
            type = type.substring(idx + 1);
        }
        
        return type.toLowerCase();
    }
    
    /**
     * Return the media type associated
     */
    public String getContentType()
    {
        return this.mimeType;
    }
    
    /**
     * Return the character encoding associated, if any
     */
    public String getCharSet()
    {
        return this.charSet;
    }
    
    /**
     * Convert this MimeType to its external String representation
     */
    public String toString()
    {
        if (null == this.charSet)
        {
            return this.mimeType;
        }
        return this.mimeType + "; charset=" + this.charSet;
    }
    
    /**
     * Compare one MimeType to another
     */
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        
        if (obj instanceof MimeType)
        {
            MimeType comp = (MimeType) obj;
            return this.toString().equals(comp.toString());
        }
        else
        {
            return false;
        }
    }
    
}