当我在收件箱中获取新消息时,如何获取通知(使用IMAP和C#)?

发布于 2025-01-29 01:29:53 字数 1419 浏览 6 评论 0 原文

我有一个代码,可以将传入的消息读取到邮件中,但是我想创建一个触发器,以便在向我传达一些消息时,会发生一些操作。例如,您可以简单地创建一个控制台应用程序,并且触发器将是console.writeline(“新消息”);

当然,理想情况下,也是要阅读此消息,但是我以后可以自己做。我不知道如何实施通知。

我将添加我工作的代码,但是在这种情况下,它似乎无济于事。我是IMAP的新手。我将感谢任何帮助和建议。

static void GetItems()
{
    ExchangeService service;
    String pattern = "file:///C:/Users/";
    service = new ExchangeService
    {
        Credentials = new WebCredentials("mail", "password")
    };
    FolderView view = new
    FolderView(10);

view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
view.PropertySet.Add(FolderSchema.DisplayName);


service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

SearchFilter searchFilter = new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0);
view.Traversal = FolderTraversal.Deep;
FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.Inbox, searchFilter, view);

foreach (Folder f in findFolderResults)
{
    Console.WriteLine("Handling folder: " + f.DisplayName);

    SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true));
    ItemView view1 = new ItemView(1);

    FindItemsResults<Item> findEmailResults = service.FindItems(f.Id, sf, view1);
    foreach (Item i in findEmailResults)
    {
        Console.WriteLine("Processing email: " + i.Subject);

    }
}

}

I have a code that reads incoming messages to the mail, but I want to create a trigger so that when some message comes to me, some action occurs. For example, you can simply create a console application and the trigger will be Console.WriteLine("New message");

Of course, ideally, it would also be to read this message, but I can already do this myself later. I can't figure out how to implement the notification.

I will add the code on which I worked, but in this case, it does not seem to help. I'm new to Imap. I would be grateful for any help and advice.

static void GetItems()
{
    ExchangeService service;
    String pattern = "file:///C:/Users/";
    service = new ExchangeService
    {
        Credentials = new WebCredentials("mail", "password")
    };
    FolderView view = new
    FolderView(10);

view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
view.PropertySet.Add(FolderSchema.DisplayName);


service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

SearchFilter searchFilter = new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0);
view.Traversal = FolderTraversal.Deep;
FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.Inbox, searchFilter, view);

foreach (Folder f in findFolderResults)
{
    Console.WriteLine("Handling folder: " + f.DisplayName);

    SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true));
    ItemView view1 = new ItemView(1);

    FindItemsResults<Item> findEmailResults = service.FindItems(f.Id, sf, view1);
    foreach (Item i in findEmailResults)
    {
        Console.WriteLine("Processing email: " + i.Subject);

    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追星践月 2025-02-05 01:29:53

我建议切换到使用图API代替EWS,使用Microsoft Graph API获取更改通知

I'd recommend switching to using Graph API instead of EWS, Use the Microsoft Graph API to get change notifications.

执妄 2025-02-05 01:29:53

EWS确实支持通知。它允许在特定情况下进行推动,拉和流通知。

“ Exchange”中的通知订阅,邮箱事件和EWS

EWS does support notifications. It allows for push, pull, and streaming notifications, whatever makes sense in your particular case.

Start at Notification subscriptions, mailbox events, and EWS in Exchange.

猫瑾少女 2025-02-05 01:29:53

因此,我对这个邮件通知系统进行了自己的研究,并找到了一篇非常好的文章。

这是文章:
mailkit:从IMAP服务器中获取新邮件通知

邮件通知代码:

一切都很好,我已经使用了。您只需要下载一些库即可。

So, I made my own research about this mail-notification system and found a really good article with the code.

This is the article:
MailKit : Obtaining new mail notification from IMAP server

This is the code for mail-notification:
https://github.com/jstedfast/MailKit/blob/6c813d02617edc3e3de5481a413b1e2fb43bfe8c/samples/ImapIdle/ImapIdle/Program.cs

Everything is working fine, I already used this. You just need to download some libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文