Welcome to @!$h@s Free CodeWorld
  free books Search
  free books FAQs
  free books Source Code
  free books Articles
  free books Resources
   Windows Tips & Tricks
   Dot Net Section
   About Myself

I am a B.E in Information Technology form Lingaya's Institute of Management and Technology Faridabad, India.

I have worked on VC++, MFC, ASP, ASP.NET ,Sql Server. Currently I am working on Visual C++ and MFC.

I made a free open source firewall for windows which can be find in MYProjects section.

I am currently working in GloalLogic, Noida.

For any comments and suggestions Contact Me at :

MySpace profile: Here

   Microsoft CRM Customization – programming email activity attachment

Submitted By :

by Boris Makushkin


Microsoft CRM is now on the scene and it is increasing its market share, due to Microsoft Business Solutions muscles and marketing strategy. It is tightly integrated with other Microsoft Business Solutions products such as Microsoft Great Plains, Solomon, Navision. Being relatively inexpensive in comparison to competitors, like Siebel, Oracle - Microsoft CRM opens you the door for worldwide operations automation. There are many crm web programs available, it's all a matter of researching which is best for you. In this small article we would like to give you, software developer, some hints on Microsoft CRM customization.
Today's topic is Activity of email type programming - you usually deal with these customizations when you improve Microsoft Exchange CRM connector. How do you create email attachment - this is the main discussion topic. We’ll use C#.Net.

Explanation :

In Exchange handler/event sink you create Activity of email type in MS CRM and one of the tasks is transfer the attachment(s) from the body of the incoming email to the attachment(s) in the Activity. You can realize it through direct access to Microsoft CRM DB. Let’s see C# code:
  1. First we are getting access to the letter via ExOLEDB:

CDO.Message iMessage  = new  CDO.MessageClass();
CDO.IBodyPart iPrt;

iMessage.DataSource.Open(bstrURLItem,  null,

"" "");

 2.   Next – we come through the attachment list, get their names and save their bodies into temporary catalogue:

