message.setSubject(MimeUtility.encodeText(subject,"gb2312","b")); //对邮件标题进行base64编码

解决方案 »

  1.   

    你的编码方式不对,这个有专门的规范public class MailHeaderEncoder {
      public MailHeaderEncoder() {
      }
      static public String encodeByBase64(String source,String charset){
        if (source==null)
          return source;
        try{
          return "=?" + charset + "?B?" +
              new String(Base64.encode(source.getBytes(charset))) + "?=";
        }catch(java.io.UnsupportedEncodingException e){
          return source;
        }
      }
      static public void main(String[] args){
        String source=MailHeaderEncoder.encodeByBase64("我的测试文字","gb2312");
        System.out.println(source);
        System.out.print(MailHeaderDecoder.decode(source));
      }
    }参考RFC2047
        //charset "?" encoding "?" encoded-text
        //encoding= "B"
        //    The "B" encoding is identical to the "BASE64" encoding defined by RFC2045.
        //encoding= "Q"
        //    The "Q" encoding is similar to the "Quoted-Printable" content-
        //    transfer-encoding defined in RFC 2045.  It is designed to allow text
        //    containing mostly ASCII characters to be decipherable on an ASCII
         //   terminal without decoding.