使用IMAP计算Gmail中的电子邮件数量

9

请问如何使用IMAP或其他方式从Gmail获取收件箱中未读邮件的数量,并在C# WinForms标签中显示?

我尝试使用Atom订阅,但始终无法成功。

以下是我希望实现的效果,如果有帮助:

Inbox(1)


请将答案从问题中移除,并将其作为实际答案发布。谢谢! - Timwi
2个回答

9

已解决

以下是我使用ImapX组件的代码:

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

        result = client.Connection();
        if (result)
            MessageBox.Show("Connection Established");

        result = client.LogIn(textBox1.Text, textBox2.Text);
        if (result)
        {
            MessageBox.Show("Logged in");
            ImapX.FolderCollection folders = client.Folders;
            ImapX.MessageCollection messages = client.Folders["INBOX"].Search("UNSEEN", true); //true - means all message parts will be received from server

            int unread = messages.Count;
            string unseen = unread.ToString();
            button1.Text = unseen;
        }

我只需要将int转换为字符串并在按钮中显示该字符串(不可见)。感谢quantumSoup指引我朝正确的方向前进。


3
如果上面的链接无法使用,可以尝试从http://imapx.codeplex.com使用新版本。 - Evereq

6
您可能希望查找所有标记为未读的邮件。
Imap imap = new Imap();
/* connect, login, etc. */
imap.Connect(...);
/* fill login and select folder code */

List<long> unseenList = imap.SearchFlag(Flag.Unseen);

// now you can get the count from unseeList
int unread = unseenList.Count;

你能再详细解释一下吗?我完全是编程新手。 - Shane121
感谢您的帮助,非常有效。那么我该如何在标签中显示它呢? - Shane121
如果我错了,请纠正我:您需要引用 Mail.dll: http://www.lesnikowski.com/mail/ - Pawel Lesnikowski
2
请问您从哪里获取了这个Imap类?是否有可用于Imap的.NET库? - Pritam Karmakar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接