使用ASP.NET Core从Exchange托管API获取附件 [英] Getting attachments from Exchange Managed API using ASP.NET Core

查看:182
本文介绍了使用ASP.NET Core从Exchange托管API获取附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Outlook加载项,该加载项对我拥有的外部API进行身份验证.对于我的一项功能,我正在从附件中向API发送附件ID列表.然后,我可以使用这些ID从Microsoft的Exchange托管API获取相应的附件.问题是,因为我使用的是.NET Core,所以推荐的库缺少访问附件所需的必要功能.

I am in the process of building an Outlook Add-In that authenticates to an external API that is also in my possession. For one of my functions, I am sending a list of attachment IDs from the add-in to the API. I can then use these IDs to get the corresponding attachments from Microsoft's Exchange Managed API. The issue is that because I'm using .NET Core, the recommended libraries are lacking the necessary functionality needed to access the attachments.

这是我尝试用来从主Microsoft.Exchange.WebServices库访问附件的一些代码:

Here is some code that I am trying to use to access the attachments from the main Microsoft.Exchange.WebServices library:

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或类似的状态.

The issue is that the first line of code throws an error unless I append .Result on the end of it. This is some flaw in the .NET Core version of the library. When I go with it, the task never leaves the status of Awaiting Action or something similar.

另一个应该针对.NET Core进行调整的库是Microsoft.Exchange.WebServices.NETStandard.

Another library that is supposedly adjusted for .NET Core is Microsoft.Exchange.WebServices.NETStandard.

以下是我发现可以正常工作的一些代码:

Here is some code I found that is suppose to work:

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.更不用说,呼叫立即失败,并出现关于重复密钥的错误.我可能记得在某处读过这种方法仅接受用户名/密码凭据.

With this sample, the object models are different and there is not even an Overall Result on the response. Not to mention, the call fails immediately with errors about there being a duplicate key. I may remember reading somewhere that this method only accepts username/password credentials.

还有没有其他解决方案可以帮助我从我的API接收来自Outlook电子邮件的附件?我是否错过了明显痛苦的事情?

Does anyone have any other solutions that could help me receive attachments from Outlook emails from my API? Am I missing something painfully obvious?

推荐答案

您还可以将Outlook REST Endpoint用于消息和日历以检索附件,这将以数组元素的形式将所有附件作为JSON格式的数据返回

You can also use Outlook REST Endpoint for messages and calendar to retrive attachments ,which will return all attachments as array elements with data in JSON Format

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;
                        }

这篇关于使用ASP.NET Core从Exchange托管API获取附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