for(int  1 i < aNum i++) {
    string  fName  iMessage.Attachments[i].FileName;
eName  fName.Substring(fName.Length-3 3).ToUpper();

     log.Debug("Attachment: "  + fName);

     iPrt  iMessage.Attachments[i];
attName  Path.GetTempPath() + fName;

     attachments.Add(Guid.NewGuid(), attName);

     iPrt  = null;

 3.   Then we cycle through the cache, containing attachments info and add them to the Activity created:

if  (attachments != null) {
     ICollection keys  attachments.Keys;
attCounter  0;

(Guid o  in  keys) {
        string  attName  (string)(attachments[o]);

crmConnector.AddAttachmentToActivity(emailId, attName, (new 
          FileInfo (attName)).Length, attCounter);


 4.   Here is the method of adding attachment to Activity:

public  Guid AddAttachmentToActivity(Guid emailId,  string  filename,  long 
filesize,  int  attachmentNumber) {
    try  {
          log.Debug("Prepare for Mail Activity Attachment Creating");

          // BizUser proxy object
          Microsoft.Crm.Platform.Proxy.BizUser bizUser  = new 

          ICredentials credentials  = new  NetworkCredential(sysUserId, 
        sysPassword, sysDomain);

bizUser.Url  crmDir +  "BizUser.srf";
bizUser.Credentials  credentials;
Microsoft.Crm.Platform.Proxy.CUserAuth userAuth 

// CRMActivityAttachment proxy object
activityAttachment.Credentials  credentials;
activityAttachment.Url  crmDir +  "CRMActivityAttachment.srf";

// Set up the XML string for the activity attachment
        string  strXml  "<activitymimeattachment>";
strXml + "<subject>Activity 1</subject>";
strXml + "<attachmentnumber>"  + attachmentNumber +
strXml + "<activityid>"  + emailId.ToString("B") +  "</activityid>";
strXml + "</activitymimeattachment>";

// Create the activity attachment
         Guid attachmentId  = new  Guid(activityAttachment.Create(userAuth,
log.Debug("Create Attachemnt ID: "  + attachmentId.ToString("B"));

UploadFileToDB(attachmentId, filename, filesize);

    catch  (System.Web.Services.Protocols.SoapException e) {
         log.Debug("ErrorMessage: "  + e.Message +  " "  + e.Detail.OuterXml + 
         " Source: "  + e.Source);
    catch  (Exception e) {
     log.Debug(e.Message +  "\r\n"  + e.StackTrace);

    return new  Guid();

 5.   Main problem, however is attachment body adding to MS CRM database. Main requirement is – attachment must be encoded as BASE64 stream and its length must be specified correctly together with Nine Type and file name of the file it will be knows as an attachment in activity. Let’s look at the C# code:

public void  UploadFileToDB(Guid attachmentId,  string  filename,  long  filesize) 
    string  contentType  "application/octet-stream";

          Hashtable mimes  LoadMimeDB(Environment.SystemDirectory + 

(mimes != null) {
            string  tmpContentType  GetMimeType(mimes, filename);

(tmpContentType != null  ∓∓ !tmpContentType.Equals(""))
          contentType  tmpContentType;

        byte[] memoryData  = new byte[filesize];

          FileStream fs  = new  FileStream(filename, FileMode.Open, 
          BinaryReader reader  = new  BinaryReader(fs);

          reader.Read(memoryData,  0, (int)filesize);


          OleDbCommand command  conn.CreateCommand();

          command.CommandText  "UPDATE ActivityMimeAttachment 
            SET FileSize =
          (?), MimeType = (?), FileName = (?), Body = (?) WHERE 
         ActivityMimeAttachmentId = (?)";
command.Parameters.Add(new  OleDbParameter("FileSize", filesize));
command.Parameters.Add(new  OleDbParameter("MimeType",
command.Parameters.Add(new  OleDbParameter("FileName"
          new  FileInfo (filename).Name));
command.Parameters.Add(new  OleDbParameter("Body",
         Convert.ToBase64String(memoryData,  0, (int)filesize)));
command.Parameters.Add(new  OleDbParameter
         ("ActivityMimeAttachmentId", attachmentId));

log.Debug("Prepare to upload attachemnt " 
           attachmentId.ToString("B") +  " in ActivityMimeAttachment");


memoryData  = null;
    catch  (Exception e) {
         log.Debug(e.Message +  "\r\n"  + e.StackTrace);

 6.   File ContectType.txt is matching list of the files extensions and their mime-type in the following format:

asc application/pgp-encrypted Armored Encrypted file  (PGP)
asd application
/astound Autosave file  (Word for Windows)
asm PC ASM File
asn application


Happy customizing, implementing and modifying! If you want us to do the job - give us a call 1-866-528-0577! help@albaspectrum.com

About Boris Makushkin :

Boris Makushkin is Lead Software Developer in Alba Spectrum Technologies – USA nationwide Microsoft CRM, Microsoft Great Plains customization company, based in Chicago, Boston, San Francisco, San Diego, Los Angeles, Houston, Dallas, Atlanta, Miami, Montreal, Toronto, Vancouver, Moscow and Europe and internationally (www.albaspectrum.com), he is Microsoft CRM SDK, C#, VB.Net, SQL, Oracle, Unix developer.

Other articles :

  1. Orkut Helper Tools and Utilities
  2. Introduction to Orkut (Tips)
  3. Tips and Tricks about Google Talk (New IM from Google )
  4. Tips and Tricks about Google
  5. Tips and Tricks about GMail

Copyright 2012, ProgrammerWorld.NET

Comments | Suggest a Site | Contact us for Advertisement | Submit Article | Submit Source Code |
For any queries regarding this web site or to contribute to this site mail me at Admin@ProgrammerWorld.NET

Advertiser can contact us on above mentioned email address. This site get more than 60000 unique Ip hits per month.

Like this site ? Keep this site available and free by donating