public class MimeUtility extends Object
MimeUtility
contains encoder and decoder information for MIME header and body text. This is an alternative to the javax.mail.internet.MimeUtility
class, but MimeUtillity
allows you to use Oracle or IANA character set names.
RFC 2047 specifies the MIME-safe header and body format. The mail header must be in the following US-ASCII character set encoding:
=? <charset> ? <encoding> ? <encoded-text> ?=
This output can be generated using the MimeUtility.encodeHeader(String, String, String)
method. The receiver of the data is able to decode the data based on the included character set (for example, iso-8859-1
) and encoding ("B"
or "Q"
) information using MimeUtility.decodeHeader(String)
.
Modifier and Type | Method and Description |
---|---|
static String |
decodeHeader(String srcstr)
Decodes a MIME-safe form into a
String object for MIME header as per RFC 2047. |
static String |
decodeText(String intext)
Decode an input "Unstructured" headers which are defined in RFC 822.
|
static String |
decodeText(String intext, String charset)
Decode an input "Unstructured" headers which are defined in RFC 822.
|
static String |
decodeWord(String eword)
Decode an input encoded mail-safe form word token accoding to RFC 2047.
|
static String |
decodeWord(String eword, String charset)
Same as decodeWord(String), except that the specified charset is used and the one declared in the string is ignored.
|
static String |
encodeHeader(String srcstr, String charset, String encoding)
Encodes a
String object into a MIME-safe form for MIME header as per RFC 2047. |
static String |
encodeText(String text)
Deprecated.
As of 10.1.2, use encodeText(text, charset, encoding) instead.
|
static String |
encodeText(String text, String charset, String encoding)
Encode an input RFC 822 text token into mail-safe form according to RFC 2047.
|
static String |
encodeText(String text, String charset, String encoding, boolean bFold)
Encode an input RFC 822 text token into mail-safe form according to RFC 2047.
|
static String |
encodeWord(String word) |
static String |
encodeWord(String word, String charset, String encoding) |
static String |
getDefaultJavaCharset()
Query the system property
file.encoding to get the default Java charset. |
static String |
javaCharset(String charset)
Get the equivalent Java encoding name of a MIME charset.
|
static String |
mimeCharset(String encoding)
Get the equivalent MIME charset name of a Java encoding.
|
public static String encodeHeader(String srcstr, String charset, String encoding) throws UnsupportedEncodingException
String
object into a MIME-safe form for MIME header as per RFC 2047.
Currently, AL16UTF16 (also known as UTF-16BE) is not supported.
The output includes the character set and encoding (for example, base64 or quoted-printable) information in the following form:
=? <charset> ? <encoding> ? <encoded-text> ?=
srcstr
- a String
object to be encodedcharset
- an Oracle or IANA character set nameencoding
- base64 ("B") or quoted-printable ("Q")String
objectUnsupportedEncodingException
- if the character set name is invalid or not supportedpublic static String decodeHeader(String srcstr) throws UnsupportedEncodingException
String
object for MIME header as per RFC 2047.
The input is expected to include the character set and encoding (for example, base64 or quoted-printable) information in the following form:
=? <charset> ? <encoding> ? <encoded-text> ?=
srcstr
- a String
object to be decoded.String
objectUnsupportedEncodingException
- - if the character set name is invalid or not supportedpublic static String mimeCharset(String encoding)
encoding
- a Java encodingpublic static String javaCharset(String charset)
charset
- a MIME charset namepublic static String getDefaultJavaCharset()
file.encoding
to get the default Java charset. Before it, if the system property mail.mime.charset
is set, a equivalent Java charset of this MIME charset will be returned.public static final String decodeWord(String eword, String charset) throws UnsupportedEncodingException
eword
- the possibly encoded valuecharset
- MIME charset nameUnsupportedEncodingException
- if the string is not an encoded-word according to RFC 2047 or the charset conversion failed.public static final String decodeWord(String eword) throws UnsupportedEncodingException
eword
- the possibly encoded mail-safe form tokenUnsupportedEncodingException
- if the input word is not an encoded email token according to RFC 2047 or the charset conversion fails.public static final String encodeText(String text) throws UnsupportedEncodingException
Note that we should use this method to encode only "unstructured" RFC 822 headers. Folding behaviour can be specified by setting the system property mail.mime.foldencodedwords
text
- mail header Unicode stringUnsupportedEncodingException
- if the encoding failspublic static final String encodeText(String text, String charset, String encoding, boolean bFold) throws UnsupportedEncodingException
Note that we should use this method to encode only "unstructured" RFC 822 headers. Folding behaviour can be specified by setting the system property mail.mime.foldencodedwords
text
- mail header Unicode stringcharset
- the character set used to encode the 'text'. Use the default platform charset if this parameter is null.encoding
- the transfer encoding in RFC 2045. Now there are only "B" and "Q". If this parameter is null, use the 'Q' encoding if more than half of the characters to be encoded are ASCII, otherwise use the "B" encoding.bFold
- if true the encoded word will be folded as per RFC 882, if false the encoded word will not be foldedUnsupportedEncodingException
- if the encoding failspublic static final String encodeText(String text, String charset, String encoding) throws UnsupportedEncodingException
Note that we should use this method to encode only "unstructured" RFC 822 headers. Folding behaviour can be specified by setting the system property mail.mime.foldencodedwords
text
- mail header Unicode stringcharset
- the character set used to encode the 'text'. Use the default platform charset if this parameter is null.encoding
- the transfer encoding in RFC 2045. Now there are only "B" and "Q". If this parameter is null, use the 'Q' encoding if more than half of the characters to be encoded are ASCII, otherwise use the "B" encoding.UnsupportedEncodingException
- if the encoding failspublic static final String decodeText(String intext) throws UnsupportedEncodingException
Decode the input string according to RFC 2047, Section 6.1.1. Invalid RFC 2047 style encoded headers are returned as is. If the decoding fails, an UnsupportedEncodingException is thrown.
intext
- the possibly encoded stringUnsupportedEncodingException
- if the decoding fails.public static final String decodeText(String intext, String charset) throws UnsupportedEncodingException
Decode the input string according to RFC 2047, Section 6.1.1. Invalid RFC 2047 style encoded headers are returned as is. If the decoding fails, an UnsupportedEncodingException is thrown.
intext
- the possibly encoded stringcharset
- MIME charset of etextUnsupportedEncodingException
- if the decoding fails.public static final String encodeWord(String word) throws UnsupportedEncodingException
UnsupportedEncodingException
public static String encodeWord(String word, String charset, String encoding) throws UnsupportedEncodingException
UnsupportedEncodingException