[英]How can Gmail's Mail be accessed by IMAP using OAuth2 authentication or retrieve using Gmail API in asp.net c#?
有什么方法可以通過ASP.NET C#中的OAUTH2身份驗證通過IMAP訪問Gmail的郵件?
使用谷歌API,我能夠獲得MessageID。 但無法檢索該消息的詳細信息:
var gmailservice = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = appName,
});
List<Message> objList = ListMessages(gmailservice, "me", AnyFromEmailAddress);
foreach (Message objM in objList)
{
// I can retrieve objM.Id but how to get message detail?
}
還是有任何免費的IMAP客戶端使用OAUTH2進行登錄,例如Limilab的Mail.dll
使用NuGet的MailKit和Google的OAuth2框架,您可以這樣做:
using (var client = new ImapClient ()) {
client.Connect ("imap.gmail.com", 993, true);
var certificate = new X509Certificate2 (@"C:\path\to\certificate.p12", "password", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential (new ServiceAccountCredential.Initializer ("your-developer-id@developer.gserviceaccount.com") {
// Note: other scopes can be found here: https://developers.google.com/gmail/api/auth/scopes
Scopes = new[] { "https://mail.google.com/" },
User = "user@gmail.com"
}.FromCertificate (certificate));
// Note: result will be true if the access token was received successfully
bool result = await credential.RequestAccessTokenAsync (cancel.Token);
// use the access token as the password string
client.Authenticate ("user@gmail.com", credential.Token.AccessToken);
// ...
client.Disconnect (true);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.