我使用c#代码和graph sdk 5.5来访问office 365邮箱。 我可以通过我的代码阅读所有消息,但无法检索任何附件。 找不到任何适用于 SDK 5.5 的工作示例。
我找到了下面的代码片段,但我不知道从哪里获取“附件 ID”:
var requestInfo = graphClient.Users["{user_id}"]
.Messages["{message_id}"]
.Attachments["{attachment_id}"]
.ToGetRequestInformation();
有人可以帮我解决这个问题吗?
这是我当前的代码:
var credentials = new ClientSecretCredential(directoryTenantID, applicationClientID, secretID);
var graphServiceClient = new GraphServiceClient(credentials);
// Read emails from the authenticated user's mailbox
try
{
Microsoft.Graph.Models.MessageCollectionResponse messageCollectionResponse = await graphServiceClient
.Users["***********"]
.MailFolders["Inbox"]
.Messages
.GetAsync(config => config.QueryParameters.Top = 50);
var messages = messag`your text`eCollectionResponse.Value;
foreach (var message in messages)
{
var attachments = graphClient.Users["[email protected]"].Messages[message.Id].Attachments.GetAsync(); (get an error on this line)
foreach (var attachment in attachments)
{
// Save attachment to a specific folder on your hard disk
// Customize the folder structure based on your needs
// For example:
// attachment.Save("C:\\Attachments\\" + attachment.Name);
}
}
}
调用异步方法时,使用await关键字确保执行等待操作完成。请参阅下面的 graphServiceClient 附件。
var credentials = new ClientSecretCredential(directoryTenantID, applicationClientID, secretID);
var graphServiceClient = new GraphServiceClient(credentials);
// Read emails from the authenticated user's mailbox
try
{
Microsoft.Graph.Models.MessageCollectionResponse messageCollectionResponse = await graphServiceClient
.Users["***********"]
.MailFolders["Inbox"]
.Messages
.GetAsync(config => config.QueryParameters.Top = 50);
foreach (var message in messages)
{
var attachments = await graphServiceClient.Users["***********"]
.Messages[message.Id]
.Attachments
.GetAsync();
foreach (var attachment in attachments)
{
// For example:
System.IO.File.WriteAllBytes($"C:\\Attachments\\{fileAttachment.Name}", fileAttachment.ContentBytes);
Console.WriteLine($"Saved {fileAttachment.Name}");
}
}
}