Site Map Contact Us Home
E-mail Newsletter
Subscribe to get informed about
Clever Components news.

Your Name:
Your Email:
 
SUBSCRIBE
 
Previous Newsletters
 




Products Articles Downloads Order Support
Customer Portal      

Sending and Receiving Emails using GMail in C#

OAuth, SMPT, IMAP using GMail

Abstract

This article describes how to send and receive Email using GMail in your .NET application.

Besides the standard way of managing your GMail account via the Google API interface, Google provides the ability to communicate with Email service via the standard SMPT and IMAP4 Internet protocols. There is a condition: both protocols should utilize the OAUTH authorization method when logging in to the service.

The Clever Internet Suite represents four special components that do all the work: InetSuite.Smtp, InetSuite.Imap4, InetSuite.MailMessage, and InetSuite.OAuth.

Let us look on how it works.

 

Sending Email in just few lines of source code

First, we need to compose the Email message. The InetSuite.MailMessage component provides a set of overloaded BuildMessage methods, by using of which you can compose a Text-plain message, HTML-formatted letter, include file attachments, and embed images. Please refer the Build E-Mail Message article to learn more about this function.

// [C#]
mailMessage1.BuildMessage(memBody.Text, "");

mailMessage1.From.FullAddress = edtFrom.Text;
mailMessage1.ToList.EmailAddresses = edtTo.Text;
mailMessage1.Subject = edtSubject.Text;

Next, we log in to Google Account and obtain the authorization token by using of the InetSuite.OAuth component. The following information should be specified: authorization URL, token URL, redirection URL, Google Client ID, Client Secret, and Score. All this information can be found within your Google Account settings after registering your application. The sample code in this article uses the test application. We registered it for this article and it can be used for testing and evaluating the provided examples. You need to register your own application for working with GMail. Please refer OAuth 2.0 for Mobile & Desktop Apps Google documentation for more details about registering the application and obtaining the client ID.

// [C#]
oAuth1.AuthUrl = "https://accounts.google.com/o/oauth2/auth";
oAuth1.TokenUrl = "https://accounts.google.com/o/oauth2/token";
oAuth1.RedirectUrl = "http://localhost";
oAuth1.ClientID = "421475025220-6khpgoldbdsi60fegvjdqk2bk4v19ss2.apps.googleusercontent.com";
oAuth1.ClientSecret = "_4HJyAVUmH_iVrPB8pOJXjR1";
oAuth1.Scope = "https://mail.google.com/";

var authorization = oAuth1.GetAuthorization();

By default, the InetSuite.OAuth component runs a single-threaded local web server for accepting the authorization token from Google service. When calling to the OAuth.GetAuthorization() method, an external web browser is opened and Google Accounts form is displayed.

 

After confirming the request, you will be redirected to the local web page that is generated by the InetSuite.OAuth component. This is standard way to obtain the authorization token from the OAuth service. The component also supports the EnterCodeForm and WebApplication methods for specifying the authorization code within the WinForms input box and for authorizing in your ASP.NET application correspondingly.

 

The contents of this page can be customized by using of the SuccessHtmlResponse property. If the authorization fails, the FailedHtmlResponse value is displayed in the external browser.

Finally, you need to substitute the composed Email message and send it with the InetSuite.Smtp component.

// [C#]
smtp1.Server = "smtp.gmail.com";
smtp1.Port = 587;
smtp1.UseTls = ClientTlsMode.Explicit;

smtp1.UserName = edtFrom.Text;

smtp1.Authorization = oAuth1.GetAuthorization();

smtp1.Open();
smtp1.Send(mailMessage1);
smtp1.Close();

 

The example sends the Email using GMail via the SMTP protocol.

 

Managing the GMail folders and Emails

The InetSuite.Imap4 component is used for managing folders and reading Emails in your GMail account. The authorization process is the same as for the SMTP component.

// [C#]
imap.Server = "imap.gmail.com";
imap.Port = 993;
imap.UseTls = ClientTlsMode.Implicit;

imap.UserName = edtUser.Text;

imap.Authorization = oAuth.GetAuthorization();

imap.Open();

var mailboxes = imap.GetMailBoxes();

for (int i = 0; i < mailboxes.Length; i++) {
   var item = mailboxes[i].ToString();
   tvFolders.Items.Add(item);
}

// [C#]
for (int i = 1; i <= imap.CurrentMailBox.ExistsMessages; i++) {
   imap.RetrieveHeader(i, mailMessage);

   ListViewItem item = lvMessages.Items.Add(mailMessage.Subject);
   item.SubItems.Add(mailMessage.From.FullAddress);
   item.SubItems.Add(mailMessage.Date.ToString());
   item.SubItems.Add(imap.GetMessageSize(i).ToString());
   item.SubItems.Add(i.ToString());
}

// [C#]
imap.RetrieveMessage(Convert.ToInt32(lvMessages.SelectedItems[0].SubItems[4].Text), mailMessage);

edtFrom.Text = mailMessage.From.FullAddress;
edtSubject.Text = mailMessage.Subject;
memBody.Lines = mailMessage.MessageText;

 

This example lists the GMail mailbox folders and shows the Emails within the selected folder via the IMAP4 protocol.

 

Downloads

Download Clever Internet Suite

Download Sending Emails using GMail in C# source code

Download Reading Emails using GMail in C# source code

Sergey Shirokov
Clever Components team
www.clevercomponents.com

    Copyright © 2000-2024