How to read unread mail in outlook using c#

To read unread mail in Outlook using C#, you can use the Microsoft.Office.Interop.Outlook namespace and the MAPIFolder class. Here's an example code snippet that demonstrates how to do this:

using Microsoft.Office.Interop.Outlook;

// Create an instance of the Outlook application
Application outlookApp = new Application();

// Get the MAPIFolder object for the Inbox folder
MAPIFolder inboxFolder = outlookApp.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderInbox);

// Get the unread items in the Inbox folder
Items unreadItems = inboxFolder.Items.Restrict("[Unread] = true");

// Loop through the unread items and print their subject and sender
foreach (MailItem mailItem in unreadItems)
{
    Console.WriteLine($"Subject: {mailItem.Subject}, Sender: {mailItem.SenderName}");
}

This code creates an instance of the Outlook application, gets the MAPIFolder object for the Inbox folder, restricts the items to only include unread items using the Restrict method, and then loops through the unread items and prints their subject and sender.

You can also use the Find method to search for unread items, like this:

Items unreadItems = inboxFolder.Find("[Unread] = true");

This will return a collection of unread items in the Inbox folder.

Note that you need to have the Outlook Object Library (Microsoft.Office.Interop.Outlook.dll) referenced in your project and the using statement at the top of your code file.

Also, you can use MAPIFolder.UnreadMail property to get the unread mail, like this:

Items unreadItems = inboxFolder.UnreadMail;

This will return a collection of unread items in the Inbox folder.

You can also use MAPIFolder.Find method with "[Unread] = true" as the search criteria to get the unread mail.

Items unreadItems = inboxFolder.Find("[Unread] = true");

Please keep in mind that this code is for Outlook 2010 and later versions. For earlier versions, you may need to use a different approach.

Also, you need to have the Outlook client installed on the machine where you are running this code, and the user needs to have Outlook open and logged in for this code to work.

You can also use Microsoft.Exchange.WebServices namespace to read unread mail, it's a more modern and more powerful way to interact with Outlook, but it requires Exchange Server 2010 or later.

using Microsoft.Exchange.WebServices.Data;

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
service.Credentials = new WebCredentials("username", "password");
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

FolderId folderId = new FolderId(WellKnownFolderName.Inbox);
ItemView itemView = new ItemView(100);
FindItemsResults<Item> findResults = service.FindItems(folderId, itemView);

foreach (Item item in findResults.Items)
{
    if (item.IsRead == false)
    {
        Console.WriteLine($"Subject: {item.Subject}, Sender: {item.SenderName}");
    }
}

Please note that you need to replace the username and password with the actual credentials of the user you want to read the mail for.