web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

proper way to send email from plugin?

(0) ShareShare
ReportReport
Posted on by

Hello - Is this the proper way to send an email from a plugin?:

var message = "test";
var emailId = service.Create(new Email
{
To = new List<ActivityParty>
{
new ActivityParty
{
PartyId = new EntityReference(Contact.EntityLogicalName, contactId)
}
},
Subject = message,
Description = message,
RegardingObjectId = new EntityReference(Incident.EntityLogicalName, caseId)
});

Should this code alone be enough to send an email from a plugin or is a different technique or more code needed?

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    You need to call a SendEmailRequest after you created the email record:

    SendEmailRequest sendEmailreq = new SendEmailRequest {
    EmailId = _emailId,
    TrackingToken = "",
    IssueSend = true
    };
    SendEmailResponse sendEmailresp = (SendEmailResponse)_serviceProxy.Execute(sendEmailreq);

    http://msdn.microsoft.com/en-us/library/hh210217.aspx

  • Community Member Profile Picture
    on at

    Unfortunately, service.Execute() is returning an exception: "an unknown error occurred"

    Is this a common error (with known solution(s)) for vets working with SendEmailRequest?

  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    I think you are missing the From attribute, however this is a complete plugin (tested, it uses late bound but is the same, sender and receiver are the current user and the regarding object is an account, I registered this plugin in Create Account Post Synchronous)

    public class EMailPlugin : IPlugin
    {
       public void Execute(IServiceProvider serviceProvider)
       {
          IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
          if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
          {
             IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
             IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
             Entity account = (Entity)context.InputParameters["Target"];

             Entity email = new Entity("email");
             Entity fromParty = new Entity("activityparty");
             Entity toParty = new Entity("activityparty");

             toParty["partyid"] = new EntityReference("systemuser", context.UserId);
             fromParty["partyid"] = new EntityReference("systemuser", context.UserId);
             email["from"] = new Entity[] { fromParty };
             email["to"] = new Entity[] { toParty };

             email["subject"] = "email subject - " + DateTime.Now.ToString();
             email["description"] = "email description";
             email["regardingobjectid"] = new EntityReference("account", account.Id);

             Guid emailId = service.Create(email);
             SendEmailRequest sendEmailreq = new SendEmailRequest
             {
                EmailId = emailId,
                TrackingToken = "",
                IssueSend = true
             };
             SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);
          }
       }
    }

  • ShaikhAtif Profile Picture
    201 on at

    i am using crm 2015 and I facing problem on this line:

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

    is there missing anything ?

  • Guido Preite Profile Picture
    54,086 Moderator on at

    can you be more specific about the error you get?

  • Community Member Profile Picture
    on at

    Hello everyone,

    I want to create an email and send it simultaneously to the recipient when update spme fields of Incident entity via plugin, My question is: What is the right way to do it?, Is Post operation or Pre operation?

    I would appreciate your answer,

  • Stanley Lai Profile Picture
    295 on at

    I would use Post operation otherwise you will get an error saying 'record with Id = XXXXXXX does not exist' if yo try to perform a SendEmailRequest on Pre operation

  • Community Member Profile Picture
    on at

    I want to forward received emails including all attachments, description, subject to another email.

    My understanding is that I may need to create a plugin or custom workflow. I could see a code on this page but experiencing problem editing it. Please help with the modification so that it will use the received description, subject and all attachments to send a new  email:

    public class EMailPlugin : IPlugin

    {

      public void Execute(IServiceProvider serviceProvider)

      {

         IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

         if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

         {

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            Entity account = (Entity)context.InputParameters["Target"];

            Entity email = new Entity("email");

            Entity fromParty = new Entity("activityparty");

            Entity toParty = new Entity("activityparty");

            toParty["partyid"] = new EntityReference("systemuser", context.UserId);

            fromParty["partyid"] = new EntityReference("systemuser", context.UserId);

            email["from"] = new Entity[] { fromParty };

            email["to"] = new Entity[] { toParty };

            email["subject"] = "email subject - " + DateTime.Now.ToString();

            email["description"] = "email description";

            email["regardingobjectid"] = new EntityReference("account", account.Id);

            Guid emailId = service.Create(email);

            SendEmailRequest sendEmailreq = new SendEmailRequest

            {

               EmailId = emailId,

               TrackingToken = "",

               IssueSend = true

            };

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

         }

      }

    }

  • Gmanunta81 Profile Picture
    327 on at

    Hello Guido,

    for me the code is working, but I don't receive/see in my outlook 2016 client email box the email.

    On CRM for each email created I see this message :

    1) This message has not yet been submitted for delivery. For more information, see help.

    the plugin runs on the User Context of the system administrator (that is also the sender: [from : SystemAdmin])

    Can you help me ?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans