Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java API
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Other JSE/JEE APIs
Sending a PDF Attachment via EMail
Mahesh Trikannad
Ranch Hand
Posts: 73
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I want to send a PDF Attachment using
Java
mail.
I have the PDF as a byte array. I dont want to save it to a file, but want to create a attachment straight from the byte array.
How can I do that, using MimeMultiPart and MimeBodyPart
Mahesh
Mahesh Trikannad
Piotr Maj
Greenhorn
Posts: 2
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
yes, this is possible. All you need is to write a custom datasource. Look at the code below:
package examples; import java.io.*; import javax.activation.*; import javax.mail.Session; import javax.mail.internet.*; public class ByteArrayTest { public static void main(String[] args) throws Exception { final byte[] attachment = "Attachment".getBytes(); MimeMessage message = new MimeMessage((Session)null); MimeMultipart multipart = new MimeMultipart(); MimeBodyPart part = new MimeBodyPart(); part.setDataHandler( new DataHandler( new DataSource() { public String getContentType() { return "application/pdf"; } public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(attachment); } public String getName() { return "myfile.pdf"; } public OutputStream getOutputStream() throws IOException { return null; }})); multipart.addBodyPart(part); message.setContent(multipart); message.writeTo(System.out); } }
I hope this will be helpful.
Mahesh Trikannad
Ranch Hand
Posts: 73
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks Piotr,
I found another solution, where MimeBodyPart is extended , to provide functionality to set the content to a bytearray .
Guess there are different ways to do it, but none very obvious.
Mahesh
Mahesh Trikannad
I want my playground back. Here, I'll give you this tiny ad for it:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
getting error while displaying PDF from jsp
Help: Attaching generated pdf file with an email
sending pdf or any attachment from a java webservice to java client using JAX-WS and SAAJ
converting byte array to pdf
how to open attachments
More...