URGENT -java MAil sending pdf as attachment
843834May 13 2008 — edited May 20 2008have 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