OAuth का उपयोग करके मेल सेवाओं तक पहुंच
Aspose.Email में OAuth 2.0 समर्थन जोड़ा गया है और इसे SMTP, POP3, IMAP और EWS सर्वरों तक पहुँचने के लिए उपयोग किया जा सकता है। सामान्यतः, सभी सर्वर जो OAuth 2.0 बेयरर टोकन को समर्थन देते हैं, को Aspose.Email के साथ उपयोग किया जा सकता है, लेकिन हमारे ईमेल क्लाइंट्स को Google मेल सर्वरों और Microsoft Office 365 सर्वरों के साथ परीक्षण किया गया है। सर्वर तक पहुँच … SmtpClient, Pop3Client, ImapClient और EWSClient OAuth के साथ दो तरीकों में लागू किया जा सकता है।
- एक्सेस टोकन को सीधे ईमेल क्लाइंट के कन्स्ट्रक्टर में प्रदान करें। इस मामले में, उपयोगकर्ता को समझना होगा कि एक्सेस टोकन का जीवनकाल सीमित है। जब टोकन समाप्त हो जाता है, तो ईमेल क्लाइंट सर्वर तक पहुँचने के लिए उपयोग नहीं किया जा सकता।
- टोकन प्रोवाइडर के आधार पर एक कस्टम कार्यान्वयन प्रदान करें ITokenProvider इंटरफ़ेस को ईमेल क्लाइंट के कन्स्ट्रक्टर में पास किया जाता है। इस मामले में, क्लाइंट टोकन की समाप्ति समय की जाँच करता है और अनुरोध करता है ITokenProvider जब पिछला एक्सेस टोकन समाप्त हो जाए तो नया एक्सेस टोकन प्राप्त करने के लिए। इस प्रकार, क्लाइंट टोकन को समय-समय पर रीफ़्रेश करता है और अनिश्चितकाल तक सर्वर के साथ काम कर सकता है। अक्सर सेवाएं एक्सेस टोकन को रीफ़्रेश करने का सरल तरीका समर्थन करती हैं। उदाहरण के लिए, गूगल सेवाओं में रीफ़्रेश टोकन का उपयोग या Microsoft पहचान प्लेटफ़ॉर्म में ROPC प्रमाणीकरण प्रवाह को टोकन प्रोवाइडर कार्यान्वयन के लिए उपयोग किया जा सकता है।
उपयुक्त सर्वर पर एक खाता कॉन्फ़िगर करें
निम्नलिखित लेख आपको मेल सेवाओं तक पहुँचने के लिए खातों को कॉन्फ़िगर करने में मदद करेंगे।
- के लिए Office 365
- के लिए Gmail
एक्सेस टोकन से मेल सेवाओं तक पहुँच
निम्नलिखित कोड उदाहरण दिखाते हैं कि एक्सेस टोकन का उपयोग करके मेल सेवाओं से कैसे कनेक्ट किया जाए।
// Connecting to SMTP server
using (SmtpClient client = new SmtpClient(
"smtp.gmail.com",
587,
"user1@gmail.com",
"accessToken",
true,
SecurityOptions.SSLExplicit))
{
}
// Connecting to IMAP server
using (ImapClient client = new ImapClient(
"imap.gmail.com",
993,
"user1@gmail.com",
"accessToken",
true,
SecurityOptions.SSLImplicit))
{
}
// Connecting to POP3 server
using (Pop3Client client = new Pop3Client(
"pop.gmail.com",
995,
"user1@gmail.com",
"accessToken",
true,
SecurityOptions.Auto))
{
}
टोकन प्रोवाइडर्स से मेल सेवाओं तक पहुँच
निम्नलिखित कोड उदाहरण दिखाते हैं कि टोकन प्रोवाइडर का उपयोग करके मेल सेवाओं से कैसे कनेक्ट किया जाए।
ITokenProvider tokenProvider = TokenProvider.Google.GetInstance(
"ClientId",
"ClientSecret",
"RefreshToken");
// Connecting to SMTP server
using (SmtpClient client = new SmtpClient(
"smtp.gmail.com",
587,
"user1@gmail.com",
tokenProvider,
SecurityOptions.SSLExplicit))
{
}
// Connecting to IMAP server
using (ImapClient client = new ImapClient(
"imap.gmail.com",
993,
"user1@gmail.com",
tokenProvider,
SecurityOptions.SSLImplicit))
{
}
// Connecting to POP3 server
using (Pop3Client client = new Pop3Client(
"pop.gmail.com",
995,
"user1@gmail.com",
tokenProvider,
SecurityOptions.Auto))
{
}
Office 365 के लिए कस्टम ITokenProvider का कार्यान्वयन
आप नीचे दिए गए टोकन प्रोवाइडर कार्यान्वयन का उपयोग करके Office 365 मेल सेवाओं तक पहुँच सकते हैं।
using JsonConvert = Newtonsoft.Json.JsonConvert;
using Aspose.Email.Clients;
using Aspose.Email.Common.Utils;
using Aspose.Email.Tests.TestUtils;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
using System.Text;
namespace TestNS
{
/// <summary>
/// Azure resource owner password credential (ROPC) token provider
/// https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth-ropc
/// https://portal.azure.com
/// https://developer.microsoft.com/en-us/graph/graph-explorer/#
/// token parser https://jwt.io
/// </summary>
internal class AzureROPCTokenProvider : ITokenProvider
{
private const string uriFormat = "https://login.microsoftonline.com/{0}/oauth2/v2.0/token";
private const string bodyFormat =
"client_id={0}" +
"&scope={1}" +
"&username={2}" +
"&password={3}" +
"&grant_type={4}";
private readonly string scope;
private const string grant_type = "password";
private readonly object tokenSyncObj = new object();
private OAuthToken token;
private readonly string tenant;
private readonly string clientId;
private readonly string clientSecret;
private readonly string userName;
private readonly string password;
/// <summary>
/// Initializes a new instance of the <see cref="AzureROPCTokenProvider"/> class
/// </summary>
/// <param name="tenant"></param>
/// <param name="clientId"></param>
/// <param name="clientSecret"></param>
/// <param name="scope"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <param name="scopeAr"></param>
public AzureROPCTokenProvider(
string tenant,
string clientId,
string clientSecret,
string userName,
string password,
string[] scopeAr)
{
this.tenant = tenant;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.userName = userName;
this.password = password;
this.scope = string.Join(" ", scopeAr);
}
/// <summary>
/// Gets oAuth access token.
/// </summary>
/// <param name="ignoreExistingToken">
/// If ignoreExistingToken is true, requests new token from a server. Otherwise behaviour is depended on whether token exists or not.
/// If token exists and its expiration date is not expired returns current token, otherwise requests new token from a server.
/// </param>
/// <returns>Returns oAuth access token</returns>
public virtual OAuthToken GetAccessToken(bool ignoreExistingToken)
{
lock (tokenSyncObj)
{
if (this.token != null && !this.token.Expired && !ignoreExistingToken)
return this.token;
token = null;
string uri = string.Format(uriFormat, string.IsNullOrWhiteSpace(tenant) ? "common" : tenant);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
string body = string.Format(bodyFormat,
HttpUtility.UrlEncode(clientId),
HttpUtility.UrlEncode(scope),
HttpUtility.UrlEncode(userName),
HttpUtility.UrlEncode(password),
HttpUtility.UrlEncode(grant_type));
byte[] bytes = Encoding.ASCII.GetBytes(body);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
MemoryStream ms = new MemoryStream(bytes);
using (Stream requestStream = request.GetRequestStream())
requestStream.Write(bytes, 0, bytes.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StringBuilder responseText = new StringBuilder();
bytes = new byte[1024];
int read = 0;
using (Stream stream = response.GetResponseStream())
{
while ((read = stream.Read(bytes, 0, bytes.Length)) > 0)
responseText.Append(Encoding.ASCII.GetString(bytes, 0, read));
}
string jsonString = responseText.ToString();
AzureTokenResponse t = JsonConvert.DeserializeObject<AzureTokenResponse>(jsonString);
token = new OAuthToken(
t.access_token,
TokenType.AccessToken,
DateTime.Now.AddSeconds(t.expires_in));
return token;
}
}
/// <summary>
/// Gets oAuth access token.
/// If token exists and its expiration date is not expired returns current token, otherwise requests new token from a server.
/// </summary>
/// <returns>Returns oAuth access token</returns>
public OAuthToken GetAccessToken()
{
return GetAccessToken(false);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public virtual void Dispose()
{
}
}
/// <summary>
/// A success response contains a JSON OAuth 2.0 response with the following parameters.
/// </summary>
public class AzureTokenResponse
{
/// <summary>
/// The requested access token. The calling web service can use this token to authenticate to the receiving web service.
/// </summary>
public string access_token { get; set; }
/// <summary>
/// Indicates the token type value. The only type that Azure AD supports is Bearer For more information about bearer tokens,
/// see The OAuth 2.0 Authorization Framework: Bearer Token Usage (RFC 6750).
/// </summary>
public string token_type { get; set; }
/// <summary>
/// How long the access token is valid (in seconds).
/// </summary>
public int expires_in { get; set; }
/// <summary>
/// How long the access token is valid (in seconds).
/// </summary>
public int ext_expires_in { get; set; }
/// <summary>
/// The time when the access token expires.
/// The date is represented as the number of seconds from 1970-01-01T00:00:00Z UTC until the expiration time.
/// This value is used to determine the lifetime of cached tokens.
/// </summary>
public int expires_on { get; set; }
/// <summary>
/// The App ID URI of the receiving web service.
/// </summary>
public string resource { get; set; }
/// <summary>
/// If an access token was returned, this parameter lists the scopes the access token is valid for.
/// </summary>
public string scope { get; set; }
/// <summary>
/// Issued if the original scope parameter included the openid scope.
/// </summary>
public string id_token { get; set; }
/// <summary>
/// Issued if the original scope parameter included offline_access.
/// </summary>
public string refresh_token { get; set; }
}
}
अगले कोड उदाहरण दिखाते हैं कि कस्टम टोकन प्रोवाइडर का उपयोग करके Office 365 सेवाओं से कैसे कनेक्ट करें।
ITokenProvider tokenProvider = new AzureROPCTokenProvider(
"Tenant",
"ClientId",
"ClientSecret",
"EMail",
"Password",
scopes);
// Connecting to SMTP server
using (SmtpClient client = new SmtpClient(
"smtp.office365.com",
587,
"Test1@test.onmicrosoft.com",
tokenProvider,
SecurityOptions.SSLExplicit))
{
}
// Connecting to IMAP server
using (ImapClient client = new ImapClient(
"outlook.office365.com",
993,
"Test1@test.onmicrosoft.com",
tokenProvider,
SecurityOptions.SSLImplicit))
{
}
// Connecting to POP3 server
using (Pop3Client client = new Pop3Client(
"outlook.office365.com",
995,
"Test1@test.onmicrosoft.com",
tokenProvider,
SecurityOptions.Auto))
{
}
// Connecting to EWS server
const string mailboxUri = "https://outlook.office365.com/ews/exchange.asmx";
ICredentials credentials = new OAuthNetworkCredential(tokenProvider);
using (IEWSClient ewsClient = EWSClient.GetEWSClient(mailboxUri, credentials))
{
}