Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attachment of CSV content into a mail

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

like image 611
user376112 Avatar asked Dec 12 '11 17:12

user376112


2 Answers

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);
like image 163
Igor Artamonov Avatar answered Oct 21 '22 04:10

Igor Artamonov


    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);
like image 28
Delta Avatar answered Oct 21 '22 04:10

Delta