Monday 6 June 2016

How to send email with attachment in Microsoft Dynamics CRM using c#

How to send email along with attachment in Microsoft Dynamics CRM using c#

How to add attachments to an activity/email inMicrosoft Dynamics CRM using c#


Today we'll learn how we can send emails along with attachments using c# code.

There are situations when we need to generate emails using plugins or sometime we need to build

scheduler services to send emails along with attachments on regular interval.



In such scenario one needs to write code to create email activity in CRM using code and send that email from code itself.



















Here is the C# code snippet to do the same:


How to create toParty:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

   Entity toParty = new Entity("activityparty");
   toParty["participationtypemask"] = new OptionSetValue(0);
   toParty["addressused"] = "email address of receiver";
   toParty["fullname"] = "full name of 
receiver";


How to send email along with the attachment:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 public static void sendEmail(Entity to_Party,String Emailbody,String attachmentbody,IOrganizationService service)
        {
            Entity email = new Entity("email");
            Entity from_Party = new Entity("activityparty");
           
           QueryExpression queue = new QueryExpression("queue");
            queue.Criteria.AddCondition("name", ConditionOperator.Equal, "your sender queue name");
            queue.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
            EntityCollection queueColl = service.RetrieveMultiple(queue);
            if (queueColl.Entities.Count > 0)
            {
                from_Party["partyid"] = new EntityReference("queue", queueColl.Entities[0].Id);
            }

            email["from"] = new Entity[] { from_Party };
            email["to"] = new Entity[] { to_Party };
            email["subject"] = "Subject of the Email";
            email["description"] = Emailbody;
            Guid emailguid = service.Create(email);

           //Add text attachment 
            addattachments(emailguid, service, attachmentbody);

            SendEmailRequest sendEmailreq = new SendEmailRequest
            {
                EmailId = emailguid,
                TrackingToken = "",
                IssueSend = true
            };
            SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);  
        }

 public static void addattachments(Guid emailguid,String message,IOrganizationService service)
{
Entity attachment = new Entity("activitymimeattachment");
attachment["subject"] = "Attachment";
string fileName = "Attachment.txt";
attachment["filename"] = fileName;
byte[] fileStream = Encoding.ASCII.GetBytes(message);   

attachment["body"] = Convert.ToBase64String(fileStream);
attachment["mimetype"] = "text/plain";
attachment["attachmentnumber"] = 1;
attachment["objectid"] = new EntityReference("email", emailguid);
attachment["objecttypecode"] = "email";
service.Create(attachment);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Hope it would be helpful.


Comments are highly appreciated..!!!

Happy CRMing 

3 comments:

  1. How do you find and extract LinkedIn emails? There are a number of ways to do this, including manually exporting the emails of your first-degree connections. If you want to get more interesting details about find address on linkedin, you may go online.

    ReplyDelete
  2. Travel Booking Software at time also called as Travel ERP has various modules in the same which at time the Travel and Tour Operators can select to take all or as per there need.

    ReplyDelete