27 November, 2011

CRM to SendEmail

Send email functionality with CRM
     internal static Boolean SendEmail(string toAddress, Guid? fromPartyGuid, string subjectLine, string body, Guid? contactId,string templateName)  
     {  
       CrmService crmService = GetCrmService();  
       //[[FROM activity party for the email.  
       activityparty fromParty = new activityparty();  
       fromParty.partyid = new Lookup();  
       fromParty.partyid.type = EntityName.queue.ToString();  
       fromParty.partyid.Value = fromPartyGuid;   
       //]]       
       //[[TO activity party for email  
       activityparty toParty = new activityparty();  
       toParty.partyid = new Lookup();  
       toParty.partyid.type = EntityName.contact.ToString();  
       toParty.partyid.Value = (contactId.HasValue) ? contactId.Value : new Guid("");  
       //]]  
       //[[Create a new email and set its properties  
       email emailInstance = new email();  
       //set email parameters  
       emailInstance.from = new activityparty[] { fromParty };  
       emailInstance.to = new activityparty[] { toParty };  
       emailInstance.subject = subjectLine;  
       emailInstance.description = body;  
       emailInstance.regardingobjectid = CreateLookup(contactId.ToString(), EntityName.contact);  
       emailInstance.trackingtoken = templateName;  
       //]]  
       //Create a GUID for the email  
       Guid emailId = crmService.Create(emailInstance);  
       //Create a SendEmailRequest  
       SendEmailRequest request = new SendEmailRequest();  
       request.EmailId = emailId;  
       request.IssueSend = true;  
       request.TrackingToken = "";  
       //Execute request  
       SendEmailResponse sendEmailresp = (SendEmailResponse)crmService.Execute(request);  
       return true;  
     }  
           public static Lookup CreateLookup(string guid, EntityName lookupType)  
     {  
       if (string.IsNullOrEmpty(guid)) return null;  
       Lookup lkup = new Lookup();  
       lkup.Value = new Guid(guid);  
       lkup.type = lookupType.ToString();  
       return lkup;  
     }  

0 comments:

Post a Comment