Tuesday, August 6, 2013

Send email using late binding CRM 2011

You can easily send emails using late binding in CRM 2011 here is the code snippets to send an email.              

WhoAmIRequest systemUserRequest = new WhoAmIRequest();
                WhoAmIResponse systemUserResponse =                    (WhoAmIResponse)serviceProxy.Execute(systemUserRequest);
                Guid _userId = systemUserResponse.UserId;
                Entity Fromparty = new Entity("activityparty");
                Entity Toparty = new Entity("activityparty");
                //set partyid
                //You can refer http://msdn.microsoft.com/en-us/library/gg328549.aspx to get acitivity party entity attribute
                Toparty["partyid"] = new EntityReference("contact", _contactId);
                Fromparty["partyid"] = new EntityReference("systemuser", _userId);
                //create email  and set attributes
                Entity emailCreate = new Entity("email");
                emailCreate["from"] = new Entity[] { Fromparty };
                emailCreate["to"] = new Entity[] { Toparty };
                emailCreate["subject"] = "subject";
                emailCreate["description"] = "EmailBody";
                emailCreate["regardingobjectid"] = new EntityReference("incident", regardindId);
                Guid emailId = serviceProxy.Create(emailCreate);

If you need more information please use comment section :) Thanks