LoginSignup
2
2

More than 5 years have passed since last update.

JavaMailでiPhoneが送信したcharset=cp932のメールを扱う

Last updated at Posted at 2018-04-18

iPhoneがcharset=cp932のメールを送ってくることがある。
Javaはcp932なんて知らないので、getContentするとJavaMailが死ぬ。

public static void convertContentType(Part part) throws MessagingException {
    String[] headers = part.getHeader("Content-Type");
    String contentType = headers[0].toLowerCase();

    if(contentType.contains("cp932")) {
        part.removeHeader("Content-Type");
        part.setHeader("Content-Type", contentType.replaceAll("cp932", "ms932"));
    }
}

これでgetContentする前にContent-Typeを変換する。
ms932ならJavaが理解できるのでgetContentできるようになる。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2