I have created a gmail class library that provides some tools for getting gmail atom feeds and for sending emails through gmail using the .Net framework. These tools are not very complicated, but they are simple to use and should save a user some time if they want to integrate an application with gmail.
GmailMessageSending email using the System.Web.Mail namespace is very simple, but there is not a straight forward way of sending email using a secure connection or on different ports, which is required by gmail. Because of these drawbacks I created the GmailMessage object that inherits from the MailMessage object, all you have to do is set up the message object and call its send message.
I also added a couple of static methods that allow you to send a email through your gmail account in as little as one line of code. Below are some examples of it's usage.
//Send a message with one line of code
RC.Gmail.GmailMessage.SendFromGmail("username", "password", "toAddress@gmail.com",
"subject", "message body");
//Send a message with one line of code with a MailMessage object
RC.Gmail.GmailMessage.SendMailMessageFromGmail("username", "password", mailMessageObject);
//User the GmailMessage object to create and send your message
RC.Gmail.GmailMessage gmailMsg = new RC.Gmail.GmailMessage("username", "password");
gmailMsg.To = "RCcode@gmail.com";
gmailMsg.From = "fromAddress@gmail.com";
gmailMsg.Subject = "C# Test Message";
gmailMsg.Body = "Test body";
MailAttachment attachment = new MailAttachment(@"c:\testfile.txt");
gmailMsg.Attachments.Add(attachment);
gmailMsg.Send();
GmailAtomFeed
The GmailAtomFeed class provides a simple object layer for programmatic access to gmails atom feed. In just a couple lines of code the feed will be retreived from gmail and parsed. After that the entries can be accessed through an object layer AtomFeedEntryCollection, plus access to the raw feed and the feeds XmlDocument is also available.
Below are some examples of it's usage.
// Create the object and get the feed
RC.Gmail.GmailAtomFeed gmailFeed = new RC.Gmail.GmailAtomFeed("username", "password");
gmailFeed.GetFeed();
// Access the feeds XmlDocument
XmlDocument myXml = gmailFeed.FeedXml
// Access the raw feed as a string
string feedString = gmailFeed.RawFeed
// Access the feed through the object
string feedTitle = gmailFeed.Title;
string feedTagline = gmailFeed.Message;
DateTime feedModified = gmailFeed.Modified;
//Get the entries
for(int i = 0; i < gmailFeed.FeedEntries.Count; i++)
{
entryAuthorName = gmailFeed.FeedEntries[i].FromName;
entryAuthorEmail = gmailFeed.FeedEntries[i].FromEmail;
entryTitle = gmailFeed.FeedEntries[i].Subject;
entrySummary = gmailFeed.FeedEntries[i].Summary;
entryIssuedDate = gmailFeed.FeedEntries[i].Received;
entryId = gmailFeed.FeedEntries[i].Id;
}
That's about it short and sweet, I hope this saves you time integrating with gmail from your applications.
Download the source code here:
GmailHelper.zip
You can view the documentation here:
html - chm
5 Comments:
Hi
I tried using this. but it fails:
"An unhandled exception of type 'System.Net.WebException' occurred in <>
Additional information: The remote server returned an error: (401) Unauthorized.
What were you trying to use? GmailMessage ot Atom object? It sounds like you had a bad user name/password, but I can not be sure.
Great class,
Do u know how to add a label to the mail?
//Magnus
Great classes.
Sorry for my poor english..
I found one error, its not a bug.. But the timestamps in the gmail xml file (2007-12-23T24:56:42Z), notice the hour bit, it's 24, and not 00.
So if one of the timestamps in the xml file is at 24 DateTime.Parse() will throw an exception...
i had to change the gmail address to
"https://mail.google.com/mail/feed/atom"
otherwise i kept getting a unauthorized error
and did a
DateTime.Parse(Regex.Replace(_feedXml.SelectSingleNode(baseXPath + "issued", nsm).InnerText, @"T24", "T00")));
per the above posters suggestion. hope that helps anyone else.
Post a Comment
Links to this post:
Create a Link
<< Home