Reputation: 55
Ihave string which i need to send as a PDF attachment to mail, my code is as follows
Message message = new MimeMessage(session);
message.setFrom("");
message.setRecipients("")
message.setSubject("Test");
String example = "PDF Content";
byte[] pdf = example.getBytes();
MimeBodyPart attachment = new MimeBodyPart();
DataSource src = new ByteArrayDataSource(pdf, "application/pdf");
Multipart mp1 = new MimeMultipart();
attachment.setDataHandler(new DataHandler(src));
attachment.setFileName("sample.pdf");
mp1.addBodyPart(attachment);
message.setContent(mp1);
Transport.send(message);
I'm getting pdf document as a attachment, but it not loading. please guide me to solve this.
Thanks in advance
Upvotes: 0
Views: 4591
Reputation: 29971
Message message = new MimeMessage(session);
message.setFrom(""); // hopefully you're putting a real value here
message.setRecipients(""); // and here
message.setSubject("Test");
MimeBodyPart attachment = new MimeBodyPart();
Multipart mp1 = new MimeMultipart();
attachment.attachFile("sample.pdf", "application/pdf", "base64");
mp1.addBodyPart(attachment);
message.setContent(mp1);
Transport.send(message);
The JavaMail FAQ has more sample code, and you'll find complete sample applications on the JavaMail web site.
Upvotes: 2