public void SendNewsletterEdition(ref NewsletterEditionPart newsletterEdition, string testEmail)
        {
            var urlHelper   = new UrlHelper(_orchardServices.WorkContext.HttpContext.Request.RequestContext);
            var subscribers = GetSubscribers(newsletterEdition.NewsletterDefinitionPartRecord_Id).Where(w => w.Confirmed);

            int[] selectedAnnIds;
            IList <AnnouncementPart>    items = null;
            IEnumerable <ExpandoObject> fullyItems;

            if (!String.IsNullOrWhiteSpace(newsletterEdition.AnnouncementIds))
            {
                selectedAnnIds = !String.IsNullOrWhiteSpace(newsletterEdition.AnnouncementIds) ? newsletterEdition.AnnouncementIds.Split(',').Select(s => Convert.ToInt32(s)).ToArray() : null;
                items          = GetAnnouncements(selectedAnnIds);
                fullyItems     = items.Select(
                    s => new {
                    AnnouncementPart = s,
                    DisplayUrl       = urlHelper.ItemDisplayUrl(s)
                }.ToExpando());
            }
            else
            {
                fullyItems = null;
            }
            var model = new {
                NewsletterEdition = newsletterEdition,
                ContentItems      = fullyItems
            }.ToExpando();

            //if (!isTest) {
            //  if (_templateService.SendTemplatedEmail((dynamic)model,
            //                                        GetNewsletterDefinition(newsletterEdition.NewsletterDefinitionPartRecord_Id,VersionOptions.Published).As<NewsletterDefinitionPart>().TemplateRecord_Id,
            //                                        new List<string> { testEmail }, null, null, false, null)) {

            //        // Aggiorno la newsletter edition, e rimuovo la relazione tra Newletter e Announcement
            //        newsletterEdition.Dispatched = true;
            //        newsletterEdition.DispatchDate = DateTime.Now;
            //        newsletterEdition.Number = GetNextNumber(newsletterEdition.NewsletterDefinitionPartRecord_Id); ;
            //        foreach (var item in items) {
            //            var ids = ("," + item.AttachToNextNewsletterIds + ",").Replace("," + newsletterEdition.NewsletterDefinitionPartRecord_Id + ",", "");
            //            item.AttachToNextNewsletterIds = ids;
            //        }
            //    }
            //}
            if (!String.IsNullOrWhiteSpace(testEmail))
            {
                string host = string.Format("{0}://{1}{2}",
                                            _orchardServices.WorkContext.HttpContext.Request.Url.Scheme,
                                            _orchardServices.WorkContext.HttpContext.Request.Url.Host,
                                            _orchardServices.WorkContext.HttpContext.Request.Url.Port == 80 ? string.Empty : ":" + _orchardServices.WorkContext.HttpContext.Request.Url.Port);

                // Place Holder
                List <TemplatePlaceHolderViewModel> listaPH = new List <TemplatePlaceHolderViewModel>();

                string unsubscribe     = T("Click here to unsubscribe").Text;
                string linkUnsubscribe = "<a href='" + string.Format("{0}{1}?newsletterId={2}", host, urlHelper.SubscriptionUnsubscribe(), newsletterEdition.NewsletterDefinitionPartRecord_Id) + "'>" + unsubscribe + "</a>";

                TemplatePlaceHolderViewModel ph = new TemplatePlaceHolderViewModel();
                ph.Name      = "[UNSUBSCRIBE]";
                ph.Value     = linkUnsubscribe;
                ph.ShowForce = true;

                listaPH.Add(ph);

                if (_templateService.SendTemplatedEmail((dynamic)model,
                                                        GetNewsletterDefinition(newsletterEdition.NewsletterDefinitionPartRecord_Id, VersionOptions.Published).As <NewsletterDefinitionPart>().TemplateRecord_Id,
                                                        new List <string> {
                    testEmail
                }, null, null, false, listaPH))
                {
                    _orchardServices.Notifier.Information(T("Newsletter edition sent to a test email!"));
                }
            }
            else if (String.IsNullOrWhiteSpace(testEmail))
            {
                _orchardServices.Notifier.Error(T("Enter a test email!"));
            }
        }
        public MailCommunicationPartHandler(IControllerContextAccessor controllerContextAccessor, INotifier notifier, ITemplateService templateService, IOrchardServices orchardServices, IMailCommunicationService mailCommunicationService,
                                            IRepository <CommunicationEmailRecord> repoMail, IRepository <TitlePartRecord> repoTitle, ITransactionManager transactionManager, ICommunicationService communicationService, ShellSettings shellSetting, IScheduledTaskManager taskManager)
        {
            _repoMail  = repoMail;
            _repoTitle = repoTitle;
            _controllerContextAccessor = controllerContextAccessor;
            _transactionManager        = transactionManager;
            _notifier        = notifier;
            _templateService = templateService;
            _orchardServices = orchardServices;

            _mailCommunicationService = mailCommunicationService;
            _communicationService     = communicationService;
            _shellSettings            = shellSetting;
            _taskManager = taskManager;

            T = NullLocalizer.Instance;
            OnUpdated <MailCommunicationPart>((context, part) => {
                if (_orchardServices.WorkContext.HttpContext.Request.Form["submit.Save"] == "submit.MailTest")
                {
                    if (part.SendToTestEmail && part.EmailForTest != string.Empty)
                    {
                        dynamic content = context.ContentItem;

                        string host = string.Format("{0}://{1}{2}",
                                                    _orchardServices.WorkContext.HttpContext.Request.Url.Scheme,
                                                    _orchardServices.WorkContext.HttpContext.Request.Url.Host,
                                                    _orchardServices.WorkContext.HttpContext.Request.Url.Port == 80 ? string.Empty : ":" + _orchardServices.WorkContext.HttpContext.Request.Url.Port);

                        var urlHelper = new UrlHelper(_orchardServices.WorkContext.HttpContext.Request.RequestContext);

                        Dictionary <string, object> similViewBag = new Dictionary <string, object>();
                        similViewBag.Add("CampaignLink", _communicationService.GetCampaignLink("Email", part));

                        // Place Holder
                        List <TemplatePlaceHolderViewModel> listaPH = new List <TemplatePlaceHolderViewModel>();

                        string unsubscribe     = T("Click here to stop receiving email for commercial use").Text;
                        string linkUnsubscribe = "<a href='" + string.Format("{0}{1}", host, urlHelper.UnsubscribeMailCommunication()) + "'>" + unsubscribe + "</a>";

                        TemplatePlaceHolderViewModel ph = new TemplatePlaceHolderViewModel();
                        ph.Name      = "[UNSUBSCRIBE]";
                        ph.Value     = linkUnsubscribe;
                        ph.ShowForce = true;

                        listaPH.Add(ph);

                        if (((Laser.Orchard.TemplateManagement.Models.CustomTemplatePickerPart)content.CustomTemplatePickerPart).SelectedTemplate != null)
                        {
                            _templateService.SendTemplatedEmail(content,
                                                                ((Laser.Orchard.TemplateManagement.Models.CustomTemplatePickerPart)content.CustomTemplatePickerPart).SelectedTemplate.Id,
                                                                null,
                                                                new List <string> {
                                part.EmailForTest
                            },
                                                                similViewBag,
                                                                false, listaPH);

                            _notifier.Information(T("Sent e-mail test!"));
                        }
                        else
                        {
                            _notifier.Error(T("Select or create a template for e-mail"));
                        }
                    }
                }
            });
            OnPublished <MailCommunicationPart>((context, part) => {
                if (part.SendOnNextPublish && !part.MailMessageSent)
                {
                    ContentItem ci  = _orchardServices.ContentManager.Get(part.ContentItem.Id);
                    dynamic content = ci;
                    if (((Laser.Orchard.TemplateManagement.Models.CustomTemplatePickerPart)content.CustomTemplatePickerPart).SelectedTemplate != null)
                    {
                        _taskManager.CreateTask("Laser.Orchard.MailCommunication.Task", DateTime.UtcNow.AddMinutes(1), ci);
                        part.MailMessageSent = true;
                    }
                    else
                    {
                        _notifier.Error(T("Select or create a template for e-mail"));
                    }
                }
            });
        }