0

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

    }
}

}

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Sanktos
  • 51
  • 8

3 Answers3

1

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

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
1

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.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

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.

Sanktos
  • 51
  • 8