Sending and Receiving Emails using GMail in C# |
||
Sending Email in just few lines of source codeFirst, 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.
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.
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.
The example sends the Email using GMail via the SMTP protocol.
Managing the GMail folders and EmailsThe 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.
This example lists the GMail mailbox folders and shows the Emails within the selected folder via the IMAP4 protocol.
DownloadsDownload Clever Internet Suite Download Sending Emails using GMail in C# source code Download Reading Emails using GMail in C# source code
Sergey Shirokov
|