Last active
January 26, 2019 15:47
-
-
Save biapar/e36b59eeb1a9c19a470ba4ee9baad280 to your computer and use it in GitHub Desktop.
Read Email Subject of Office365 Email Box in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Exchange.WebServices.Data; | |
using System; | |
using System.Collections.Generic; | |
namespace ReadEmail365 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2016); | |
service.Credentials = new WebCredentials("username@domain.it", "xyzurjdkls"); | |
service.TraceEnabled = true; | |
service.TraceFlags = TraceFlags.All; | |
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"); | |
//service.PreAuthenticate = true; | |
service.UseDefaultCredentials = false; | |
service.WebProxy = null; | |
//service.AutodiscoverUrl("username@domain.it", 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) | |
{ | |
list.Add(item.Subject.ToString()); | |
} | |
else | |
{ | |
list.Add("test"); | |
} | |
list.Add(item.DateTimeSent.ToString()); | |
Console.WriteLine(item.Subject.ToString()); | |
} | |
//EmailMessage email = new EmailMessage(service); | |
//email.ToRecipients.Add("username@domain.it"); | |
//email.Subject = "HelloWorld"; | |
//email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API"); | |
//email.Send(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
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