使用 ASP.NET 核心从 Exchange Managed API 获取附件
Getting attachments from Exchange Managed API using ASP.NET Core
我正在构建一个 Outlook 加载项,该加载项对我也拥有的外部 API 进行身份验证。对于我的一个功能,我将附件 ID 列表从加载项发送到 API。然后我可以使用这些 ID 从 Microsoft 的 Exchange Managed API 获取相应的附件。问题是因为我使用的是 .NET Core,推荐的库缺少访问附件所需的必要功能。
这是我试图用来从主 Microsoft.Exchange.WebServices
库访问附件的一些代码:
ServiceResponseCollection<GetAttachmentResponse> getAttachmentsResponse = service.GetAttachments(attachmentInfo.attachmentIds.ToArray(), null, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent));
if (getAttachmentsResponse.OverallResult == ServiceResult.Success)
{
foreach (var attachmentResponse in getAttachmentsResponse)
{
// removed logic for simplicity
}
}
问题是第一行代码会引发错误,除非我在其末尾附加 .Result
。这是库的 .NET Core 版本中的一些缺陷。当我使用它时,任务永远不会离开 Awaiting Action
或类似的状态。
另一个据称针对 .NET Core 进行了调整的库是 Microsoft.Exchange.WebServices.NETStandard
。
这是我发现的一些代码,应该可以工作:
var getAttachmentsResponse = service.GetAttachments(attachmentInfo.attachmentIds.ToArray(), null, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent));
foreach (var attachmentResponse in getAttachmentsResponse)
{
// removed logic for simplicity
}
对于这个示例,对象模型不同,响应中甚至没有 Overall Result
。更不用说,调用会立即失败,并出现有关存在重复键的错误。我可能记得在某处读到此方法只接受 username/password 凭据。
有没有人有任何其他解决方案可以帮助我从 API 的 Outlook 电子邮件中接收附件?我是不是漏掉了一些明显的东西?
您还可以使用邮件和日历的 Outlook REST 端点来检索附件,这将return所有附件作为数组元素,数据采用JSON格式
var currentAttachments ;
function getAttachments()
{
var options ={
isRest: true,
asyncContext: { message: 'Hello World!' }
};
Office.context.mailbox.getCallbackTokenAsync(options, getAttachment);
function getAttachment(asyncResult)
{
var token = asyncResult.value;
var getAttachmentsUrl = Office.context.mailbox.restUrl +
'/v2.0/me/messages/' + Office.context.mailbox.convertToRestId(Office.context.mailbox.item.itemId, Office.MailboxEnums.RestVersion.v2_0) + '/attachments';
$.ajax({
url: getAttachmentsUrl,
contentType: 'application/json',
type: 'get',
headers: { 'Authorization': 'Bearer ' + token }
}).done(function (item) {
currentAttachments = item;
}).fail(function (error) {
console.log(error);
});
}
}
// Then we can use Foreach Loop to iterate through these attachments
var outputStringFromRest = "";
for (i = 0; i < currentAttachments.value.length; i++) {
var _att = currentAttachments.value[i];
outputStringFromRest += "<BR>" + i + ". Name: ";
outputStringFromRest += _att.Name;
outputStringFromRest += "<BR>ID: " + _att.Id;
outputStringFromRest += "<BR>contentType: " + _att.ContentType;
outputStringFromRest += "<BR>size: " + _att.Size;
outputStringFromRest += "<BR>attachmentType: " + "file";
outputStringFromRest += "<BR>isInline: " + _att.IsInline;
}
我正在构建一个 Outlook 加载项,该加载项对我也拥有的外部 API 进行身份验证。对于我的一个功能,我将附件 ID 列表从加载项发送到 API。然后我可以使用这些 ID 从 Microsoft 的 Exchange Managed API 获取相应的附件。问题是因为我使用的是 .NET Core,推荐的库缺少访问附件所需的必要功能。
这是我试图用来从主 Microsoft.Exchange.WebServices
库访问附件的一些代码:
ServiceResponseCollection<GetAttachmentResponse> getAttachmentsResponse = service.GetAttachments(attachmentInfo.attachmentIds.ToArray(), null, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent));
if (getAttachmentsResponse.OverallResult == ServiceResult.Success)
{
foreach (var attachmentResponse in getAttachmentsResponse)
{
// removed logic for simplicity
}
}
问题是第一行代码会引发错误,除非我在其末尾附加 .Result
。这是库的 .NET Core 版本中的一些缺陷。当我使用它时,任务永远不会离开 Awaiting Action
或类似的状态。
另一个据称针对 .NET Core 进行了调整的库是 Microsoft.Exchange.WebServices.NETStandard
。
这是我发现的一些代码,应该可以工作:
var getAttachmentsResponse = service.GetAttachments(attachmentInfo.attachmentIds.ToArray(), null, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent));
foreach (var attachmentResponse in getAttachmentsResponse)
{
// removed logic for simplicity
}
对于这个示例,对象模型不同,响应中甚至没有 Overall Result
。更不用说,调用会立即失败,并出现有关存在重复键的错误。我可能记得在某处读到此方法只接受 username/password 凭据。
有没有人有任何其他解决方案可以帮助我从 API 的 Outlook 电子邮件中接收附件?我是不是漏掉了一些明显的东西?
您还可以使用邮件和日历的 Outlook REST 端点来检索附件,这将return所有附件作为数组元素,数据采用JSON格式
var currentAttachments ;
function getAttachments()
{
var options ={
isRest: true,
asyncContext: { message: 'Hello World!' }
};
Office.context.mailbox.getCallbackTokenAsync(options, getAttachment);
function getAttachment(asyncResult)
{
var token = asyncResult.value;
var getAttachmentsUrl = Office.context.mailbox.restUrl +
'/v2.0/me/messages/' + Office.context.mailbox.convertToRestId(Office.context.mailbox.item.itemId, Office.MailboxEnums.RestVersion.v2_0) + '/attachments';
$.ajax({
url: getAttachmentsUrl,
contentType: 'application/json',
type: 'get',
headers: { 'Authorization': 'Bearer ' + token }
}).done(function (item) {
currentAttachments = item;
}).fail(function (error) {
console.log(error);
});
}
}
// Then we can use Foreach Loop to iterate through these attachments
var outputStringFromRest = "";
for (i = 0; i < currentAttachments.value.length; i++) {
var _att = currentAttachments.value[i];
outputStringFromRest += "<BR>" + i + ". Name: ";
outputStringFromRest += _att.Name;
outputStringFromRest += "<BR>ID: " + _att.Id;
outputStringFromRest += "<BR>contentType: " + _att.ContentType;
outputStringFromRest += "<BR>size: " + _att.Size;
outputStringFromRest += "<BR>attachmentType: " + "file";
outputStringFromRest += "<BR>isInline: " + _att.IsInline;
}