private static void FormsEventHandler(IFormEntryCreatedEvent eventInfo)
        {
            var controls = eventInfo.Controls;

            FormsManager         man            = FormsManager.GetManager();
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            List <string>        emails         = new List <string>();

            foreach (var item in controls.Where(c => man.LoadControl(man.GetControl <ObjectData>(c.Id)) is EmailDistributionDropDown))
            {
                var data = jsonSerializer.Deserialize <List <DistributionObject> >((man.LoadControl(man.GetControl <ObjectData>(item.Id)) as EmailDistributionDropDown).DistributionList);
                var d    = data.Where(i => i.Text == item.Value.ToString()).FirstOrDefault();
                foreach (var emailItem in d.Email.Split(','))
                {
                    emails.Add(emailItem);
                }
            }
            if (Config.Get <FormsConfig>().Notifications.Enabled)
            {
                List <ISubscriberRequest> subscribers = new List <ISubscriberRequest>();
                var  ns                 = SystemManager.GetNotificationService();
                var  serviceContext     = new ServiceContext("ThisApplicationKey", "Forms");
                Guid subscriptionListId = eventInfo.FormSubscriptionListId;
                IEnumerable <ISubscriberRequest> currentSubscribers = ns.GetSubscribers(serviceContext, subscriptionListId, null);
                var currentSubscribersLookup = currentSubscribers.ToLookup(s => s.Email);
                IDictionary <string, string> notificationTemplateItems = GetNotificationTemplateItems(eventInfo);

                foreach (var item in emails.Where(r => !currentSubscribersLookup.Contains(r)))
                {
                    subscribers.Add(new SubscriberRequestProxy()
                    {
                        Email = item, ResolveKey = item
                    });
                }

                MessageJobRequestProxy messageJobRequestProxy = new MessageJobRequestProxy()
                {
                    Subscribers       = subscribers,
                    MessageTemplateId = Config.Get <FormsConfig>().Notifications.FormEntrySubmittedNotificationTemplateId,
                    SenderProfileName = Config.Get <FormsConfig>().Notifications.SenderProfile,
                    Description       = string.Format("Form entry submission email notification for form '{0}'", eventInfo.FormTitle)
                };

                // Make it so, Number One.
                ns.SendMessage(serviceContext, messageJobRequestProxy, notificationTemplateItems);
            }
        }
        public void CreateMessageJobAndSendForExecution(string message)
        {
            try
            {
                var ns                = SystemManager.GetNotificationService();
                var context           = new ServiceContext("*****@*****.**", "SuccessStories");
                var contextDictionary = new Dictionary <string, string>();

                var profileName = "Default";

                var subjectTemplate = "New Success Story submitted";
                var bodyTemplate    = message;

                var tmpl = new MessageTemplateRequestProxy()
                {
                    Subject  = subjectTemplate,
                    BodyHtml = bodyTemplate
                };

                List <ISubscriberRequest> subscribers = new List <ISubscriberRequest>();
                string[] notificationRecipients       = WebConfigurationManager.AppSettings["notificationRecipients"].Trim().Split(',');

                for (int i = 0; i < notificationRecipients.Length; i++)
                {
                    var subscriber = new SubscriberRequestProxy()
                    {
                        Email      = notificationRecipients[i],
                        ResolveKey = $"unique-identifier-in-the-specified-context-{i}"
                    };
                    subscribers.Add(subscriber);
                }

                IMessageJobRequest job = new MessageJobRequestProxy()
                {
                    MessageTemplate   = tmpl,
                    Subscribers       = subscribers,
                    SenderProfileName = profileName
                };

                ns.SendMessage(context, job, contextDictionary);
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            }
        }
示例#3
0
        /// <summary>
        /// Sends the email.
        /// </summary>
        /// <param name="fromEmail">From email.</param>
        /// <param name="toEmail">To email.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        public static void SendEmail(string fromEmail, string[] toEmail, string subject, string body)
        {
            // TODO: Find means of supporting email attachments with NotificationService.
            // TODO: Find way to prevent Sitefinity from persisting ad-hoc subscriber list in database indefinitely.
            var notificationService = SystemManager.GetNotificationService();
            var context             = new ServiceContext("GenericEmailSender", "Babaganoush");

            var messageTemplate = new MessageTemplateRequestProxy
            {
                Subject  = subject,
                BodyHtml = body
            };

            string             resolveKey = string.Format("{0}-{1}-{2}", context.AccountName, context.ModuleName, Guid.NewGuid());
            IMessageJobRequest job        = new MessageJobRequestProxy
            {
                MessageTemplate = messageTemplate,
                Subscribers     = toEmail.Select(email => new SubscriberRequestProxy {
                    Email = email, ResolveKey = resolveKey
                })
            };

            notificationService.SendMessage(context, job, new Dictionary <string, string>());
        }