程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# - Check email existance in Outlook

C# - Check email existance in Outlook

編輯:C#入門知識

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved