Thursday, December 20, 2007

How to download and save email activity attachments from CRM 3.0

Ever want to get an attachment from an Email in CRM 3.0?
Well you are in luck, here is how to do it.

1) Use fetchxml to get all attachments from a specific email or for any other emails matching your query, for simplicity sake, the fetch xml will be for a single email.

2) Email attachments will be returned in the result set complete with the contents of the original file in Base64 format, you will have to decode it.

Use the following method to get a nodelist for any query

static XmlNodeList GetFetchResult(string fetchXML)


CrmService service = new CrmService();

service.Credentials = new System.Net.NetworkCredential("CRMAdmin", "Pa$$w0rd", "ADVWORKS");

string fetch = fetchXML;

string ret = service.Fetch(fetch);

System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();


System.Xml.XmlNodeList list = xdoc.SelectNodes("resultset/result");

xdoc = null;

return list;


All email attachments are kept in the entity "ActivityMimeAttachment", here is the xml for the query.

Replace the "activityID" value with any email value or add your conditions.

<fetch mapping='logical'>
<entity name='activitymimeattachment'>
<filter type='and'>
<condition attribute='activityid' operator='eq' value='{A9B5DD63-D59E-DC11-920F-0003FF612152}'/>
Call the GetFetchResult into a new Nodelist and loop through it.
XmlNodeList list = GetFetchResult("");

if (list.Count > 0)


foreach (XmlNode n in list)

string body = n["body"].InnerText;
string decoded = base64Decode(body);



Use this method to convert the body of the email into it's original format.

static string base64Decode(string data)




System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();

System.Text.Decoder utf8Decode = encoder.GetDecoder();

byte[] todecode_byte = Convert.FromBase64String(data);

int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

char[] decoded_char = new char[charCount];

utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);

string result = new String(decoded_char);

return result;


catch (Exception e)


throw new Exception("Error in base64Decode" + e.Message);


From here you can save the file, the filename is in the "filename" attribute of the result set.
Have a great day.
Oshri Cohen

Labels: , , , , , , , ,


At February 4, 2008 at 5:34 AM , Anonymous Anonymous said...

Anyone have any idea about sending email using with crm3.0 web service(source Code) please send me back ASAP.


At February 4, 2008 at 5:38 AM , Blogger salman said...

Anyone have any idea about sending email using with crm3.0 web service(source code), please send me back ASAP.

At February 4, 2008 at 6:35 PM , Anonymous Jaber said...

Hello Oshri Cohen

My name i s Jaber and I found one of your great work on code project "How to detect a duplicate entry in Microsoft CRM 3.0". But when I using the Javascript on my Form. I got an error Permissin Denied. Although I put the user name, password and Domain instead of DefaultCredential i.e. System.Net.NetworkCredential("CRMAdmin", "Pa$$w0rd", "ADVWORKS");.
Can You help me out.

Thanks A Million.
Kind Regards

At February 5, 2008 at 4:44 PM , Blogger Oshri Cohen said...

You get a permission error with javascript on your page?
I have never seen that.

Please post your code in an email to

At May 1, 2008 at 10:00 AM , Anonymous Anonymous said...

Hi Oshri Cohen
I have a Problem to download an attachment from my crm 3.0
the anser from the crm is there
There was a problem while attempting to download the requested file.
the code is

Can you help me


At May 1, 2008 at 10:04 AM , Blogger Oshri Cohen said...

private byte[] GetAnnotationAttachment(Guid annotationId)
Guid attachid = annotationId;
int objecttypecode = 5; //Annotation attachment OTC
string url = _serverUrl + "/Activities/Attachment/download.aspx?AttachmentType=" + objecttypecode.ToString() + "&AttachmentId=" + attachid.ToString();
System.Net.WebClient myWebClient = new System.Net.WebClient();
myWebClient.Credentials = service.Credentials;
return myWebClient.DownloadData(url);


Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home