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);

        }



16 comments:

  1. thank you! was really helpful
    Alain

    ReplyDelete
  2. Thank you for this. I was unaware that from and to fields were activityparties, so I tried to give it an EntityReference with little results. Changed it to activityparties and it worked.

    ReplyDelete
  3. For your information,
    i tried to put HTML content (instead of text) in the wod_EmailEntity["description"] and it works!

    ReplyDelete
  4. Do you know if there is a way to avoid sending the trackingtoken ?

    ReplyDelete
  5. Very well, the publish is actually the greatest on this deserving topic. I agree with your conclusions and will eagerly look forward to your incoming updates. Just saying thanks will not just be adequate, for the fantastic clarity in your writing. I will at once grab your rss feed to stay informed of any updates.

    ReplyDelete
  6. Admiring the time and effort you put into your website and detailed information you provide. It’s awesome to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

    ReplyDelete
  7. La Robinet infrarouge proposent les designers actuels associés aux dernières innovations qui raviront les amateurs de robinetsale.com des meilleurs prix sur notre large gamme de robinetterie dédiée à la cuisine. Mitigeurs évier tous robinet pour un équipement de qualité.Des équipements de qualité pour votre maison, et vous pouvez faire la cuisine qualité fiable robinet, vraiment dans votre chambre? Vous cherchez un robinet pour votre service de cuisine, nous avons ce qu'il vous faut! Robinetsale.com conçu pour fournir toutes sortes de Accessoires salle de bain, nous pouvons concevoir une variété de styles disponibles ici. Mitigeurs de cuisine Robinetsale : Vous trouverez ici le Mitigeur baignoire Mitigeur baignoire . Peu importe qui ouvre le robinet : ici coulent la qualité, le savoir-faire et le design.

    ReplyDelete
  8. Pour acheter votre robinet de lavabo pas cher, pour bénéficier du meilleur prix pour l'achat de votre robinet de lavabo.http://www.robinetso.fr/5-robinet-de-lavabo

    ReplyDelete
  9. Thanks for taking the time to discuss that, I feel strongly about this and so really like getting to know more on this kind of field. Do you mind updating your blog post with additional insight? It should be really useful for all of us.
    Bathroom Sink Taps

    ReplyDelete
  10. I am Kajal Verma,hey you can book at whatsapp also so if you want to book then come here:- ??????

    ReplyDelete
  11. This code connects to a CRM instance, creates an email entity, sets the sender and recipients, and then sends the email. Ootdtak Kot Pernah Adjust the regardingObjectId, toRecipients, subject, and body variables to match your specific requirements. Remember to replace the connection string with your CRM details.

    ReplyDelete