private Dictionary <string, object> GetSettings(dynamic contentModel, int templateId, MailCommunicationPart part)
        {
            var data = new Dictionary <string, object>();

            var baseUri = new Uri(_orchardServices.WorkContext.CurrentSite.BaseUrl);

            Dictionary <string, object> similViewBag = new Dictionary <string, object>();

            similViewBag.Add("CampaignLink", _communicationService.GetCampaignLink("Email", part));

            string body = _templateService.RitornaParsingTemplate(contentModel, templateId, similViewBag);

            if (!body.StartsWith("Error On Template"))
            {
                string host = string.Format("{0}://{1}{2}",
                                            baseUri.Scheme,
                                            baseUri.Host,
                                            baseUri.Port == 80 ? string.Empty : ":" + baseUri.Port);

                var urlHelper = _commonServices.GetUrlHelper();


                // Add Link [UNSUBSCRIBE]
                string ph_Unsubscribe  = "[UNSUBSCRIBE]";
                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>";

                if (body.Contains(ph_Unsubscribe))
                {
                    body = body.Replace(ph_Unsubscribe, linkUnsubscribe);
                }
                else
                {
                    body += "<br /><br />" + linkUnsubscribe;
                }

                var    subject  = contentModel.TitlePart.Title;
                var    smtp     = _orchardServices.WorkContext.CurrentSite.As <SmtpSettingsPart>();
                string priority = "L";
                switch (_mailerConfig.MailPriority)
                {
                case MailPriorityValues.High:
                    priority = "H";
                    break;

                case MailPriorityValues.Normal:
                    priority = "N";
                    break;

                default:
                    priority = "L";
                    break;
                }

                var baseUrl = baseUri.ToString();
                // token di sicurezza: contiene data e ora (senza minuti e secondi) e id del content item
                var token = string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHH"), (contentModel as ContentItem).Id);
                token = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(token));
                //var url = string.Format("{0}/Laser.Orchard.MailCommunication/MailerResult?tk={1}", baseUrl, token);  // versione per il GET
                var tenantPrefix = GetTenantUrlPrexix(_shellSettings);
                var url          = string.Format("{0}/{1}api/Laser.Orchard.MailCommunication/MailerResultAPI?tk={2}", baseUrl, tenantPrefix, token); // versione per il POST

                data.Add("Subject", subject);
                data.Add("Body", body);
                data.Add("Sender", smtp.Address);
                data.Add("Priority", priority);
                data.Add("Url", url);        // url di ritorno per comunicare a Orchard il numero di mail inviate con successo
                data.Add("Attachments", ""); // TODO esempio: "[\"prova.pdf\",\"prova.docx\"]" 2016-01-14: per ora non li gestiamo
            }

            return(data);
        }
        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"));
                    }
                }
            });
        }