首页 > 其他 > 详细

ExchangeServeice获取在线outlook邮箱中的未读邮件

时间:2014-03-05 21:37:21      阅读:697      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
using Microsoft.Exchange.WebServices.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ExchangeTest
{
    class Program
    {
        static ExchangeService service;
        static string userEmail = "xxxxx@abcd.onmicrosoft.com";
        static string userPassword = "xxxxx";

        static void Main(string[] args)
        {
            Console.WriteLine("Connecting to Exchange Online, please wait...");
            service = new ExchangeService(ExchangeVersion.Exchange2013);
            service.Credentials = new WebCredentials(userEmail, userPassword);
            service.AutodiscoverUrl(userEmail, RedirectionUrlValidationCallback);

            //获取未读邮件数
            int unRead = Folder.Bind(service, WellKnownFolderName.Inbox).UnreadCount;

            GetUnreadEmails();
        }

        /// <summary>
        /// 获取邮件
        /// </summary>
        private static void GetUnreadEmails()
        {
            ItemView view = new ItemView(int.MaxValue);
            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("无标题");
                }
                //list.Add(item.DateTimeSent.ToString());
            }
        }

        /// <summary>
        /// 设置获取什么类型的邮件
        /// </summary>
        private static SearchFilter SetFilter()
        {
            List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
            searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
            SearchFilter s = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection.ToArray());

            //如果要获取所有邮件的话代码改成这样:
            //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;
        }

        /// <summary>
        /// 重定向URL验证回调
        /// </summary>
        static bool RedirectionUrlValidationCallback(String redirectionUrl)
        {
            bool redirectionValidated = false;
            if (redirectionUrl.Equals("https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml"))
                redirectionValidated = true;

            return redirectionValidated;
        }
        
    }
}
bubuko.com,布布扣

ExchangeServeice获取在线outlook邮箱中的未读邮件,布布扣,bubuko.com

ExchangeServeice获取在线outlook邮箱中的未读邮件

原文:http://www.cnblogs.com/tomz/p/3583151.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!