目前使用 Outlook v2.0 ,这对我来说真的很新,我遇到了一个无法预料的问题。目前我有我的身份验证,并且能够毫无问题地创建我的 Outlook 客户端。但是,在对当前有 250 封电子邮件的邮箱进行测试运行后,我看到这个 api 只检索到 10 封。看看是否有人在使用 Outlook api v 2.0 时遇到了这个问题
代码
private static async Task<OutlookServicesClient> CreateOutlookClientAsync(AuthenticationContext authenticationContext)
{
OutlookServicesClient outlookClient = null;
try
{
outlookClient = new OutlookServicesClient(
new Uri(CrmPrototype.Helpers.AuthHelper.OutlookAPIEndpoint),
async () =>
await GetTokenHelperAsync(authenticationContext, CrmPrototype.Helpers.AuthHelper.OutlookAuthenticationEndpoint)
);
return outlookClient;
}
catch (Exception ex)
{
// TODO Log
return outlookClient;
}
}
private static async Task<GraphServiceClient> CreateGraphClientAsync(AuthenticationContext authenticationContext)
{
GraphServiceClient graphClient = null;
try
{
graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
string accessToken = await GetTokenHelperAsync(authenticationContext, CrmPrototype.Helpers.AuthHelper.OutlookAuthenticationEndpoint);
// Append the access token to the request.
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
}));
return graphClient;
}
catch (Exception ex)
{
// TODO Log
return graphClient;
}
}
private static async Task<string> GetTokenHelperAsync(AuthenticationContext context, string resourceId)
{
string accessToken = null;
try
{
X509Certificate2 certificate = new X509Certificate2(CrmPrototype.Helpers.AuthHelper.devCertPath, CrmPrototype.Helpers.AuthHelper.devCertKey, X509KeyStorageFlags.MachineKeySet);
ClientAssertionCertificate clientAssertionCert = new ClientAssertionCertificate(CrmPrototype.Helpers.AuthHelper.devClientId, certificate);
AuthenticationResult result = null;
result = await context.AcquireTokenAsync(resourceId, clientAssertionCert);
accessToken = result.AccessToken;
return accessToken;
}
catch (Exception ex)
{
// TODO Log
return accessToken;
}
}
public static async Task<IMessageCollection> GetEmails(string emailBox)
{
IMessageCollection emails = null;
AuthenticationContext authenticationContext = new AuthenticationContext(CrmPrototype.Helpers.AuthHelper.devTenant);
try
{
var outlookClient = await CreateOutlookClientAsync(authenticationContext);
var mail_Box = await outlookClient.Users[emailBox].MailFolders["Inbox"].Messages.OrderByDescending(m => m.ReceivedDateTime).ExecuteAsync();
var messages = mail_Box.CurrentPage; << only gets 10 emails at a time
foreach (var message in messages)
{
var stop = 0;
}
return emails;
}
catch (Exception ex)
{
// TODO Log
return emails;
}
}
观看结果