public HttpResponseMessage AddRecipientsToListFromList(int sourceListId, int destinationListId)
        {
            RecipientList destList = RecipientList.Load(destinationListId);
            RecipientList sourceList = RecipientList.Load(sourceListId);
            if (destList != null && sourceList != null)
            {
                int addedAddresses = destList.AddRecipientItemsFromRecipientList(sourceList.Id);
                RecipientStatus status = new RecipientStatus {ImportedEmails = addedAddresses};

                return Request.CreateResponse(HttpStatusCode.OK,
                    GetJsonResult<RecipientStatus>(status));
            }

            return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Source or Destination list not found");
        }
        public HttpResponseMessage AddRecipientsToJobFromList(int sourceListId, int jobId)
        {
            Job job = Job.Load(jobId);
            if (job == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Job not found");
            }
            RecipientList sourceList = RecipientList.Load(sourceListId);
            if (sourceList == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Source list not found");
            }

            int addedAddresses = job.AddWorkItemsFromRecipientList(sourceList.Id);
            RecipientStatus status = new RecipientStatus {ImportedEmails = addedAddresses};

            return Request.CreateResponse(HttpStatusCode.OK, GetJsonResult<RecipientStatus>(status));
        }
        protected RecipientStatus AddEPiServerGroupRecipients(IEmailImporter importer, string groupName)
        {
            RecipientStatus status = new RecipientStatus();

            List<string> addresses = new List<string>();
            string[] usersInRole = Roles.GetUsersInRole(groupName);

            foreach (string userName in usersInRole)
            {
                MembershipUser user = Membership.GetUser(userName);
                if (user != null && string.IsNullOrEmpty(user.Email) == false)
                    addresses.Add(user.Email);
            }

            if (addresses.Count == 0)
            {
                status.Status = "Could not find any email addresses for users in the EPiServer group.";
                return status;
            }

            // Add the items
            List<string> duplicateAddresses;
            List<string> invalidAddresses;

            System.Diagnostics.Stopwatch tmr = System.Diagnostics.Stopwatch.StartNew();
            int count = importer.ImportEmailAddresses(addresses.ToArray(), out invalidAddresses, out duplicateAddresses);
            tmr.Stop();

            if (invalidAddresses.Count > 0)
            {
                status.InvalidMessage = string.Join(", ", invalidAddresses.ToArray());
            }

            status.ImportedEmails = count;
            status.DuplicatedEmails = duplicateAddresses.Count;
            status.InvalidEmails = invalidAddresses.Count;
            status.TimeToImport = tmr.ElapsedMilliseconds;
            status.Status = "Import ok";

            return status;
        }