locked
Send E-mail by setting Multiple user in 'To' in CRM 2011 Plugin RRS feed

  • Question

  • Hello Experts,

    I have done some Plugin code ,but in that the requirement is like to send the same mail to multiple users of particular security roles.

    I have successfully fetched users , now i want to send a single mail to those users.

    private void EmailUser(IOrganizationService service, EntityCollection _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 toParty = new Entity("activityparty");
                for (int i = 0; i < _toUserId.Entities.Count; i++)
                {
                    if (_toUserId[i] != null && _toUserId[i].Id != null)
                    {
    
                        toParty.Attributes.Add(_toUserId[i].LogicalName, _toUserId[i].Id);
    
                        //EmailUser(localContext.OrganizationService, toUsers[i].Id, fromUser[0].Id);
                    }
                }
    
                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);
            }

    What should i change in my above code. ?>

    Any help would be appreciated.

    Monday, March 10, 2014 9:53 AM

Answers

  • Hi,

    You can add multiple user to your ToParty object.

     EntityCollection to = new EntityCollection();
                    string[] ids = null;
                    ids = ConfigurationManager.AppSettings["ids"].ToString().Split(',');
                    foreach (string id in ids)
                    {
                        Entity toActivityparty = new Entity("activityparty");
                        toActivityparty["partyid"] = new EntityReference("systemuser", new Guid(id));
                        to.Entities.Add(ccActivityparty);
                    }
                    emailEntity["to"] = to;



    Gopinath

    mscrmtechie.blogspot.com



    Monday, March 10, 2014 10:09 AM
  • Hi,

    Find below the full code snippet for sending emails to multiple users.

                    EntityCollection entCollectionAsActivityPartyOfUsers = new EntityCollection();
    
                    //Loop To Itearate Number Of Fetched Users
                    //Change the loop according to your logic
                    foreach (string userId in useridList)
                    {
    		   //Query each user to check if the email address is present or not. If you will not check then it will fail while sending
                        QueryExpression retrieveQuery = new QueryExpression("systemuser");
                        retrieveQuery.ColumnSet.AddColumn("internalemailaddress");
                        retrieveQuery.Criteria.AddCondition("systemuserid", ConditionOperator.Equal, new Guid(userId));
                        EntityCollection collUsers = service.RetrieveMultiple(retrieveQuery);
    					
                        if (collUsers.Entities.Count > 0)
                        {
                            if (collUsers.Entities[0].Contains("internalemailaddress") && collUsers.Entities[0]["internalemailaddress"] != null)
                            {
                                //Creating Activity Party
                                Entity toParty = new Entity("activityparty");
                                toParty["partyid"] = new EntityReference(strEntUser, new Guid(userId));
                                entCollectionAsActivityPartyOfUsers.Entities.Add(toParty);
                            }
                        }
                    }
    
                    if (entCollectionAsActivityPartyOfUsers.Entities.Count > 0)
                    {
                        //Creating Email Object
                        Entity email = new Entity();
                        email.LogicalName = "email";
                        email[strEmailSubject] = "Subject";
                       
                        //Setting FROM In The Email Object
                        Entity fromParty = new Entity("activityparty");
                        fromParty["partyid"] = configOwner;
    
                        //Setting TO In The Email Object
                        email[strEmailTo] = entCollectionAsActivityPartyOfUsers;
                        email[strEmailFrom] = new Entity[] { fromParty };
    
                        Guid emailID = service.Create(email);
    					
                        if (emailID != null)
                        {
                            SendEmailRequest req = new SendEmailRequest();
                            req.EmailId = emailID;
                            req.IssueSend = true;
                            req.TrackingToken = "";
    
                            SendEmailResponse res = (SendEmailResponse)service.Execute(req);
                        }
                    }


    Thanks, MS CRM Developer, Ranjan


    • Edited by Ranjan_P Monday, March 10, 2014 10:16 AM
    • Marked as answer by DynamicsCRM31 Monday, March 10, 2014 12:06 PM
    Monday, March 10, 2014 10:12 AM

All replies

  • Hi,

    You can add multiple user to your ToParty object.

     EntityCollection to = new EntityCollection();
                    string[] ids = null;
                    ids = ConfigurationManager.AppSettings["ids"].ToString().Split(',');
                    foreach (string id in ids)
                    {
                        Entity toActivityparty = new Entity("activityparty");
                        toActivityparty["partyid"] = new EntityReference("systemuser", new Guid(id));
                        to.Entities.Add(ccActivityparty);
                    }
                    emailEntity["to"] = to;



    Gopinath

    mscrmtechie.blogspot.com



    Monday, March 10, 2014 10:09 AM
  • Hi,

    Find below the full code snippet for sending emails to multiple users.

                    EntityCollection entCollectionAsActivityPartyOfUsers = new EntityCollection();
    
                    //Loop To Itearate Number Of Fetched Users
                    //Change the loop according to your logic
                    foreach (string userId in useridList)
                    {
    		   //Query each user to check if the email address is present or not. If you will not check then it will fail while sending
                        QueryExpression retrieveQuery = new QueryExpression("systemuser");
                        retrieveQuery.ColumnSet.AddColumn("internalemailaddress");
                        retrieveQuery.Criteria.AddCondition("systemuserid", ConditionOperator.Equal, new Guid(userId));
                        EntityCollection collUsers = service.RetrieveMultiple(retrieveQuery);
    					
                        if (collUsers.Entities.Count > 0)
                        {
                            if (collUsers.Entities[0].Contains("internalemailaddress") && collUsers.Entities[0]["internalemailaddress"] != null)
                            {
                                //Creating Activity Party
                                Entity toParty = new Entity("activityparty");
                                toParty["partyid"] = new EntityReference(strEntUser, new Guid(userId));
                                entCollectionAsActivityPartyOfUsers.Entities.Add(toParty);
                            }
                        }
                    }
    
                    if (entCollectionAsActivityPartyOfUsers.Entities.Count > 0)
                    {
                        //Creating Email Object
                        Entity email = new Entity();
                        email.LogicalName = "email";
                        email[strEmailSubject] = "Subject";
                       
                        //Setting FROM In The Email Object
                        Entity fromParty = new Entity("activityparty");
                        fromParty["partyid"] = configOwner;
    
                        //Setting TO In The Email Object
                        email[strEmailTo] = entCollectionAsActivityPartyOfUsers;
                        email[strEmailFrom] = new Entity[] { fromParty };
    
                        Guid emailID = service.Create(email);
    					
                        if (emailID != null)
                        {
                            SendEmailRequest req = new SendEmailRequest();
                            req.EmailId = emailID;
                            req.IssueSend = true;
                            req.TrackingToken = "";
    
                            SendEmailResponse res = (SendEmailResponse)service.Execute(req);
                        }
                    }


    Thanks, MS CRM Developer, Ranjan


    • Edited by Ranjan_P Monday, March 10, 2014 10:16 AM
    • Marked as answer by DynamicsCRM31 Monday, March 10, 2014 12:06 PM
    Monday, March 10, 2014 10:12 AM
  • Thank You !!!

    You saved my day  !!! :-)

    Monday, March 10, 2014 12:07 PM