How to attach a PDF file in email

98 views
Skip to first unread message

newdeveloper

unread,
Dec 15, 2011, 6:31:03 PM12/15/11
to Google App Engine
I use the following code to attach a PDF file in email. However the
PDF content (PDF source code) is integrated into email body instead of
in a PDF attachment file . Could anyone help to figure out how to
attach a PDF file in email. Thank you very much.


InputStream is=httpConn.getInputStream();//download PDF file from
internet
ByteArrayDataSource ds = new ByteArrayDataSource(is, "application/
pdf");
MimeBodyPart attachment= new MimeBodyPart();
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
try {
Message msg = new MimeMessage(session);
attachment.setDataHandler(new DataHandler(ds));
attachment.setFileName("Report.pdf");
Multipart mp = new MimeMultipart();
mp.addBodyPart(attachment);
msg.setContent(mp);
msg.setFrom(new InternetAddress("..", ".."));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress("..",
".."));
msg.setSubject("..");
Transport.send(msg);
} catch ..

voscausa

unread,
Dec 18, 2011, 8:49:51 AM12/18/11
to google-a...@googlegroups.com
Maybe this helps. I use a PDF in the blobstore to attach a PDFand the GAE Python mail API. I do not need to create the multipart Mime message message myself.

message.body = 'message with a PDF attachment'       
attach_name = 'your_name'
blobs = blobstore.BlobInfo.gql("WHERE filename = '%s'" %(attach_name))        :
blob_key = blobs[0].key()                  
blob_reader = blobstore.BlobReader(blob_key)
attach_data = blob_reader.read()                # read from the blob
message.attachments=[(attach_name,attach_data)]
Reply all
Reply to author
Forward
0 new messages