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

        }



17 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. You don need do anything but click here to get free prescription glasses. I believe you never have such a great chance to get free glasses. Hurry up.

    ReplyDelete
  8. En outre, vous pouvez obtenir un style minimaliste dans votre maison. Ceci est robinets vente en ayant deux Robinet opposé atteint. Vous commandés par pression de la poignée et de contr?le de la température. Ces robinets cascade ont une grande variété. Ils sont de la bonne taille et la forme, vous pouvez choisir. Bidet classe mitigeur et le luxe que vous touchez vraiment, vous devez déplacer votre baignoire. Il existe une robinetterie baignoire de baignoire, quand un bon match, vous pouvez compléter votre spa est incroyable. Lors de l'achat rénové salle de bains baignoire robinet ou le remplacement existent. Il ya toujours un droit robinets LED de bidet suite de vos grands, des suites d'un vestiaire naturelles ou petits. Ils renforcent également organiser votre espace de bains. En achetant ces mitigeur douche, mais vous devez être prudent, parce que les différents fournisseurs ont différents types de robinets, peut ne pas être adapté à vos besoins. Attention, dans ce cas, ne pas aveuglément acheter pas.

    ReplyDelete
  9. 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
  10. As a general rule, you should remember low pressure Taps Sale will work well with a high pressure water system; however, Basin Taps is not the same vice versa. In addition to seeking Bathroom Sink Taps that look good and offer great performance, Bathtub Taps should endeavor to get ones that are reasonably priced. By taking the time to compare the various options available you should be able to get fantastic Automatic Taps for your bath and basin that you will love for many years to come.

    ReplyDelete
  11. 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
  12. Automatic taps also called "hands-free" taps. Some of them needs batteries. Choosing a automatic tap for your bathroom or kitchen can let your house more comfortable. It also can solve water and power.Find Cheap Automatic Taps at http://www.cheaptap.co.uk/automatic-taps-c-2.html Online Store, Well designed free hands antomatic taps sale with large discounts and fast shipping.

    ReplyDelete
  13. Exquisite petal pink morganite center stones are in full bloom in these stunning Morganite Rings . Sought after for their exceptional clarity and captivating blush tones, these gems are accentuated perfectly by delicate settings which allow for maximum light to enter the stone.

    ReplyDelete