【问题标题】:How can Gmail's Mail be accessed by IMAP using OAuth2 authentication or retrieve using Gmail API in asp.net c#?如何使用 OAuth2 身份验证通过 IMAP 访问 Gmail 的邮件或在 asp.net c# 中使用 Gmail API 进行检索?
【发布时间】:2016-05-03 12:44:42
【问题描述】:

在 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?
 }

或者有没有像 Limilab 的 Mail.dll 这样使用 OAUTH2 登录的免费 IMAP 客户端

【问题讨论】:

  • 我不知道,我支付了 Limilabs 组件的费用,它就像一个冠军。当您考虑它会节省您的时间时,它真的很便宜。自 2008 年以来,我一直在使用他们的组件。
  • 您是否实施了来自此link 的示例?
  • 抱歉,我发烧了,回复晚了。是的,我是从那个链接实现的,但电子邮件的 html 丢失了。

标签: c# asp.net gmail imap oauth2


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 2012-01-29
    • 2016-04-18
    • 1970-01-01
    • 2015-06-25
    • 2020-06-24
    相关资源
    最近更新 更多