OAuth2GetXOAuthKeyStatic Method |
Namespace: MailBeeAssembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax public static string GetXOAuthKeyStatic(
string email,
string accessToken
)
Public Shared Function GetXOAuthKeyStatic (
email As String,
accessToken As String
) As String
Parameters
- email
- Type: SystemString
E-mail address, like user@domain.com. - accessToken
- Type: SystemString
Access token.
Return Value
Type:
StringA string with URI for XOAUTH2 authentication process for IMAP/SMTP.
Exceptions Remarks When using Google API Auth Client library, the only required method of
OAuth2 class is
GetXOAuthKeyStatic(String, String), you don't need
to use other methods of this class. For that purpose, this static (
Shared in Visual Basic) version exists.
Examples Two samples in this topic. You can find more samples and explanations in OAuth 2.0 in Windows and ASP.NET MVC apps guide.
The first sample performs OAUTH2 authentication using Google Service accounts (common method for Google Apps).
You'll need to reference Google API Auth Client library (with NuGet). Note that at least .NET 4.0 is required for this library.
using System;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using MailBee;
using MailBee.ImapMail;
public class Program
{
public static void Main(string[] args)
{
String serviceAccountEmail = "88...-...7j@developer.gserviceaccount.com";
string userEmail = "user@some-googleapps-domain.com";
var certificate = new X509Certificate2(@"C:\Temp\a...0-privatekey.p12",
"secret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
User = userEmail,
Scopes = new[] { "https://mail.google.com/" }
}.FromCertificate(certificate));
if (credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)
{
string imapXOAuthKey = OAuth2.GetXOAuthKeyStatic(userEmail, credential.Token.AccessToken);
Imap imp = new Imap();
imp.Log.Enabled = true;
imp.Log.Filename = @"C:\Temp\log.txt";
imp.Log.Clear();
imp.Connect("imap.gmail.com");
imp.Login(userEmail, imapXOAuthKey, AuthenticationMethods.SaslOAuth2,
AuthenticationOptions.None, null);
imp.SelectFolder("Inbox");
imp.Disconnect();
}
}
}
Imports System.Security.Cryptography.X509Certificates
Imports Google.Apis.Auth.OAuth2
Imports MailBee
Imports MailBee.ImapMail
Public Class Program
Public Shared Sub Main(args As String())
Dim serviceAccountEmail As String = "88...-...7j@developer.gserviceaccount.com"
Dim userEmail As String = "user@some-googleapps-domain.com"
Dim certificate = New X509Certificate2("C:\Temp\a...0-privatekey.p12", _
"secret", X509KeyStorageFlags.Exportable)
Dim credential As New ServiceAccountCredential(
New ServiceAccountCredential.Initializer(serviceAccountEmail) With { _
.User = userEmail, _
.Scopes = New String() {"https://mail.google.com/"} _
}.FromCertificate(certificate))
If credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result Then
Dim imapXOAuthKey As String = OAuth2.GetXOAuthKeyStatic(userEmail, credential.Token.AccessToken)
Dim imp As New Imap()
imp.Log.Enabled = True
imp.Log.Filename = "C:\Temp\log.txt"
imp.Log.Clear()
imp.Connect("imap.gmail.com")
imp.Login(userEmail, imapXOAuthKey, AuthenticationMethods.SaslOAuth2, _
AuthenticationOptions.None, Nothing)
imp.SelectFolder("Inbox")
imp.Disconnect()
End If
End Sub
End Class
The second sample connects to a regular Google account. In its IMAP part, it's similar to the sample in
OAuth2 topic but uses
Google API Auth Client library instead of DotNetOpenAuth.dll. The key advantage is that no user input is required (copy-paste from the browser to the console).
Also, it shows OAuth2 usage with SMTP, not just IMAP.
using System;
using Google.Apis.Auth.OAuth2;
using MailBee;
using MailBee.ImapMail;
using MailBee.SmtpMail;
public class Program
{
public static void Main(string[] args)
{
string userEmail = "john.doe@gmail.com";
UserCredential credential;
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "somewhat like 628470012345.apps.googleusercontent.com",
ClientSecret = "somewhat like 8IbRSofadFQ7ThjClxp8GBa2"
},
new[] { "https://mail.google.com/" },
"user",
System.Threading.CancellationToken.None).Result;
string xoauthKey = OAuth2.GetXOAuthKeyStatic(userEmail, credential.Token.AccessToken);
bool useImap = true;
if (useImap)
{
Imap imp = new Imap();
imp.Log.Enabled = true;
imp.Log.Filename = @"C:\Temp\log.txt";
imp.Log.Clear();
imp.Connect("imap.gmail.com");
imp.Login(userEmail, xoauthKey, AuthenticationMethods.SaslOAuth2,
AuthenticationOptions.None, null);
imp.SelectFolder("Inbox");
imp.Disconnect();
Console.WriteLine("SUCCEEDED");
}
else
{
Smtp mailer = new Smtp();
mailer.SmtpServers.Add("smtp.gmail.com", userEmail, xoauthKey, AuthenticationMethods.SaslOAuth2);
mailer.Log.Enabled = true;
mailer.Log.Filename = @"C:\Temp\log.txt";
mailer.Log.Clear();
mailer.From.Email = userEmail;
mailer.To.Add(userEmail);
mailer.Subject = "empty email to myself";
mailer.Send();
}
}
}
Imports System.Security.Cryptography.X509Certificates
Imports Google.Apis.Auth.OAuth2
Imports MailBee
Imports MailBee.ImapMail
Imports MailBee.SmtpMail
Public Class Program
Public Shared Sub Main(args As String())
Dim userEmail As String = "john.doe@gmail.com"
Dim credential As UserCredential
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets() With { _
.ClientId = "somewhat like 628470012345.apps.googleusercontent.com", _
.ClientSecret = "somewhat like 8IbRSofadFQ7ThjClxp8GBa2" _
}, New String() {"https://mail.google.com/"}, "user", _
System.Threading.CancellationToken.None).Result
Dim xoauthKey As String = OAuth2.GetXOAuthKeyStatic(userEmail, credential.Token.AccessToken)
Dim useImap As Boolean = True
If useImap Then
Dim imp As New Imap()
imp.Log.Enabled = True
imp.Log.Filename = "C:\Temp\log.txt"
imp.Log.Clear()
imp.Connect("imap.gmail.com")
imp.Login(userEmail, xoauthKey, AuthenticationMethods.SaslOAuth2, _
AuthenticationOptions.None, Nothing)
imp.SelectFolder("Inbox")
imp.Disconnect()
Console.WriteLine("SUCCEEDED")
Else
Dim mailer As New Smtp()
mailer.SmtpServers.Add("smtp.gmail.com", userEmail, xoauthKey, _
AuthenticationMethods.SaslOAuth2)
mailer.Log.Enabled = True
mailer.Log.Filename = "C:\Temp\log.txt"
mailer.Log.Clear()
mailer.From.Email = userEmail
mailer.To.Add(userEmail)
mailer.Subject = "empty email to myself"
mailer.Send()
End If
End Sub
End Class
See Also