C Check email existance in Outlook
阿新 • • 發佈:2018-11-07
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
// -------------------------------------------------------------------------------------------------------------------- // <copyright file="EmailChecker.cs" company="Chimomo's Company">// Respect the work.// </copyright>// <summary>// Defines the EmailChecker type.// </summary>// -------------------------------------------------------------------------------------------------------------------- namespace CSharpLearning{ using System; using System.Linq; using Microsoft.Exchange.WebServices.Data; /// <summary> /// The email checker class. /// </summary> internal class EmailChecker { /// <summary> /// Check emails with the specified subject. Return the first mail item if found; null if not found. /// </summary> /// <param name="emailAddress"> /// The email address. /// </param> /// <param name="emailSubject"> /// The email subject. /// </param> /// <returns> /// The <see cref="MailItem"/>. /// </returns> 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); } /// <summary> /// The mail item class. /// </summary> internal class MailItem { /// <summary> /// Gets or sets the mail received time. /// </summary> public DateTime Time { get; set; } /// <summary> /// Gets or sets the mail from. /// </summary> public string From { get; set; } /// <summary> /// Gets or sets the mail subject. /// </summary> public string Subject { get; set; } /// <summary> /// Gets or sets the mail to. /// </summary> public string[] To { get; set; } /// <summary> /// Gets or sets the mail cc. /// </summary> public string[] CC { get; set; } /// <summary> /// Gets or sets the mail body. /// </summary> public string Body { get; set; } } }}