4

So i am currently building an application that allows a group of users to view all emails from a certain email address. This all works correctly. The problem I am encountering comes when i am trying to get the attachments.

I am relatively new to this area and used the example from Microsoft found here. Compare this to my code below:

    protected internal override Stream GetAttachmentStreamFinal(MailAttachmentDetails attachment)
    {
        var response = m_service.GetAttachments(new[] { attachment.Id }, BodyType.Text, Enumerable.Empty<PropertyDefinitionBase>());
        if (response.OverallResult != ServiceResult.Success)
        {
            if (response.Count > 0)
            {
                var ex = new MailException(response[0].ErrorMessage);
                ex.Data.Add(response[0].ErrorCode, response[0].ErrorMessage);
                foreach (var ed in response[0].ErrorDetails)
                {
                    ex.Data.Add(ed.Key, ed.Value);
                }
                throw ex;
            }
            throw new MailException("Error occurred while fetching the attachment from the mail service.");
        }

        foreach (var attachmentResponse in response)
        {
            if (attachmentResponse.Attachment is FileAttachment)
            {
                var fa = attachmentResponse.Attachment as FileAttachment;
                var cs = new MemoryStream(fa.Content);
                fa.Load(cs);
                cs.Seek(0, SeekOrigin.Begin);
                return cs;
            }
        }
        return null;
    }

As you can see both sets of code are very similar. However when I step through and get to the attachmentResponse.Attachment is FileAttachment line I get this error thrown

Attempt by method 'Mail.ExchangeEmailService.GetAttachmentStreamFinal(Mail.MailAttachmentDetails)' to access method 'Microsoft.Exchange.WebServices.Data.GetAttachmentResponse.get_Attachment()' failed.

Everything is being passed in correctly and the response returns as success.

I have noticed when stepping through my code that Attachment shows as a non-public member. But as this is encapsulated in Microsofts class im unsure as to why that is the case or what i can do?

Major
  • 146
  • 10
  • Are you doing this from a mail app (that runs inside of Outlook)? The sample you linked is for mail apps and is more complicated than a normal EWS application needs to be. – Jason Johnston Mar 25 '15 at 13:18
  • No this runs in a Web application. and this is used when the client requests the file – Major Mar 25 '15 at 13:21
  • I am also running into same issue while I have used latest version of assembly form nuget, but when i reference assembly after installing from [MSI](http://www.microsoft.com/en-us/download/details.aspx?id=42951) it works. Do you came across some stable solution to the problem? – Hitendra Aug 25 '15 at 07:00

3 Answers3

5

I just want to expand on @Jason Johnstons answer.

For some reason the version of EWS in NuGet is not correct. It throws the error that you are experiencing.

A workaround is to remove the reference to the NuGet package via

Uninstall-Package Microsoft.Exchange.WebServices

Then download and run the MSI file here

https://www.microsoft.com/en-us/download/details.aspx?id=42951

This will install the DLLs you require to the default location of

[ C:\Program Files\Microsoft\Exchange\Web Services\2.2 ]

Then simply copied they into your lib directory (or such) and created references to the DLLs directly instead.

Credit: http://www.resolvinghere.com/sm/microsoftexchangewebservicesdatagetattachmentresponsegetattachment-failed.shtml

3

As the other answers already mentioned, the nuget package from Microsoft is not up to date. I also had the same problem as the OP.

First I solved it by following the answer of Daniel - SDS Group. But then I found the nuget package Exchange.WebServices.Managed.Api from marklamley. It is the current version 2.2.1.1 of the ews-managed-api GitHub project.

zgue
  • 3,793
  • 9
  • 34
  • 39
  • This nuget package worked for me. I am able to download the attachments grammatically when I run the code in Visual Studio . However, when I deploy the same code in AWS hosting , it gives me the error which we faced earlier - 'Attempt by method 'Mail.ExchangeEmailService.GetAttachmentStreamFinal(Mail.MailAttachmentDetails)' to access method 'Microsoft.Exchange.WebServices.Data.GetAttachmentResponse.get_Attachment()' failed." – Balaji Birajdar Jun 13 '18 at 16:50
  • 1
    @BalajiBirajdar I do host my app in Azure and have no experience with AWS. But there should no difference between running locally or in the cloud. I never had such an issue. – zgue Jun 13 '18 at 17:26
2

Make sure you have the latest version of the Microsoft.Exchange.WebServices.dll. Older versions didn't return the actual attachment data when calling that particular overload of the GetAttachments method.

Jason Johnston
  • 17,194
  • 2
  • 20
  • 34
  • Im using version 2.2 which i downloaded from [link](http://www.microsoft.com/en-us/download/details.aspx?id=42951) Is this correct? The properties says Version 15.0.0.0 and Runtime Version 2.0.50727 – Major Mar 25 '15 at 15:15
  • 1
    You were correct in the end. I had referenced it but in my build it was still using the old version 14. – Major Mar 25 '15 at 16:35
  • Make sure you've got correct version registered in GAC. Could have saved me a few hours today... – MattheW Jul 26 '17 at 13:02