Send Emails and Attachments in CRM 2011 using the SDK

by Steven Jennings 7. July 2011 18:18

The other day I had a request from a client to create a customer ASPX webpage that sends emails through CRM along with the attached Excel file. Here is an example of how this can be completed. Start with the Send Email sample code from Microsoft website located here and added the functionality of attaching files to the email.

 

Adding an attached file to the Email

 

Step 1) First you need to attach a file to the ActivityMimeAttachment as base64

 

Code:

public ActivityMimeAttachment AttachFileToEmail(String fileName)

{

// Open a file and read its contents into a byte array.

var fileLocation    = ConfigurationManager.AppSettings["ExcelPath"] + fileName;

var stream          = File.OpenRead(fileLocation);

var byteData        = new byte[stream.Length];

 

stream.Read(byteData, 0, byteData.Length);

 

// Encode the data using base64.

var encodedData = Convert.ToBase64String(byteData);

var extension = Path.GetExtension(fileLocation).ToLower();

 

String mimeType = null;

 

switch (extension)

{

case ".xls":

mimeType = "application/vnd.ms-excel";

break;

case ".xlsx":

mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

break;

}

 

var sampleAttachment = new ActivityMimeAttachment

{

ObjectId        = new EntityReference(Email.EntityLogicalName, _emailId),

ObjectTypeCode  = Email.EntityLogicalName,

FileName        = fileName,

MimeType        = mimeType,

Body            = encodedData,

};

 

return sampleAttachment;

}

 

Step 2) Add the EncodedDate to the Create an e-mail message in the Sample Code

Code:

// Create an e-mail message.

Email email = new Email

{

To = new ActivityParty[] { toParty },

From = new ActivityParty[] { fromParty },

Subject = "SDK Sample e-mail",

Description = "CRM SDK Sample Body

DirectionCode = true,

email_activity_mime_attachment = new[] { AttachFileToEmail("FileName") }

};

 

The Sample of the Closed Activity Record in CRM

 image

You can download my SendEmail sample code here.

 

If you have any questions or need help please contact me @ steven.jennings[at]aresgrp.com

Tags:

CRM 2011

Page List

About the author

I work for a consulting firm in Dublin Ohio called Affiliated Resource Group. For the last five years I have been spearheading our Microsoft Dynamics CRM practice. I have a deep appreciation for the Microsoft CRM platform and I am very excited about it. You might even describe me as a Microsoft CRM Advocate. I have many battle scars from my experience with the product and I’m constantly being asked questions about CRM and how-to-do something in it. Hence, this BLOG is to help disseminate that knowledge and information to everyone. As of last year I was posting links to many other blogs to help spread the knowledge, but now with the community.dynamics.com doing that for me, I will be following that practice unless a really juicy article catches my eye. Many people have asked where my post are for the first half of 2010, my company had me posting to another blog and maintain two was near impossible. I am now down to just this blog. So good luck and I hope that this blog may help in some way. If you have suggestions or questions, please email me them.

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012 BillOnCRM