Wednesday, 12 December 2012

How to Read Gmail Unread messages using C#

            
                  I am coming with Gmail feature that may be useful although many people don't know and how to access it via simple C# code.

Gmail gives a feature called Atom feed that is xml format file to know the unread messages .

  

                http://mail.google.com/mail/feed/atom/ shows the most recent unread items from your inbox.

              Gmail also offers feeds for your labels:http://mail.google.com/mail/feed/atom/labelname/ .
To know the feature in detials go to this link @ (the unofficial news and tips about Google).

                As a developer you can download the C# example here.
                   All what you need is create instance a simple class called GmailHandler and drop this lines in your project.





There are 2 steps in the procedure

  
  1.      Create the gmail handler class:                GmailHandler gmailFeed = new GmailHandler("WriteHereYourGmailUserName(ex:  babu))", "WriteHereYourGmailPassword(ex: ****)");
  2.    Get the feed:       XmlDocument myXml = gmailFeed.GetGmailAtom();
       

happy coding............


 Thanks to

Given here http://fci-h.blogspot.in/2009/09/reading-atom-feed-of-gmail-inbox-via-c.html

Update:
           Sorry guys I'm not good at content editing.. Forgive me if any mistakes found.

For this we need to call the specific atom url  with the authentication.
     
       What ahmed is trying is make this as class library and make it more object oriented. If you don't care about such things go below




            StringBuilder sBuilder = new StringBuilder();
            // create a request for gmail atom url
            WebRequest webRequest = WebRequest.Create( "https://mail.google.com/mail/feed/atom");

            // make the request pre authenticated
            webRequest.PreAuthenticate = true;

            // Create the credentials and attach it to the request
            System.Net.NetworkCredential credentials = new NetworkCredential("gmail username", "gmail password");
            webRequest.Credentials = credentials;

           //Pass the request and get the response          
            WebResponse webResponse = webRequest.GetResponse();
            Stream stream = webResponse.GetResponseStream();
           
          // Get the response stream and read it to a string builder
            while ((byteCount = stream.Read(buffer, 0, buffer.Length)) > 0)
                sBuilder.Append(System.Text.Encoding.ASCII.GetString(buffer, 0, byteCount));

           // Then convert the string to xml
            feedXml = new XmlDocument();
            feedxml.Load(sBuilder.ToString());


        This method only returns the unread gmail messages only.. You can make it to some sort of notifications to your app....


   Thanks....

6 comments:

  1. it is not give full summary.... pls help me ..email id : santhapandian83@gmail.com

    ReplyDelete
    Replies
    1. Check the update man.... If still not getting idea reply me...

      Delete
  2. Dear Sundar,

    Thank you so much. Very helpful

    I have one question: Once i getting UNREAD mails using c# then how to change UNREAD to READ mails. because every minutes i want to check unread mails. if its possible please let me know the process..

    ReplyDelete
    Replies
    1. The atom feed just displays the unread mail subject andfew things alone. It's not the inbox. I think there is no way to mark unread messages to read.

      Any way i'll try any other client library are available to manipulate gmail features. if find let u know through another post.

      If you want to show the update if new mail comes. you can check the mail count of the atom feed. If it increases fetch the difference and filter the xml by showing the list.

      For ex: If already it has 4 unread mails and 2 comes. u get the count as 6 which the previous one is 4. Then filter the first 2 mails and show them to the user.. I think this is a good workaround

      Delete
    2. Thank you for your replies sundar..

      I will work on it. And if its you have any idea please share with me

      Delete
  3. This code is not working.
    You waste visitor's time.
    You have to be honest.
    Not good!!!!!!!

    ReplyDelete