Create E-mail Activity and Multiple E-mail Attachment in MS CRM 2011

Copy and paste the below function to get it work. You can use this function to create email activity and email attachment related to the same email activity from Plugin/ Custom Workflow / Or any external web application or windows application.

private void CreateEmailAndEmailAttachment(OrderDTO orderDTO)
{
Guid _emailId;
string[] emailAddresses;
string temp;
string emailstring;

List<ActivityParty> objtoParty = new List<ActivityParty>();
List<ActivityParty> objccParty = new List<ActivityParty>();
List<ActivityParty> objbccParty = new List<ActivityParty>();
List<ActivityParty> objfromParty = new List<ActivityParty>();

// Email From
var activityParty = new ActivityParty
{
AddressUsed = orderDTO.EmailFrom
};
objfromParty.Add(activityParty);

// Email To
emailAddresses = orderDTO.EmailTo.Split(‘;’);
foreach (string sAddr in emailAddresses)
{
temp = sAddr.Trim();

if (temp.IndexOf(“<“, StringComparison.Ordinal) == -1)
{
activityParty = new ActivityParty
{
AddressUsed = temp
};
}
else
{
emailstring = temp.Substring(temp.IndexOf(“<“, StringComparison.Ordinal) + 1, temp.IndexOf(“>”, StringComparison.Ordinal) – temp.IndexOf(“<“, StringComparison.Ordinal) – 1);
activityParty = new ActivityParty
{
AddressUsed = emailstring
};
}
objtoParty.Add(activityParty);
}

// Email CC
if (orderDTO.EmailCC.Length != 0)
{
emailAddresses = orderDTO.EmailCC.Split(‘;’);
foreach (string sAddr in emailAddresses)
{
temp = sAddr.Trim();

if (temp.IndexOf(“<“, StringComparison.Ordinal) == -1)
{
activityParty = new ActivityParty
{
AddressUsed = temp
};
}
else
{
emailstring = temp.Substring(temp.IndexOf(“<“, StringComparison.Ordinal) + 1, temp.IndexOf(“>”, StringComparison.Ordinal) – temp.IndexOf(“<“, StringComparison.Ordinal) – 1);
activityParty = new ActivityParty
{
AddressUsed = emailstring
};
}
objccParty.Add(activityParty);
}
}

// Email Bcc
if (orderDTO.EmailBcc.Length != 0)
{
emailAddresses = orderDTO.EmailBcc.Split(‘;’);
foreach (string sAddr in emailAddresses)
{
temp = sAddr.Trim();

if (temp.IndexOf(“<“, StringComparison.Ordinal) == -1)
{
activityParty = new ActivityParty
{
AddressUsed = temp
};
}
else
{
emailstring = temp.Substring(temp.IndexOf(“<“, StringComparison.Ordinal) + 1, temp.IndexOf(“>”, StringComparison.Ordinal) – temp.IndexOf(“<“, StringComparison.Ordinal) – 1);
activityParty = new ActivityParty
{
AddressUsed = emailstring
};
}
objbccParty.Add(activityParty);
}
}

//Creating Email Activity
Email email = new Email
{
Subject = orderDTO.EmailSubject,
ActivityId = Guid.NewGuid(),
To = objtoParty,
From = objfromParty,
Cc = objccParty,
Bcc = objbccParty,
Description = orderDTO.EmailBody,
RegardingObjectId = new EntityReference(Quote.EntityLogicalName, new Guid(orderDTO.OrderHeaderDTO.ordergroup_id))
};
_emailId = this.crmService.Create(email);

#region Creating Multiple E-mail Attachment

//Create Email Attachment
ActivityMimeAttachment _fileAttachment = new ActivityMimeAttachment
{
ObjectId = new EntityReference(Email.EntityLogicalName, _emailId),
ObjectTypeCode = Email.EntityLogicalName,
Subject = orderDTO.EmailSubject,
Body = System.Convert.ToBase64String(orderDTO.EmailAttachmentFileContent),
FileName = orderDTO.EmailAttachmentFileName
};
this.crmService.Create(_fileAttachment);

if (orderDTO.EmailAttachmentFileContent1 != null)
{
//Create Email Attachment1
_fileAttachment = new ActivityMimeAttachment
{
ObjectId = new EntityReference(Email.EntityLogicalName, _emailId),
ObjectTypeCode = Email.EntityLogicalName,
Subject = orderDTO.EmailSubject,
Body = System.Convert.ToBase64String(orderDTO.EmailAttachmentFileContent1),
FileName = orderDTO.EmailAttachmentFileName1
};
this.crmService.Create(_fileAttachment);
}

if (orderDTO.EmailAttachmentFileContent2 != null)
{
//Create Email Attachment2
_fileAttachment = new ActivityMimeAttachment
{
ObjectId = new EntityReference(Email.EntityLogicalName, _emailId),
ObjectTypeCode = Email.EntityLogicalName,
Subject = orderDTO.EmailSubject,
Body = System.Convert.ToBase64String(orderDTO.EmailAttachmentFileContent2),
FileName = orderDTO.EmailAttachmentFileName2
};
this.crmService.Create(_fileAttachment);
}

if (orderDTO.EmailAttachmentFileContent3 != null)
{
//Create Email Attachment3
_fileAttachment = new ActivityMimeAttachment
{
ObjectId = new EntityReference(Email.EntityLogicalName, _emailId),
ObjectTypeCode = Email.EntityLogicalName,
Subject = orderDTO.EmailSubject,
Body = System.Convert.ToBase64String(orderDTO.EmailAttachmentFileContent3),
FileName = orderDTO.EmailAttachmentFileName3
};
this.crmService.Create(_fileAttachment);
}

if (orderDTO.EmailAttachmentFileContent3 != null)
{
//Create Email Attachment4
_fileAttachment = new ActivityMimeAttachment
{
ObjectId = new EntityReference(Email.EntityLogicalName, _emailId),
ObjectTypeCode = Email.EntityLogicalName,
Subject = orderDTO.EmailSubject,
Body = System.Convert.ToBase64String(orderDTO.EmailAttachmentFileContent4),
FileName = orderDTO.EmailAttachmentFileName4
};
this.crmService.Create(_fileAttachment);
#endregion
}

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
This entry was posted in MS CRM 2011 and tagged , , . Bookmark the permalink.

2 Responses to Create E-mail Activity and Multiple E-mail Attachment in MS CRM 2011

  1. Ankit Gupta says:

    This article was quite helpful. Thanks for posting!!!

Leave a Reply

Your email address will not be published. Required fields are marked *