gpt4 book ai didi

c# - 使用 C# 从 gmail 获取未读计数

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:58 28 4
gpt4 key购买 nike

我一直在尝试获取 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);
}
}
}
}

错误是:

The selected authentication mechanism is not supported" on line 26

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

最佳答案

来自 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);

也许您启用了双因素身份验证并且需要生成应用程序密码。否则,您将收到一封电子邮件警告,警告有人试图访问您的邮箱,您必须将您的应用程序添加为异常(exception)。作为替代解决方案,您可以尝试自述文件底部的 https://github.com/jstedfast/MailKit 示例代码

关于c# - 使用 C# 从 gmail 获取未读计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33110648/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com