dcsimg
Sending pdf attachment from binary input stream
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Shilps_Shrivastava
Posted On:   Thursday, May 9, 2002 08:15 AM

hi, I am trying to send an attachment with an email. What am i doing is :: I am reading a pdf file and storing the data in a byte array. Then read the bytes from the array in an input stream.Then get the object from the stream and set the content in the mimebodypart.I want this content to send in the attachment.but getting the error "". What should be the mime type for the pdf .I had tried a lot og mime types like application/octet-stream, multipart/mixed,multipart/alternative and many more but it dosent work. the code is pasted below. Can u plz help me..... Message msg = new MimeMessage(session); ByteArrayOutputStream baos =null; baos = new ByteArrayOutputStream(); FileInputStream fis = new FileInputStream("D:/Genius/Docs/insurance.pdf"); int data;   More>>

hi, I am trying to send an attachment with an email. What am i doing is :: I am reading a pdf file and storing the data in a byte array. Then read the bytes from the array in an input stream.Then get the object from the stream and set the content in the mimebodypart.I want this content to send in the attachment.but getting the error "". What should be the mime type for the pdf .I had tried a lot og mime types like application/octet-stream, multipart/mixed,multipart/alternative and many more but it dosent work. the code is pasted below. Can u plz help me..... Message msg = new MimeMessage(session); ByteArrayOutputStream baos =null; baos = new ByteArrayOutputStream(); FileInputStream fis = new FileInputStream("D:/Genius/Docs/insurance.pdf"); int data; while((data = fis.read()) != -1) { baos.write(data); } fis.close(); baos.flush(); baos.close(); byte[] bytes = baos.toByteArray(); System.out.println(">>>>>>>>>>>>>>>>>>>>"+bytes.length); ByteArrayInputStream bais = new ByteArrayInputStream(bytes); while((data = bais.read(bytes)) != -1) { } MimeBodyPart messageBodyPart1 = new MimeBodyPart(new InternetHeaders(), bytes); Object obj = messageBodyPart1.getContent(); String att_type = "multipart/mixed"; messageBodyPart1.setHeader("Content-Type", "application/pdf"); //messageBodyPart1.setHeader("Content-Transfer-Encoding", "base64"); messageBodyPart1.setHeader("Content-Disposition", "attachment"); //messageBodyPart.setText(message); MimeMultipart multipart = new MimeMultipart(); //multipart.setSubType("octet-stream"); messageBodyPart1.setContent(obj,"application/pdf"); // multipart.addBodyPart(messageBodyPart); messageBodyPart1.setFileName("abc.pdf"); multipart.addBodyPart(messageBodyPart1); msg.setContent(multipart); Thnx in advance.Hope for a soon reply.

   <<Less

Re: Sending pdf attachment from binary input stream

Posted By:   Lunkwill_Vroomfondel  
Posted On:   Sunday, May 12, 2002 04:01 PM


File pdfFile=new File("file.pdf");

Properties props=System.getProperties();
props.put("mail.smtp.host","localhost");
Session s=Session.getInstance(props,null);

Address to=new InternetAddress("mailto@haha");


Address from=new InternetAddress("mailfrom@huhu");

MimeMessage msg=new MimeMessage(s);
msg.addRecipient(Message.RecipientType.TO,to);
msg.setFrom(from);
msg.setSubject("test");

Multipart mp=new MimeMultipart();
msg.setContent(mp);

BodyPart bp1=new MimeBodyPart();

bp1.setText("fike attached");


mp.addBodyPart(bp1);

BodyPart att=new MimeBodyPart();
DataSource ds=new FileDataSource(pdfFile);
att.setDataHandler(new DataHandler(ds));
att.setFileName("test.pdf");

mp.addBodyPart(att);

Transport.send(msg);
About | Sitemap | Contact