How to enforce charset (encoding) for text content?
843834Jan 4 2008 — edited Jun 4 2008I'm sending an e-mail with an attachment. The attachment contains CSV data and I want JavaMail to encode this using the UTF-8 character set.
So I create a MimeBodyPart as follows:
-----
String content = "\"Year\", \"Gross income\", \"Net income\"\r\n"
+ "\"2004\", \"\u20AC 23493\", \"\u20AC 11238\"\r\n"
+ "\"2005\", \"\u20AC 25932\", \"\u20AC 10320\"\r\n"
+ "\"2006\", \"\u20AC 28362\", \"\u20AC 12022\"\r\n"
+ "\"2007\", \"\u20AC 30129\", \"\u20AC 26021\"\r\n";
String fileName = "Income 2004 to 2007.csv";
String mimeType = "text/csv; charset=UTF-8; header=present";
DataHandler dh = new DataHandler(content, mimeType);
BodyPart part = new MimeBodyPart();
part.setFileName(fileName);
part.setDataHandler(dh);
-----
The MIME type, with "charset" and "header" parameters, is in accordance with RFCs 2046 (MIME) and 4180 (CSV):
- http://www.rfc-editor.org/rfc/rfc2046.txt
- http://www.rfc-editor.org/rfc/rfc4180.txt
However, when sending the e-mail using Transport.send(...) the e-mail comes in with the correct headers, but the content is still encoded in the platform encoding:
-----
------=_Part_1_14572016.1199456902904
Content-Type: text/csv; charset=UTF-8; header=present; name=Records.csv
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename=Records.csv
"Year", "Gross income", "Net income"
"2004", "=DB 23493", "=DB 11238"
"2005", "=DB 25932", "=DB 10320"
"2006", "=DB 28362", "=DB 12022"
"2007", "=DB 30129", "=DB 26021"
------=_Part_1_14572016.1199456902904--
-----
Note that the string "=DB" is the Euro-symbol, but encoded in the MacRoman character set, which is the platform default on my Mac workstation.
So it seems JavaMail or JAF is not interpreting the charset parameter.
How can I enforce that the content is encoded in the proper charset?