Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help: Attaching generated pdf file with an email  RSS feed

 
Bashar Ayyash
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I need your help with the following case:

I have a servlet that generate a PDF file using iText. I want to attach the generated pdf object to JavaMail 1.4, without storing the pdf on my server.

Please help with this situation or give me links to sites that discuss attaching objects to JavaMail.

Regards,
[ July 19, 2006: Message edited by: Bashar Ayyash ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sending a mail with attachments is explained here.

The only difference would be that, instead of using a DataSource, you'd use the "MimeBodyPart(InputStream is)" constructor, to which you feed a stream of the bytes that make up the PDF.
 
Bashar Ayyash
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again,

but I get the generated PDF page, from iText, as Document object.
How can I forwared the object to the e-mail message to attach the pdf document?

Regards,
Bashar
 
Bashar Ayyash
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example to the code needed to generate PDF document, the codes are in a servlet doPost method :

ServletOutputStream out = response.getOutputStream();
Document document = new Document(PageSize.A4, 10, 10, 10, 10);
try {
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
document.open();

PdfPTable outerTable = new PdfPTable(1);
Font titleFont = new Font(helvetica, 21, Font.NORMAL);
PdfPCell cell = new PdfPCell(new Paragraph("Address\n\n\n", titleFont));
outerTable.addCell(cell);
cell = new PdfPCell(new Paragraph("Date: " + new java.util.Date()));
outerTable.addCell(cell);

document.add(outerTable);

document.close();
} catch (DocumentException e) {
e.printStackTrace();
}
resp.setContentType("application/pdf");
out.flush();

//---->
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using the servlet output stream, use a ByteArrayOutputStream that captures the data in a byte array. From that you can create a ByteArrayInputStream for use with the JavaMail API.
 
Bashar Ayyash
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you give me an example, please?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example of what - using ByteArray[In|Out]putStream?

The byte array serves as a temporary holding area for the contents of the PDF document in memory, because you don't want to write it to disk.

- you need a byte array variable
- use that to create a BAOS, which you pass to PdfWriter.getInstance
- then use it to create a BAIS
- pass the BAIS to the MimeBodyPart constructor
- create the attachment as described in the tutorial linked above
- Done!
 
Bashar Ayyash
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,

Thank you?, I attached the pdf correctly, but there is one problem. When I open the e-mail at the recipient person, I don't see/download the attached file? Where is the error in my code?:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document(PageSize.A4, 10, 10, 10, 10);
try {
PdfWriter writer = PdfWriter.getInstance(document, baos);
document.open();

PdfPTable outerTable = new PdfPTable(1);
Font titleFont = new Font(helvetica, 21, Font.NORMAL);
PdfPCell cell = new PdfPCell(new Paragraph("Address\n\n\n", titleFont));
outerTable.addCell(cell);
cell = new PdfPCell(new Paragraph("Date: " + new java.util.Date()));
outerTable.addCell(cell);

document.add(outerTable);

document.close();
} catch (DocumentException e) {
e.printStackTrace();
}
//resp.setContentType("application/pdf");
baos.flush();

//Generating an email with the attachment
String to = "bashar@ayyashelectra.com";
String from = "bashar@hotmail.com";
String host = "localhost";
boolean debug = true;
PrintWriter out = response.getWriter();

Properties props = new Properties();
props.put("mail.smtp.host", host);

Session session = Session.getInstance(props, null);
session.setDebug(debug);
try{
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
// msg.setRecipients(Message.RecipientType.CC, address);
msg.setSubject("JAVAMail APIs Multipart Test");
msg.setSentDate(new Date());

//Create and fill the first message
MimeBodyPart mpb1 = new MimeBodyPart();
mpb1.setText(msgText1);

//Create ByteArrayInputStream object
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

//Create and fill the second message part
MimeBodyPart mbp2 = new MimeBodyPart(bais);


//Create multipart
Multipart mp = new MimeMultipart();
mp.addBodyPart(mpb1);
mp.addBodyPart(mbp2);

//add the multipart to the message
msg.setContent(mp);

Transport.send(msg);
}catch (MessagingException mex){
mex.printStackTrace();
Exception ex = null;
if((ex = mex.getNextException()) != null){
ex.printStackTrace();
}
}
//---->

Regards,
 
Bashar Ayyash
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

can any body help me with the above code, as I mentioned earlier, I can successfully send an attachment of the generated pdf with the e-mail but at the other side the user can see the e-mail but without an attachmen.

Any help is realy appreciated.

Regards,
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anything particularly wrong with that. You could try adding this:Or you could try a method that works for me:And just to forestall you from asking for help again, you'll find ByteArrayDataSource in the demo directory of your JavaMail download.
 
Bashar Ayyash
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, you were all most helpfull.
Here is the correct codes:

mbp2.setFileName("Clearance.pdf");

mbp2.setDataHandler(new DataHandler(new ByteArrayDataSource(baos.toByteArray(),"application/pdf")));

We must specifiy the type in ByteArrayDataSource.

Regards,
[ July 23, 2006: Message edited by: Bashar Ayyash ]
 
Joshua Cloch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"We must specifiy the type in ByteArrayDataSource."

Actually,we donot need to specify the type of the attachment--any type will be ok.The only thing you need to do is remember to add the file extension of the attachment.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!