Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

URGENT -java MAil sending pdf as attachment

843834May 13 2008 — edited May 20 2008
have a small java program that sends the pdf as an attachment without using phisical pdf file.
but the problem is i am unable to open the pdf that comes as as attachment in mail.
error while opening pdf : " acrobat cannot open file bcos the file type is not supported or the file is curropted(for example it was sent as email attachment and was not properly decoded)"

here is the sample code:

Properties props = System.getProperties();

//Specify the desired SMTP server
props.put("mail.smtp.host", mailHost.trim());
if(mailPort!=null || !mailPort.equalsIgnoreCase(""))
{
props.put("mail.smtp.port", mailPort.trim());
}

// create a new Session object
Session mailsession = Session.getInstance(props, null);

// create a new MimeMessage object (using the Session created above)
Message message = new MimeMessage(mailsession);

try{
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
new InternetAddress[] { new InternetAddress(mailTo) });

message.setSubject(subject);

BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Please find the attacment for password");

//use a MimeMultipart as we need to handle the file attachments
Multipart multipart = new MimeMultipart();


multipart.addBodyPart(messageBodyPart);
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
StringBuffer sb = new StringBuffer(body);
DataSource ds = new ByteArrayDataSource(body.getBytes("iso-8859-1"), "application/pdf");
attachmentBodyPart.setDataHandler(new DataHandler(ds));


attachmentBodyPart.setFileName("attach.pdf");


multipart.addBodyPart(attachmentBodyPart);


message.setContent(multipart);

message.setSentDate(date);

Transport.send(message);


I think, It's "corrupted" because I am sending a text string and not a PDF document.
But how can i convert string to pdf document.
Thanks,
bvnaresh
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 17 2008
Added on May 13 2008
2 comments
1,144 views