How to Access SMTP & IMAP Server using OAuth inside .NET Apps

This technical tip explains how .NET developers can Access SMTP & IMAP Server using OAuth inside .NET Applications. Aspose.Email for .NET can be used to access SMTP and IMAP servers. OAuth support for Google mail is implemented only for version 2.0 as OAuth 1.0 has been officially deprecated as of April 20, 2012. At this moment, Google supports OAuth only for the Google Apps Platform and public mail accounts can't be used with the OAuth mechanism. When a new Google Apps account is created, a new "Installed Application" should be registered for this account for testing in the APIs Console. Free trial accounts can be created, provided by Google Apps for Business.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//your code here...//[C# Code Sample]
static void Main(string[] args)
    {
        // The authorizationCode should be replaced with your value.
        // To get authorizationCode use the URL bellow:
        // https://accounts.google.com/o/oauth2/auth?redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=929645059575.apps.googleuser
content.com&scope=https%3A%2F%2Fmail.google.com%2F
        string authorizationCode = "4/zx4_I4ZhhUdmgLdsejpjeMkwAAMs.kk7o1Qx9U28VOl05ti8ZT3Y1uIlidQI";  // authorizationCode should be replaced with 
new value !!!!!!!
        string accessToken = GetAccessToken(authorizationCode);
        AccessSMTPServer(accessToken);
        AccessIMAPServer(accessToken);
    }
    static void AccessSMTPServer(string accessToken)
    {
        MailMessage message = new MailMessage(
            "user1@testaccount1913.narod2.ru",
            "user1@testaccount1913.narod2.ru",
            "NETWORKNET-33499 - " + Guid.NewGuid().ToString(),
            "Access to SMTP servers using OAuth");
        using (SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "user1@testaccount1913.narod2.ru", accessToken, true))
        {
            client.Timeout = 400000;
            client.SecurityMode = SmtpSslSecurityMode.Implicit;
            client.EnableSsl = true;
            client.Send(message);
        }
    }
    static void AccessIMAPServer(string accessToken)
    {
        using (ImapClient client = new ImapClient("imap.gmail.com", 993, "user1@testaccount1913.narod2.ru", accessToken, true))
        {
            client.EnableSsl = true;
            client.SecurityMode = ImapSslSecurityMode.Implicit;
            client.Connect();
            client.SelectFolder("Inbox");
            ImapMessageInfoCollection messageInfoCol = client.ListMessages();
        }
    }
    internal static string GetAccessToken(string authorizationCode)
    {
        string actionUrl = "https://accounts.google.com/o/oauth2/token";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(actionUrl);
X

Url: http://www.aspose.com/.net/email-component.aspx

Language: C# | User: Sheraz Khan | Created: Jul 15, 2015 | Tags: Access SMTP Server using OAuth Access IMAP Server using OAuth .NET Email component Fetch Messages from IMAP Server Fetch Messages from SMTP Server Connect to an Exchange using IMAP