// -------------------------------------------------------------------------------------------------------------------- //// Respect the work. // //// Defines the EmailChecker type. // // -------------------------------------------------------------------------------------------------------------------- namespace CSharpLearning { using System; using System.Linq; using Microsoft.Exchange.WebServices.Data; ////// The email checker class. /// internal class EmailChecker { ////// Check emails with the specified subject. Return the first mail item if found; null if not found. /// /// /// The email address. /// /// /// The email subject. /// ////// The public static MailItem Check(string emailAddress, string emailSubject) { var exchangeService = new ExchangeService(ExchangeVersion.Exchange2010); exchangeService.AutodiscoverUrl(emailAddress); var findItemsResults = exchangeService.FindItems(WellKnownFolderName.Inbox, new ItemView(128)); var itemResponses = exchangeService.BindToItems( findItemsResults.Select(item => item.Id), new PropertySet( BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients, EmailMessageSchema.CcRecipients)); return itemResponses.Select(item => new MailItem { Time = item.Item.DateTimeReceived, From = ((EmailAddress)item.Item[EmailMessageSchema.From]).Address, Subject = item.Item.Subject, To = ((EmailAddressCollection)item.Item[EmailMessageSchema.ToRecipients]).Select(recipient => recipient.Address).ToArray(), CC = ((EmailAddressCollection)item.Item[EmailMessageSchema.CcRecipients]).Select(recipient => recipient.Address).ToArray(), Body = item.Item.Body.ToString() }).ToArray().FirstOrDefault(item => item.Subject == emailSubject); } ///. /// /// The mail item class. /// internal class MailItem { ////// Gets or sets the mail received time. /// public DateTime Time { get; set; } ////// Gets or sets the mail from. /// public string From { get; set; } ////// Gets or sets the mail subject. /// public string Subject { get; set; } ////// Gets or sets the mail to. /// public string[] To { get; set; } ////// Gets or sets the mail cc. /// public string[] CC { get; set; } ////// Gets or sets the mail body. /// public string Body { get; set; } } } }