C #-Check email existance in Outlook

Source: Internet
Author: User

// --------------------------------------------------------------------------------------------------------------------// 
 
  //   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; }        }    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.