Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Send Email Using Plugin

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hello,


I am writing an plugin to send an email to a user.  Does anyone know how I could do this?  I have seen articles referencing ActivityParty, but I was not able to add the correct reference to use this class.

Any ideas?


Thanks,


Devin

*This post is locked for comments

  • ShaikhAtif Profile Picture
    ShaikhAtif 201 on at
    RE: Send Email Using Plugin

    Aileen,

    i having error on this line :

    SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail); //error line

    reference :

    SendEmailRequest reqSendEmail = new SendEmailRequest();

                                   reqSendEmail.EmailId = _emailId;//ID of created mail

                                   reqSendEmail.TrackingToken = "";

                                   reqSendEmail.IssueSend = true;

    SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Send Email Using Plugin

    ANSWER:

    I was able to get the email to send.  One has go into the User profile in CRM, and click on "APPROVE EMAIL"

     

    This needs to be part of the documentation.  It should be more obvious that the email mailbox needs to be approved before emails can be sent via code.  Workflows send emails fine, but anything externally will not send unless you have approved the email address!

     

     

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Send Email Using Plugin

    Call to method:

    sendEmail(new Guid("7891B00E-ED80-E211-B2EA-02BFAC1A6434"), owner.Key, "systemuser", "Test Email", "Test Description", owner.Value);

    Method:

    private void sendEmail(Guid From, Guid To, string ToEntitySchemaName, string Subject, string Description, List<string> urls)

       {

           var description = new StringBuilder();

           description.AppendLine("Hello,");

           description.AppendLine("Text");

           description.AppendLine("<ul>");

           foreach (var address in urls) {

               description.AppendFormat("<li>{0}</li>", string.Format(HyperLinkFormat, address, "Link to things"));

           }

           description.AppendLine("</ul>");

           try

           {

               // Create 'From' activity party for the email

               ActivityParty fromParty = new ActivityParty

               {

                   PartyId = new EntityReference(SystemUser.EntityLogicalName, From)

               };

               // Create 'To' activity party for the email

               ActivityParty toParty = new ActivityParty

               {

                   PartyId = new EntityReference(ToEntitySchemaName, To)

               };

               // Create an e-mail message

               Email email = new Email

               {

                   To = new ActivityParty[] { toParty },

                   From = new ActivityParty[] { fromParty },

                   Subject = Subject,

                   Description = description.ToString(),

                   DirectionCode = true

               };

               Guid _emailId = _service.Create(email);

               SendEmailRequest sendEmailreq = new SendEmailRequest

               {

                   EmailId = _emailId,

                   TrackingToken = "",

                   IssueSend = true

               };

               SendEmailResponse sendEmailresp = (SendEmailResponse)_service.Execute(sendEmailreq);

               if (sendEmailresp != null)

               {

                   //Console.WriteLine("Email record created successfully");

                   //Console.ReadKey();

               }

           }

           catch (Exception ex)

           {

               //Console.Write("Errorex.Message);

               //Console.ReadKey();

               throw new InvalidPluginExecutionException(ex.Message);

           }

       }

  • Aileen Gusni Profile Picture
    Aileen Gusni 44,524 on at
    RE: Send Email Using Plugin

    Devin,

    Can you copy paste your code here?

    It seems you copy paste a systemuser guid but use entity type other than allowed entities (user and queue)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Send Email Using Plugin

    Hello,

    I have implemented the code that was found in the SDK to send an email.  However I am getting an error upon debugging the code.

    "the specified sender type is not supported"

    I am passing a guid of a systemuser in CRM.

  • Cornel Croitoriu Profile Picture
    Cornel Croitoriu 50 on at
    RE: Send Email Using Plugin

    You can use the SendEmailRequest class. The partyId and type are mandatory.

  • Suggested answer
    Karth Profile Picture
    Karth on at
    RE: Send Email Using Plugin

    There's sample code available within CRM SDK. You can reference the below link

    msdn.microsoft.com/.../hh210217.aspx

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,622 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans