Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Read Email Subject of Office365 Email Box in C#
using Microsoft.Exchange.WebServices.Data;
using System;
using System.Collections.Generic;
namespace ReadEmail365
class Program
static void Main(string[] args)
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2016);
service.Credentials = new WebCredentials("", "xyzurjdkls");
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.Url = new Uri("");
//service.PreAuthenticate = true;
service.UseDefaultCredentials = false;
service.WebProxy = null;
//service.AutodiscoverUrl("", RedirectionUrlValidationCallback);
if (service != null)
ItemView view = new ItemView(30);
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, SetFilter(), view);
List<string> list = new List<string>();
foreach (Item item in findResults.Items)
if (item.Subject != null)
//EmailMessage email = new EmailMessage(service);
//email.Subject = "HelloWorld";
//email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API");
catch (Exception ex)
private static SearchFilter SetFilter()
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true));
SearchFilter s = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, searchFilterCollection.ToArray());
return s;
private static bool RedirectionUrlValidationCallback(string redirectionUrl)
// The default for the validation callback is to reject the URL.
bool result = false;
Uri redirectionUri = new Uri(redirectionUrl);
// Validate the contents of the redirection URL. In this simple validation
// callback, the redirection URL is considered valid if it is using HTTPS
// to encrypt the authentication credentials.
if (redirectionUri.Scheme == "https")
result = true;
return result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment