0

When attachment is Email I could simply get the mime content and save it. But when the attachment is Contact I am not able to get the MimeContent. Here is the code

itemAttachment.Load(new PropertySet(ItemSchema.MimeContent)); MimeContent mc = itemAttachment.Item.MimeContent; //Convert it to bytes byte[] contentBytes = mc.Content;

When attachment is contact I am getting below exception

Microsoft.Exchange.WebServices.Data.ServiceResponseException occurred HResult=-2146233088 Message=MIME conversion is not supported for this item type. Source=Microsoft.Exchange.WebServices StackTrace: at Microsoft.Exchange.WebServices.Data.ServiceResponse.InternalThrowIfNecessary() at Microsoft.Exchange.WebServices.Data.ServiceResponse.ThrowIfNecessary() at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest1.Execute() at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalGetAttachments(IEnumerable1 attachments, Nullable1 bodyType, IEnumerable1 additionalProperties, ServiceErrorHandling errorHandling) at Microsoft.Exchange.WebServices.Data.ExchangeService.GetAttachment(Attachment attachment, Nullable1 bodyType, IEnumerable1 additionalProperties) at Microsoft.Exchange.WebServices.Data.Attachment.InternalLoad(Nullable1 bodyType, IEnumerable1 additionalProperties) at Microsoft.Exchange.WebServices.Data.ItemAttachment.Load(IEnumerable`1 additionalProperties) at Presensoft.JournalEmailVerification.EmailVerification.DownloadFailedAttachments(EmailMessage msg, JournalEmail journalEmail) in C:\Users\sameer\Presensoft\Email Archiving\Presensoft.JournalEmailVerification\EmailVerification.cs:line 605 InnerException:

What is the proper way to download and save Contact attachment?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Sameer
  • 3,124
  • 5
  • 30
  • 57

1 Answers1

1

Saving as a contact in Exchange

Using MimeContent on a contact will not work because the server does not support the conversion. You can get MimeContent for a contact, you just can't save MimeContent as a contact.

Unfortunately there is no easy way to save an attached contact. What I would suggest is to manually clone the contact. Using EWS Managed API you can access the ItemAttachment as a contact. You would then need to create a new contact on the server, copy all the properties from the attached contact to the new contact using TryGetProperty(), then save the new contact.

I know it would make sense to just use the Save method on the contact object when you access the ItemAttachment. The problem is that there is a read only property on the object, IsAttachment, that is set to true and will not allow the object to be saved. If you try you will get an exception that this method is not available for attachments.

Saving as a .vcf file

If you are trying to save the contact to your local file system there are just a couple lines of code necessary.

itemAttachment.Load(ItemSchema.MimeContent);
File.WriteAllBytes("Contact.vcf", itemAttachment.Item.MimeContent.Content);

I hope this information helps. If this does resolve your problem, please mark the post as answered.

Thanks,

--- Bob ---

Bob Bunn
  • 613
  • 3
  • 7
  • My requirement is to save the attachment in the file system as a separate file. Any ways to achieve this? – Sameer Apr 03 '14 at 17:55
  • I was able to get the MimeContent and save the file with my test code. I updated the answer to add the two lines of code I used after determining that the attachment was an ItemAttachment and the ItemClass of the attachment was IPM.Contact. – Bob Bunn Apr 03 '14 at 18:19
  • Bob, itemAttachment.Load(new PropertySet(ItemSchema.MimeContent)) and itemAttachment.Load(ItemSchema.MimeContent) what is the difference? – Sameer Apr 03 '14 at 18:22
  • From what I can tell there is no difference. I've tried both with my code and get the same results. – Bob Bunn Apr 03 '14 at 18:36
  • Bob, that is the same code I am using and with only difference of itemAttachment.Load(new PropertySet(ItemSchema.MimeContent)) use . I am getting the same exception anyways. I am wondering how is it working for you. Is there any settings in exchange to make it work?? – Sameer Apr 04 '14 at 04:17
  • Ahhh!!! the code is working for me in my local system and local exchange setup..it is only that this is not working while trying to grab mime content from Clients exchange box.. Client is using Exchange 2010 without any service pack... Is there any dependency on exchange version?? – Sameer Apr 04 '14 at 06:26