有没有办法在asp.net c#中使用OAUTH2身份验证通过IMAP访问Gmail的邮件?
使用google 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
答案 0 :(得分:-1)
使用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);
}