Sending mail with attachment from GAE

75 views
Skip to first unread message

Nitin Tomer

unread,
Apr 9, 2011, 9:32:12 AM4/9/11
to Google App Engine
Hi,

I am trying to send an email from GAE. To send a text mail without
attachment, I used the following code and it worked like a charm -

String msgBody = "This is another dummy message.";

try
{
Message msg = new MimeMessage(session);
msg.setFrom(new
InternetAddress("ad...@dgobbledygook2.appspotmail.com", "FormHandler
Admin"));
msg.addRecipient(Message.RecipientType.TO,new
InternetAddress("n.t...@gmail.com", "Nitin Tomer"));
msg.setSubject("Test mail to n.t...@gmail.com");
msg.setText(strToPrint);
Transport.send(msg);
}
catch (AddressException e)
{
log.info(e.getMessage());
}
catch (MessagingException e) {
log.info(e.getMessage());
}

Now I want to send a mail with attachment. I copied a PDF file into
the WAR directory of my project and deployed the project. The PDF file
is there at the GAE now, I can access it using the URL. But when I use
the following code to send the mail with attachment, it's not working.
There is no exception/error, but nothing is happeining -

String msgBody = "This is just a dummy message.";

try
{
Message msg = new MimeMessage(session);
msg.setFrom(new
InternetAddress("ad...@dgobbledygook2.appspotmail.com", "FormHandler
Admin"));
msg.addRecipient(Message.RecipientType.TO,new
InternetAddress("n.t...@gmail.com", "Nitin Tomer"));
msg.setSubject("Test mail to n.t...@gmail.com");
//msg.setText(msgBody);

Multipart mp = new MimeMultipart();

MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(msgBody, "text/html");
mp.addBodyPart(htmlPart);

// Read file

File file = new File("TestForm.pdf");
byte fileContent[] = null;

try
{
FileInputStream fin = new FileInputStream(file);

/*
* Create byte array large enough to hold the content of
the file.
* Use File.length to determine size of the file in
bytes.
*/

fileContent = new byte[(int)file.length()];

/*
* To read content of the file in byte array, use
* int read(byte[] byteArray) method of java
FileInputStream class.
*
*/

fin.read(fileContent);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}

MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName("manual.pdf");
attachment.setContent(fileContent, "application/pdf");
mp.addBodyPart(attachment);

msg.setContent(mp);
Transport.send(msg);
}
catch (AddressException e)
{
log.info(e.getMessage());
}
catch (MessagingException e) {
log.info(e.getMessage());
}

Please tell me what I am doing wrong, and how can I make it work.

Thanks and Regards

Nitin

Nitin Tomer

unread,
Apr 11, 2011, 6:10:37 AM4/11/11
to Google App Engine
Hi,

I managed to do it on my own, using this code -

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

// Send a message

String msgBody = "This is just a dummy message.";

try {
MimeMessage message = new MimeMessage(session, req.getInputStream());

Address[] add = message.getFrom();

for(int i=0;i<add.length;i++)
{
log.info("FromAddress: " + add[i].toString());


}


Message msg = new MimeMessage(session);
msg.setFrom(new

InternetAddress("ad...@nttodo.appspotmail.com", "NT To Do List
Admin"));
msg.addRecipient(Message.RecipientType.TO,add[0]);
msg.setSubject("Acknowledgement of your mail to
ad...@nttodo.appspotmail.com");
msg.setText(msgBody);


Transport.send(msg);
}
catch (AddressException e)
{
log.info(e.getMessage());
}
catch (MessagingException e) {
log.info(e.getMessage());
}

Regards

Nitin

> --
> You received this message because you are subscribed to the Google Groups "Google App Engine" group.
> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages