【问题标题】:Getting unread count from gmail using C#使用 C# 从 gmail 获取未读计数
【发布时间】:2016-01-11 16:11:53
【问题描述】:

我一直在尝试获取 Gmail 中未读电子邮件的计数,但遇到了一些问题。我进行了搜索,发现 ImapX 库应该可以帮助我实现这一目标,但是我在 StackOverFlow 上找到的关于预览问题的代码不起作用。这是我现在的代码:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string username = "my_email@gmail.com";
            string passwd = "my_pass";
            int unread = 0;

            ImapClient client = new ImapClient("imap.gmail.com", 993, true);
            bool result = client.IsConnected;

            if (result)
                Console.WriteLine("Connection Established");

            result = client.Login(username, passwd); // <-- Error here
            if (result)
            {
                Console.WriteLine("Logged in");
                FolderCollection folders = client.Folders;
               // Message messages = client.Folders["INBOX"].Messages;
                foreach (ImapX.Message m in client.Folders["INBOX"].Messages)
                {
                    if (m.Seen == false)
                        unread++;
                }
                Console.WriteLine(unread);
            }
        }
    }
}

错误是:

不支持所选的身份验证机制”第 26 行

这是result = client.Login(username, passwd);

【问题讨论】:

    标签: c# email gmail imapx


    【解决方案1】:

    来自 ImapX 的示例:

    var client = new ImapX.ImapClient("imap.gmail.com", 993, true);
    client.Connection();
    client.LogIn(userName, userPassword);
    var messages = client.Folders["INBOX"].Search("ALL", true);
    

    也许您已启用双因素身份验证,并且您需要生成应用程序密码。否则,您将收到一封电子邮件警告,提示您正在尝试访问您的邮箱,并且您必须将您的应用程序添加为例外。作为替代解决方案,您可以尝试 README 底部的 https://github.com/jstedfast/MailKit 示例代码

    【讨论】:

      【解决方案2】:

      很有可能,gmail 正在寻找您执行 STARTTLS 命令,而 ImapX 似乎不支持该命令。如果您查看对 IMAPX1 CAPABILITY 请求的响应,您可能会看到“LOGINDISABLED”元素,这意味着服务器尚不接受“LOGIN”语句。因此,即使您使用 SSL,服务器(在我的情况下为 Microsoft Exchange)仍在寻找 STARTTLS 命令之前它会让我登录。

      【讨论】:

        【解决方案3】:

        您必须建立连接。

                ImapClient client = new ImapClient("imap.gmail.com", 993, true);
                client.Connect();
                bool result = client.IsConnected;
        

        因此,如果您添加了 client.Connect() 行,我认为它可以解决您的问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-11-13
          • 1970-01-01
          • 2014-04-19
          • 1970-01-01
          • 2011-10-14
          • 2023-03-31
          • 2017-11-14
          • 2023-03-11
          相关资源
          最近更新 更多