当我在收件箱中获取新消息时,如何获取通知(使用IMAP和C#)?
我有一个代码,可以将传入的消息读取到邮件中,但是我想创建一个触发器,以便在向我传达一些消息时,会发生一些操作。例如,您可以简单地创建一个控制台应用程序,并且触发器将是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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议切换到使用图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.
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.
因此,我对这个邮件通知系统进行了自己的研究,并找到了一篇非常好的文章。
这是文章:
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.