My application has to send a textfile, which it first has to generate as a String. The text contains non-ASCII symbols, so i would like it to be UTF-8. I\'ve tried a lot of vari
This is a sample code that I use to send files (irrespective on encoding or data structure).
BodyPart fileBodyPart = new MimeBodyPart();
fileBodyPart.setDataHandler(new DataHandler(fileDataSource));
fileBodyPart.setFileName(attachment.getName());
fileBodyPart.setHeader("Content-Type", fileDataSource.getContentType());
fileBodyPart.setHeader("Content-ID", attachment.getName());
fileBodyPart.setDisposition(Part.INLINE);
Where fileDataSource
is a javax.activation.DataSource
(text file will be in here), and
fileBodyPart.setDisposition(Part.INLINE);
(PART.INLINE
means datasource is inlined with the message body, just like HTML emails, PART.ATTACHMENT
means datasource is an attachment).
Hope this helps.