Java Mail MimeUtility encodeText UnsupportedEncodingException BASE64

时间:2015-08-04 03:01:38

标签: java encoding base64 javamail

作为我的J2EE应用程序电子邮件服务的一部分,我编码为BASE64

body = MimeUtility.encodeText(orig_mail_body,"UTF-8","BASE64");

但在某些情况下,它会抛出异常:

java.io.UnsupportedEncodingException: Unknown transfer encoding: BASE64
at javax.mail.internet.MimeUtility.encodeWord(MimeUtility.java:565)
at javax.mail.internet.MimeUtility.encodeText(MimeUtility.java:373)

我一直试图揭露为什么我会收到这条特别的信息,但无济于事。 有人可以照亮我吗?

2 个答案:

答案 0 :(得分:2)

这似乎是'编码的唯一有效值。论证是" B"或者" Q&#34 ;;所以我的代码应该是:

body = MimeUtility.encodeText(orig_mail_body,"UTF-8","B");

答案 1 :(得分:0)

如果你现在使用java 8,那么有一个类可以解决这个问题。

byte[] bytes = orig_mail_body.getBytes();
        Base64.Encoder encoder = Base64.getEncoder();
        String encode = encoder.encodeToString(bytes);
        System.out.print(encode);

春天:org.springframework.security.crypto.codec

    public static String base64Encode(String token) {
    byte[] encodedBytes = Base64.encode(token.getBytes());
    return new String(encodedBytes, Charset.forName("UTF-8"));
}


public static String base64Decode(String token) {
    byte[] decodedBytes = Base64.decode(token.getBytes());
    return new String(decodedBytes, Charset.forName("UTF-8"));
}

使用apache commons

  

org.apache.commons.codec.binary.Base64;

    byte[] encodedBytes = Base64.encodeBase64("Test".getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));
byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
System.out.println("decodedBytes " + new String(decodedBytes));

每期