zoukankan      html  css  js  c++  java
  • ExchangeServeice获取在线outlook邮箱中的未读邮件

    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;
            }
            
        }
    }
  • 相关阅读:
    逗号表达式
    Windows UninstallTool(右键卸载工具) 方便、快捷卸载电脑中的软件
    获取 Python 模块的路径
    Py2exe 打包后图标不显示[转载]
    获取系统文件关联图标
    py2exe 打包的exe,添加管理员权限
    获取注册表某键下的所有子键
    [已解决]Python脚本运行出错:libs\chardet\universaldetector.py:90: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode
    Git使用
    SQL Server获取指定行(如第二行)的数据
  • 原文地址:https://www.cnblogs.com/tomz/p/3583151.html
Copyright © 2011-2022 走看看