Reading Arabic Attachments using JavaMail

后端 未结 3 1586
面向向阳花
面向向阳花 2021-01-06 23:53

I\'ve a problem of downloading arabic attachment files using java mail.

The file name is always ambiguous.

The problem is the Bodypart retrieve

3条回答
  •  遥遥无期
    2021-01-07 00:35

    The header is encoded according to the mechanism described in RFC 2047 (it's the encoded-word) which says that a section of a header matching =??B??= is a byte-encoded section. The says how to interpret the bytes, and (because it's the B style, not the Q style) the are base-64 encoded.

    This is all rather complex. Luckily, you can deal with this easily by using the static javax.mail.internet.MimeUtility.decodeText() method. That means you can switch to this:

    String filename = MimeUtility.decodeText(bodyPart.getFileName());
    

    Actually, you're better off combining that with the next line too as well:

    File f = new File("C:\\Attachments",
                      MimeUtility.decodeText(bodyPart.getFileName()));
    

    It's better because it avoids more trouble with building filenames than trying to do it all by hand. (It also means that you can factor out that literal pathname into some configuration location.)

提交回复
热议问题