微软更新后获取邮件内容

        FindItemsResults<Item> findMailResults = null;
            public async System.Threading.Tasks.Task<FindItemsResults<Item>> GetMail()
            {
                try
                {
                    string appId = ConfigurationManager.AppSettings["appId"];
                    string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
                    string tenantId = ConfigurationManager.AppSettings["tenantId"];
                    var cca = ConfidentialClientApplicationBuilder
                    .Create(appId)
                    .WithClientSecret(clientSecret)
                    .WithTenantId(tenantId)
                    .Build();

                    var ewsScopes = new string[] { "https://outlook.office365.com/.default" };

                    try
                    {
                        var authResult = await cca.AcquireTokenForClient(ewsScopes)
                            .ExecuteAsync();

                        // Configure the ExchangeService with the access token
                        var ewsClient = new ExchangeService();
                        ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
                        ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
                        ewsClient.ImpersonatedUserId =
                            new ImpersonatedUserId(ConnectingIdType.SmtpAddress, ConfigurationManager.AppSettings["Email"]);

                        //Include x-anchormailbox header
                        ewsClient.HttpHeaders.Add("X-AnchorMailbox", ConfigurationManager.AppSettings["Email"]);

                        // Make an EWS call
                        var folders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(10));
                        foreach (var folder in folders)
                        {
                            Console.WriteLine($"Folder: {folder.DisplayName}");
                        }
                        ItemView view = new ItemView(50);
                        view.OrderBy.Add(ItemSchema.DateTimeReceived, Microsoft.Exchange.WebServices.Data.SortDirection.Descending);


                        Folder inbox = Folder.Bind(ewsClient, WellKnownFolderName.Inbox);//Inbox文件夹,不包括子文件夹

                        PropertySet detailedPropertySet = new PropertySet(BasePropertySet.FirstClassProperties, AppointmentSchema.Body);

                        findMailResults = ewsClient.FindItems(WellKnownFolderName.Inbox, view);
                        if (findMailResults != null && findMailResults.Items.Count != 0)
                        {
                            ewsClient.LoadPropertiesForItems(from Item item in findMailResults select item, detailedPropertySet);
                        }
                    }
                    catch (MsalException ex)
                    {
                        Console.WriteLine($"Error acquiring access token: {ex}");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error: {ex}");
                    }

                    if (System.Diagnostics.Debugger.IsAttached)
                    {
                        Console.WriteLine("Hit any key to exit...");
                    }
                    return findMailResults;
                }
                catch (Exception ex)
                {
                    Common comm = new Common();
                    comm.EmailLog("", ex.Message);
                    throw;
                }
            }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值