Home > Software design >  How to get notification when I'm getting a new message in my inbox(using IMAP and C#)?
How to get notification when I'm getting a new message in my inbox(using IMAP and C#)?

Time:05-16

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

    }
}

}

CodePudding user response:

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

CodePudding user response:

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.

  • Related