getBytes返回没有BOM的UTF-8

我以这种方式通过电子邮件发送一个txt文件。 文件的数据在一个StringBuffer中。

文件构build:

StringBuffer lBufferFinal = new StringBuffer(); // Put some text in lBufferFinal FichierByteVO lFichier = new FichierByteVO(); lFichier.setFileName("Statistic.txt"); lFichier.setMIMEType("text/plain"); lFichier.setFile(lBufferFinal.toString().getBytes(Charset.forName("UTF-8"))); 

在电子邮件中添加文件:

 MimeMessage lMessage = new MimeMessage(lSession); // Do thing tu put sender, receiver, subject etc to the MimeMessage Multipart = new MimeMultipart(); BodyPart lMessageBodyPart = new MimeBodyPart(); lMessageBodyPart.setText("Here is an email !"); lMultipart.addBodyPart(lMessageBodyPart); lMessageBodyPart = new MimeBodyPart(); DataHandler lDataHandler = new DataHandler(new ByteArrayDataSource(lFichier.getFile(), lFichier.getMIMEType())); lMessageBodyPart.setDataHandler(lDataHandler); lMessageBodyPart.setFileName(lFichier.getFileName()); lMultipart.addBodyPart(lMessageBodyPart); } lMessage.setContent(lMultipart); 

我通过电子邮件收到的文件默认在记事本++或excel(谁不能识别重音符)中打开为“UTF-8 without BOM”。

所以我需要用excel打开,所以要使用BOM编码的UTF-8。

你有什么build议吗 ?

最后,我使用.CSV扩展和ISO-8859-1,它运行良好!

不方便使用BOM。 你可以像这样把邮件的编码:

 JavaMailSenderImpl mailer = new JavaMailSenderImpl(); ... mailer.setDefaultEncoding("ISO-8859-1"); 

这将识别口音