Getting attachments from Exchange Managed API using ASP.NET Core

巧了我就是萌 提交于 2019-12-08 04:08:47

问题


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.

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

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.

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
}

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.

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


回答1:


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


来源:https://stackoverflow.com/questions/47761482/getting-attachments-from-exchange-managed-api-using-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!