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 Murach's Java Servlets and JSP this week in the Servlets 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>(Throwable.java)
at javax.mail.MessagingException.<init>(MessagingException.java:34)
at javax.mail.internet.ContentType.<init>(ContentType.java:72)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1016)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:790)
at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:216)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:1841)
at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:1822)
at javax.mail.Transport.send(Transport.java:80)
at ct.fw.mail.SMTPMailSender.run(SMTPMailSender.java:294)
at java.lang.Thread.run(Thread.java:802)

Just before sending the message, the headers are:

Date:Fri, 15 Jul 2005 18:39:56 -0500 (CDT)
From onotreply@ptsteams.com
Reply-To onotreply@ptsteams.com

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 http://jakarta.apache.org/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.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Send PDF in JavaMail
Similar Threads
How To Send Multiple Attachments In An Email
Java Mail with Attachments
AXIS attachments
how to send mail using gmail server
JavaMail installation