C# 如何在收到新邮件时将事件侦听器连接到office 365

C# 如何在收到新邮件时将事件侦听器连接到office 365,c#,.net,email,event-handling,exchangewebservices,C#,.net,Email,Event Handling,Exchangewebservices,我已将邮箱从exchange server迁移到office 365 我已经编写了使用凭据连接到office 365的代码,因此我能够阅读收件箱中的所有电子邮件。请查找以下代码 public async System.Threading.Tasks.Task test() { var pcaOptions = new PublicClientApplicationOptions { ClientId

我已将邮箱从exchange server迁移到office 365

我已经编写了使用凭据连接到office 365的代码,因此我能够阅读收件箱中的所有电子邮件。请查找以下代码

 public async System.Threading.Tasks.Task test()
        {
            var pcaOptions = new PublicClientApplicationOptions
            {
                ClientId = ConfigurationManager.AppSettings["appId"],
                TenantId = ConfigurationManager.AppSettings["tenantId"],
            };

            var pca = PublicClientApplicationBuilder
                .CreateWithApplicationOptions(pcaOptions).Build();

            var ewsScopes = new string[] { "https://outlook.office.com/EWS.AccessAsUser.All" };

            try
            {
                string password = "test";
                SecureString sec_pass = new SecureString();
                Array.ForEach(password.ToArray(), sec_pass.AppendChar);
                sec_pass.MakeReadOnly();
                // Make the interactive token request
                var authResult = await pca.AcquireTokenByUsernamePassword(ewsScopes, "test@demotenant.com", sec_pass).ExecuteAsync();
                //var authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();
                // Configure the ExchangeService with the access token
                var ewsClient = new ExchangeService();
                //ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "test@demotenant.onmicrosoft.com");
                ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
                ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);

                FindItemsResults<Item> result = ewsClient.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
                foreach (Item item in result)
                {
                    EmailMessage message = EmailMessage.Bind(ewsClient, item.Id);
                    String body = message.ConversationTopic;
                    String from = message.From.Address.ToString();

                }
                // Make an EWS call
                var folders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(10));
                foreach (var folder in folders)
                {
                    Console.WriteLine($"Folder: {folder.DisplayName}");
                }
            }
            catch (MsalException ex)
            {
                Console.WriteLine($"Error acquiring access token: {ex.ToString()}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.ToString()}");
            }
        }
public async System.Threading.Tasks.Task test()
{
var pcaooptions=新的PublicClientApplicationOptions
{
ClientId=ConfigurationManager.AppSettings[“appId”],
TenantId=ConfigurationManager.AppSettings[“TenantId”],
};
var pca=PublicClientApplicationBuilder
.CreateWithApplicationOptions(PCAOOptions).Build();
var ewscopes=新字符串[]{”https://outlook.office.com/EWS.AccessAsUser.All" };
尝试
{
字符串password=“test”;
SecureString sec_pass=新SecureString();
ForEach(password.ToArray(),sec_pass.AppendChar);
sec_pass.MakeReadOnly();
//发出交互式令牌请求
var authResult=等待pca.AcquireTokenByUsernamePassword(EWSCOpes,“test@demotenant.com,sec_pass)。ExecuteAsync();
//var authResult=await pca.AcquireTokenInteractive(ewscopes.ExecuteAsync();
//使用访问令牌配置ExchangeService
var ewsClient=新的ExchangeService();
//ewsClient.ImpersonatedUserId=新的ImpersonatedUserId(ConnectingIdType.SmtpAddress,“test@demotenant.onmicrosoft.com");
ewsClient.Url=新Uri(“https://outlook.office365.com/EWS/Exchange.asmx");
ewsClient.Credentials=新的OAuthCredentials(authResult.AccessToken);
FindItemsResults=ewsClient.FindItems(WellKnownFolderName.Inbox,新项目视图(10));
foreach(结果中的项目)
{
EmailMessage=EmailMessage.Bind(ewsClient,item.Id);
字符串体=message.ConversationTopic;
String from=message.from.Address.ToString();
}
//打EWS电话
var folders=ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot,newfolderview(10));
foreach(文件夹中的var文件夹)
{
Console.WriteLine($“文件夹:{Folder.DisplayName}”);
}
}
渔获物(MSAlexex)
{
WriteLine($“获取访问令牌时出错:{ex.ToString()}”);
}
捕获(例外情况除外)
{
WriteLine($”错误:{ex.ToString()}”);
}
}
现在我想添加一个监听器,它可以在收件箱中收到新邮件时运行此代码。
有人能建议我怎么做吗。

EWS流媒体或推送通知是在EWS中实现这一点的一种方法。更好的方法是在Graph API中使用Webooks,而不使用EWS(除非需要编写代码来运行OnPrem)。我建议的另一件事是看看Power Automation(以前的Flow),它还能够在收到新邮件时触发大量操作