Showing posts with label Dynamics CRM 2011 Email. Show all posts
Showing posts with label Dynamics CRM 2011 Email. Show all posts

Thursday, April 12, 2012

Dynamics CRM 2011 : C# Code for Sending Email to Unresolved Recipients

In Dynamics CRM 2011 we can send email to unresolved receipts (email address not resolved as CRM record email address i.e. Contact, Account email address etc.) either by manually typing the receipt address into the To field or can add the recipient via C# code as well. You can use the code below to send email to unresolved recipient.

Dynamics CRM 2011 will only allow email send to unresolved reciepents if “Allow messages with unresolved e-mail reciepents to be sent” option in System Settings will be enabed (Settings->Administration-System Settings->E-mail).
 [C# Code]


private void SendEmailToUnresolvedRecent(IOrganizationService prmCrmService, string 
             prmToRecipientEmailAddress, Guid prmSenderUserId, string prmSubject, string prmMessageBody)
        {

            // Email record id
            Guid wod_EmailId = Guid.Empty;

            // Creating Email 'to' recipient activity party entity object
            Entity wod_EmailToReciepent = new Entity("activityparty");

            // Creating Email 'from' recipient activity party entity object
            Entity wod_EmailFromReciepent = new Entity("activityparty");

            // Assigning receiver email address to activity party addressused attribute
            //wod_EmailToReciepent["participationtypemask"] = new OptionSetValue(0);
            wod_EmailToReciepent["addressused"] = prmToRecipientEmailAddress;

            // Setting from user account
            wod_EmailFromReciepent["partyid"] = new EntityReference("systemuser", prmSenderUserId);

            // Creating Email entity object
            Entity wod_EmailEntity = new Entity("email");

            // Setting email entity 'to' attribute value
            wod_EmailEntity["to"] = new Entity[] { wod_EmailToReciepent };

            // Setting email entity 'from' attribute value
            wod_EmailEntity["from"] = new Entity[] { wod_EmailFromReciepent };

            // Setting email subject and description
            wod_EmailEntity["subject"] = prmSubject;

            wod_EmailEntity["description"] = prmMessageBody;

            // Creating email record
            wod_EmailId = prmCrmService.Create(wod_EmailEntity);

            // Creating SendEmailRequest object for sending email
            SendEmailRequest wod_SendEmailRequest = new SendEmailRequest();

            // Creating Email tracking token request object
            GetTrackingTokenEmailRequest wod_GetTrackingTokenEmailRequest = new GetTrackingTokenEmailRequest();

            // Creating Email tracking token response object to get tracking token value
            GetTrackingTokenEmailResponse wod_GetTrackingTokenEmailResponse = null;

            // Setting email record if for sending email
            wod_SendEmailRequest.EmailId = wod_EmailId;

            wod_SendEmailRequest.IssueSend = true;

            // Getting tracking token value
            wod_GetTrackingTokenEmailResponse = (GetTrackingTokenEmailResponse)
                                                 prmCrmService.Execute (wod_GetTrackingTokenEmailRequest);

            // Setting tracking token value
            wod_SendEmailRequest.TrackingToken = wod_GetTrackingTokenEmailResponse.TrackingToken;

            // Sending email
            prmCrmService.Execute(wod_SendEmailRequest);

        }