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.to...@gmail.com", "Nitin Tomer"));
                msg.setSubject("Test mail to n.to...@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.to...@gmail.com", "Nitin Tomer"));
                msg.setSubject("Test mail to n.to...@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

-- 
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-appengine@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.

Reply via email to