locked
How to send an email to multiple user through Plugin in CRM 2011 ? RRS feed

  • Question

  • Hello Experts,

    I am trying to send an email to users on creation of some Entity in my Organization.

    But i ended with to send the same email to the users individually.

    But i need to send the same all in one email.

    My Plugin code is as below.

    Can anyone say what i need to change ?

    protected void ExecutePostPlugInTestCreate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                var fromUser = RetrieveUsersInfoByRoleAdmin(localContext.OrganizationService);
                var toUsers = RetrieveUsersInfoByRole(localContext.OrganizationService);
                for (int i = 0; i < toUsers.Entities.Count; i++)
                {
                    if (toUsers[i] != null && toUsers[i].Id != null)
                    {
                        EmailUser(localContext.OrganizationService, toUsers[i].Id, fromUser[0].Id);
                    }
                }
    
                // TODO: Implement your custom Plug-in business logic.
            }
    
            private void EmailUser(IOrganizationService service, Guid _toUserId, Guid _fromUserId)
            {
                Entity email = new Entity("email");
                email.Attributes.Add("subject", "New Region has been Created.");
                email.Attributes.Add("description", "Create Business Units.");
    
                EntityReference from = new EntityReference("systemuser", _fromUserId);
                EntityReference to = new EntityReference("systemuser", _toUserId);
    
                Entity fromParty = new Entity("activityparty");
                fromParty.Attributes.Add("partyid", from);
                Entity toParty = new Entity("activityparty");
                toParty.Attributes.Add("partyid", to);
    
                EntityCollection frmPartyCln = new EntityCollection();
                frmPartyCln.EntityName = "systemuser";
                frmPartyCln.Entities.Add(fromParty);
    
                EntityCollection toPartyCln = new EntityCollection();
                toPartyCln.EntityName = "systemuser";
                toPartyCln.Entities.Add(toParty);
    
                email.Attributes.Add("from", frmPartyCln);
                email.Attributes.Add("to", toPartyCln);
    
                //Create an EMail Record
                Guid _emailId = service.Create(email);
    
                // Use the SendEmail message to send an e-mail message.
                SendEmailRequest sendEmailreq = new SendEmailRequest
                {
                    EmailId = _emailId,
                    TrackingToken = "",
                    IssueSend = true
                };
    
                SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);
            }



    Thursday, March 6, 2014 6:01 AM

Answers

  • It looks like you are retrieving your recipient list and then passing them one at a time to the private void EmailUser() method through a loop.  The method then takes the one recipient it is receiving and generates a new email every time it is called.

    You may want to move the section that retrieves the recipients down into the private void EmailUser() method so that this method is only called once.


    Jason Peterson

    • Marked as answer by DynamicsCRM31 Wednesday, March 12, 2014 10:28 AM
    Thursday, March 6, 2014 10:24 AM

All replies

  • Anyone ?

    Thursday, March 6, 2014 6:45 AM
  • It looks like you are retrieving your recipient list and then passing them one at a time to the private void EmailUser() method through a loop.  The method then takes the one recipient it is receiving and generates a new email every time it is called.

    You may want to move the section that retrieves the recipients down into the private void EmailUser() method so that this method is only called once.


    Jason Peterson

    • Marked as answer by DynamicsCRM31 Wednesday, March 12, 2014 10:28 AM
    Thursday, March 6, 2014 10:24 AM
  • Can you do the same in my code ?

    Because i am getting error like the key is already present.

    Thursday, March 6, 2014 10:52 AM