This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Send PDF in JavaMail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Send PDF in JavaMail" Watch "Send PDF in JavaMail" New topic

Send PDF in JavaMail

Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
I have little experience with javamail and would really appreciate any help you could offer on this error. I'm trying to send a pdf (in memory) as an attachment to an email.

So far I've got:

And this helper class:

When run, I'm getting the following exception:

SMTPMailSender |javax.mail.internet.ParseException
at java.lang.Throwable.<init>(
at javax.mail.MessagingException.<init>(
at javax.mail.internet.ContentType.<init>(
at javax.mail.internet.MimeBodyPart.updateHeaders(
at javax.mail.internet.MimeBodyPart.updateHeaders(
at javax.mail.internet.MimeMultipart.updateHeaders(
at javax.mail.internet.MimeBodyPart.updateHeaders(
at javax.mail.internet.MimeMessage.updateHeaders(
at javax.mail.internet.MimeMessage.saveChanges(
at javax.mail.Transport.send(

Just before sending the message, the headers are:

Date:Fri, 15 Jul 2005 18:39:56 -0500 (CDT)

Can anybody tell me what I've done wrong here?

I also noticed that the DataHandler has objectMimeType set to "multipart/mixed;

Can this be right? Is this the problem?
[ July 15, 2005: Message edited by: Dave Wingate ]

Fun programming etcetera!
Dave Boden
Ranch Hand

Joined: Nov 08, 2000
Posts: 37
You have to write your implementation of DataSource so that it returns a new instance of the InputStream every time getInputStream() is called. You have to read it into a buffer in order to do this. Here's my implementation of InputStreamDataSource which I've tested (it works )

Amit Siinngghh

Joined: Jun 19, 2004
Posts: 12
try apache commons email

Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hello all,

Has anyone worked on SearchTerm?


Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Brilliant. Buffering the inputstream worked for me as well. Thanks for your help. I've been beating my head against the monitor for quite a while on this one.
[ October 10, 2005: Message edited by: Dave Wingate ]
Awais Bajwa
Ranch Hand

Joined: Jan 16, 2001
Posts: 191
Great work Dave..
but still I want to know what is the advantage of the following Buffering code.

Revanth Sharma

Joined: Jul 30, 2011
Posts: 14
I am trying to implement something similar to this. Does this work for xls files and doc files or does it mess the format.
madhava Thamata

Joined: Mar 24, 2013
Posts: 3
Hi All

I have same requriement like to attache the PDF in mail with out saving in local.
could any body provide some info about my requirement.

It is sorta covered in the JavaRanch Style Guide.
subject: Send PDF in JavaMail