Attach CSV content to mail

One of my servlets creates CSV content in a String variable.

I would like to send this CSV as an attachment file, but everyone knows the limitations of GAE: it is not possible to create a file. So, I decided to find another solution.

Mine should attach the CSV string as follows:

String csv = ""; Message msg = new MimeMessage(session); msg.setDataHandler(new DataHandler(new ByteArrayDataSource(csv.getBytes(),"text/csv"))); msg.setFileName("data.csv"); 

I receive mail, but without an application. The CSV string is integrated into the bulk of the mail.

How to connect this CSV string, such as a CSV file, to mail?

thanks

+4
source share
2 answers

You need a 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); 
+7
source
  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); 
0
source

Source: https://habr.com/ru/post/1385938/


All Articles