Reputation: 869
One of my servlet creates CSV content in a String variable.
I'd like to send this CSV like an attachment file but everybody knows the limitations of GAE : it's impossible to create a file. So, I decided to find an another solution.
Mine is to attach the CSV string like that :
String csv = "";
Message msg = new MimeMessage(session);
msg.setDataHandler(new DataHandler(new ByteArrayDataSource(csv.getBytes(),"text/csv")));
msg.setFileName("data.csv");
I receive the mail but without attachment. The CSV string is integrated into the body part of the mail.
How to attach this CSV string like a CSV file into the mail?
Thanks
Upvotes: 6
Views: 18099
Reputation: 35961
You need MimeMultipart
message and attach it as a MimeBodyPart:
Message msg = new MimeMessage(session);
MimeBodyPart attachFilePart = new MimeBodyPart();
attachFilePart.setDataHandler(new DataHandler(new ByteArrayDataSource(csv.getBytes(),"text/csv")))
attachFilePart.setFileName("data.csv");
msg.addBodyPart(attachFilePart);
Upvotes: 11
Reputation: 547
javax.mail.Multipart multipart = new MimeMultipart();
javax.mail.internet.MimeBodyPart messageBodyPart = new javax.mail.internet.MimeBodyPart();
multipart.addBodyPart(messageBodyPart);
javax.activation.DataSource source = new FileDataSource("C:\\Notes\\data.csv");
messageBodyPart.setDataHandler( new DataHandler(source));
messageBodyPart.setFileName("data.csv");
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
MimeBodyPart part = new MimeBodyPart();
part.setText(text);
multipart.addBodyPart(part);
Upvotes: 1