Search Wiki:
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.


Sending 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", "", "subject", "message body"); 
   //Send a message with one line of code with a MailMessage object 
   RC.Gmail.GmailMessage.SendMailMessageFromGmail("username", "password", mailMessageObject); 
   //Use the GmailMessage object to create and send your message 
   RC.Gmail.GmailMessage gmailMsg = new RC.Gmail.GmailMessage("username", "password"); 
   gmailMsg.To = ""; 
   gmailMsg.From = ""; 
   gmailMsg.Subject = "C# Test Message"; 
   gmailMsg.Body = "Test body"; 
   MailAttachment attachment = new MailAttachment(@"c:\testfile.txt"); 


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"); 
   // 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.
Last edited Feb 20 2008 at 8:26 AM  by quamtar, version 2
basil wrote  Feb 24 2008 at 1:10 AM  
Cool!!! Thanks

ShinyZhu wrote  Feb 28 2008 at 4:49 AM  
It's cool. good job.

ardavan wrote  Mar 9 2008 at 10:12 AM  
its's good ,thank you ,but how i can download gmail component and include in project for use?

Coppermill wrote  Mar 11 2008 at 6:04 PM  
Great class, saves me writing one :-)

rondwest wrote  Jun 13 2008 at 2:09 AM  
is there any way to get the full body of the email, rather than just a summary?

JaDaar wrote  Jun 30 2008 at 7:31 AM  
Very cool class, thanks a lot!!

locddspkt wrote  Sep 1 2008 at 6:21 PM  
good jog. I try to use so many code, but i cannot send any mails. I don't know why. But with your class. It 's all OK. Thanks a lot

kjelltj wrote  Jan 15 2009 at 8:05 PM  
The mailMessageObject object does not exist. That is the message I get when running this code

melora wrote  Feb 12 2009 at 9:45 PM  
Looks like it could be very handy. I've guessed but haven't had luck yet, so it's useless to me, so far. Please let me know if I can get any information about how to actually install and use it in an application.

king_aggressive wrote  Nov 5 2009 at 1:18 AM  

eylkrn wrote  Dec 11 2009 at 11:37 PM  
The code is outdated . I will post a fix for GmailAtomFeed.cs in the discussions

eylkrn wrote  Dec 11 2009 at 11:38 PM  
btw. Thanks for the code!!

Salinda wrote  Apr 2 2010 at 10:58 AM  
Hay I tried your code. But I got an error saying " The remote server returned an error: (401) Unauthorized". Any of you can help me with this. Thanks a loot for uploading that code.

FaBjE wrote  Apr 3 2010 at 5:49 PM  
To fix the gmail atom feed you need to change the feed URL to:
That solved the (401) Unauthorized problem for me.

CsharpChico wrote  Apr 26 2010 at 4:03 AM  
that is a great article on using gmail to send messages but the one thing that gets my attention is that you must use a pretermined outbound email address also a predetermined attachment file name. What if I want to make it possible to use any gmail account? How about sending a picture or powerpoint presentation as the attachment? I think you will find this code quite interesting
<code> private void button1_Click(object sender, EventArgs e)
button1.Enabled = true;
this.Cursor = Cursors.WaitCursor;

using (MailMessage mailMessage = new MailMessage(new MailAddress(textBox1.Text),
new MailAddress(textBox1.Text)))
mailMessage.Body = textBox5.Text;
mailMessage.Subject = textBox4.Text;

SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential(textBox2.Text, textBox3.Text);
SmtpServer.Port = 587;
SmtpServer.Host = "";
SmtpServer.EnableSsl = true;
mail = new MailMessage();
String[] addr = textBox1.Text.Split(',');
mail.From = new MailAddress(textBox2.Text);
Byte i;
for (i = 0; i < addr.Length; i++)
mail.Subject = textBox4.Text;
mail.Body = textBox5.Text;
if (listBox1.Items.Count != 0)
for (i = 0; i < listBox1.Items.Count; i++)
mail.Attachments.Add(new Attachment(listBox1.Items[i].ToString()));

mail.IsBodyHtml = true;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.ReplyTo = new MailAddress(textBox1.Text);
catch (Exception ex)
MessageBox.Show(ex.Message, "EMail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

this.Cursor = Cursors.Arrow;
button1.Enabled = true;
here is a better alternative for the attachment
<code> private void button2_Click(object sender, EventArgs e)
if (openFileDialog1.ShowDialog() == DialogResult.OK)
Hope this helps some people out!

CsharpChico wrote  Apr 26 2010 at 4:19 AM  
To help make the code mentioned a moment ago easier to understand. textBox1.Text should be the text box that will handle the email address that the message is being sent to. textBox2.Text will be the Gmail account that you are sending the message from(you must include textBox3.Text will be the text box that handles the password (Dont forget to set you password char in properties so the password is not being typed in is not visible)for textBox2.Text(gmail account that is being used to send message with). textBox4.Text will be the text box that represent the subject. textBox5.Text is the text box that will represent the body/message being sent. With textBox5.Text make sure you make it multiline and stretch it out to make a box that will look like a textarea. Finally we get to the attachments. You will add a listBox (NOT LISTVIEW!!) If you use list view it will throw an error when trying to add attachments. Good luck to all of you

Broham wrote  Aug 4 2010 at 5:30 AM  
Is it possible to mark emails retrieved through the GetAtomFeed.GetFeed function as read, so they are not retrieved on the next run?

NARKOZ wrote  Aug 14 2010 at 4:55 AM  
Thanks. Saved me from routine work.

Bond wrote  Oct 10 2010 at 7:36 AM  
Great work! thanks a ton my friend!

nate3711 wrote  Mar 17 2011 at 4:08 AM  
This works on my development pc but does not work when run from my remote server. Running IIS 7 (hosted by godaddy). Any ideas.

FaizanKazi wrote  Mar 30 2011 at 9:17 PM  
Any chance I could get this to work with Windows Forms? It requires System.Web.dll to run, and I couldn't figure out a way to add that to my Windows Forms project.

Wilco20004 wrote  Jun 14 2011 at 8:30 PM  
answer to FaizanKazi's question Use; and; work's 100x better than web.dll :)

Wilco20004 wrote  Jun 14 2011 at 8:31 PM  
Does anyone have code to gather all the computer "specs" for me?

Indraneel wrote  Aug 18 2011 at 9:49 AM  
How do i receive mail from gmail ?

Tory wrote  Jun 7 2012 at 5:32 PM  
Is there any way to get the attachments for an e-mail using the RSS feed?

Page view tracker