Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, November 7, 2010

How to Send Email with Attachments Using Google App Engine

The cool thing about Google App Engine is that you can send e-mails from there for free (well, up to 2000 per day). You can even send attachments with it!

How do you do that? Here is a code snippet. Note that two cases are dealt here, one when there are attachments (so a multi part message is created) and the simplest case where there no attachments:


private static String SendEmail(String user, String to,
    String cc, String subject, String body, String filename,
    String mimeType, byte[] rawData)
{
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    MimeMessage message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(user));
        message.addRecipient(Message.RecipientType.TO,
                             new InternetAddress(to));
        if (cc != null && !cc.trim().isEmpty()) {
              message.addRecipient(Message.RecipientType.CC,
                             new InternetAddress(cc));                    
        }
        message.setSubject(subject, "UTF-8");

        if (rawData != null && filename != null &&
           !filename.isEmpty()) {
             if (mimeType == null || mimeType.isEmpty()) {
                 mimeType = GetMimeType(filename);
             }

             Multipart mp = new MimeMultipart();
             MimeBodyPart htmlPart = new MimeBodyPart();

             htmlPart.setContent(body, "text/html");
             mp.addBodyPart(htmlPart);
             ByteArrayDataSource dataSource = new
                 ByteArrayDataSource(rawData, mimeType);
     
             MimeBodyPart attachment = new MimeBodyPart();
             attachment.setFileName(filename);
             attachment.setDataHandler(
                 new DataHandler(dataSource));                                  
             mp.addBodyPart(attachment);

             message.setContent(mp);
         }
         else {
             message.setText(body, "UTF-8");
         }

         Transport.send(message);
     } catch (Exception e) {
          return "Error: " + e.getMessage();
     } 
     return "ok";
}

Tuesday, October 5, 2010

Cloud Warz: Windows Azure vs Google App Engine

Today I gave a presentation comparing two cloud providers, Microsoft Windows Azure and Google App Engine: "A practical introduction to two of the most popular cloud vendors and unbiased comparison".

Check out the presentation here:
http://www.nanogmail.com/WindowAzureGoogleAppEngineComparison.pdf
The source code for the Windows Azure project is under which can be used as your "hello world" with data access is under:
and for the Google App Engine is under

The scariest thing perceived about Azure is that you have to give your credit card during registration.
Another big inconvenience is that it is really expensive and as an introductory offer you get only one small virtual instance. So, as concluded in the presentation, probably some 90% will prefer Google App Engine unless Microsoft does some drastic steps in changing its model.