Showing posts with label Cloud. Show all posts
Showing posts with label Cloud. 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";
}

Wednesday, November 3, 2010

How to write a client to access Google App Engine with GWT

This tip is needed when you create a new Google App Engine project using Eclipse and choose the default settings, in particular when you leave Google Web Toolkit box checked (no trick needed if you uncheck it). If you always uncheck Google Web Toolkit you won't need this. You may still find useful the below example on how to access Google App Engine from a C# client.

The problem is that the framework will create for you all the skeletons for accessing the App Engine from the browser. But what if in addition you want to access the App Engine from a custom made client?

Passing regular parameters will not help - they won't be parsed correctly by the Google App Engine framework.
You need a custom parameter passed, that looks like this:

"5|0|6|http://1.latest.myapp.appspot.com/myapp/|SOMEGUID|myapp.clientGreetingService|greetServer|java.lang.String/2004016611|ThisIsAParameterValue|1|2|3|4|1|5|6|";


Here is the client in C# accessing Google App Engine (created from Eclipse with Web Toolkit option): 

WebRequest request = WebRequest.Create(   "http://1.latest.myapp.appspot.com/appname/greet");
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "POST";
request.ContentType = "text/x-gwt-rpc; charset=utf-8";

string content = "5|0|6|http://1.latest.myapp.appspot.com/myapp/|AF76B9CA2759BDA4DBA207D5542054B2|myapp.clientGreetingService|greetServer|java.lang.String/2004016611|ThisIsAParameterValue|1|2|3|4|1|5|6|";

byte[] contentBytes = System.Text.UTF8Encoding.UTF8.GetBytes(content);
request.ContentLength = contentBytes.Length;
using (Stream stream = request.GetRequestStream())
{
    stream.Write(contentBytes, 0, contentBytes.Length);
}

WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);

reader.Close();
response.Close(); 

In a nutshell, what is important is to provide the servlet name (greet server as in the example above) and then parameter types and values. Other values are not important, the Google App Engine parser just needs the number and placement of pipes.

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.