You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Episerver技术问询:如何在EventsPublishedContent事件中发送通知消息

在Episerver的EventsPublishedContent事件中发送通知消息的实现方法

我来给你详细讲讲怎么在Episerver的EventsPublishedContent事件里发送通知消息,步骤很清晰,跟着做就行:

1. 创建事件初始化模块,订阅PublishedContent事件

Episerver的内容事件需要在模块初始化时完成订阅,我们可以创建一个实现IInitializableModule的类,让系统启动时自动注册事件监听:

using EPiServer;
using EPiServer.Core;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;

namespace YourProjectNamespace
{
    [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class PublishedContentNotificationHandler : IInitializableModule
    {
        private IContentEvents _contentEvents;
        private INotificationService _notificationService;

        public void Initialize(InitializationEngine context)
        {
            // 从服务定位器获取所需服务实例
            _contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
            _notificationService = ServiceLocator.Current.GetInstance<INotificationService>();

            // 订阅PublishedContent事件,绑定处理方法
            _contentEvents.PublishedContent += HandleContentPublished;
        }

        private void HandleContentPublished(object sender, ContentEventArgs e)
        {
            // 过滤出你需要监听的内容类型,比如你的StandardPage
            if (e.Content is StandardPage publishedPage)
            {
                // 定制通知内容,可包含页面名称、发布时间、发布者等信息
                var notificationText = $"页面「{publishedPage.PageName}」已完成发布,发布时间:{System.DateTime.Now:yyyy-MM-dd HH:mm:ss}";
                
                // 调用自定义通知服务发送消息
                _notificationService.SendNotification(notificationText, "Episerver页面发布通知");
            }
        }

        public void Uninitialize(InitializationEngine context)
        {
            // 取消事件订阅,避免内存泄漏
            if (_contentEvents != null)
            {
                _contentEvents.PublishedContent -= HandleContentPublished;
            }
        }
    }
}

2. 实现自定义通知服务

接下来需要一个实际发送消息的通知服务,这里以邮件通知为例,你也可以根据需求改成Slack、Teams或者Episerver内部消息:

using System.Net;
using System.Net.Mail;
using EPiServer.ServiceLocation;

namespace YourProjectNamespace
{
    // 注册为服务,方便依赖注入
    [ServiceConfiguration(typeof(INotificationService))]
    public class EmailNotificationService : INotificationService
    {
        public void SendNotification(string message, string subject)
        {
            // 配置你的SMTP服务器信息
            using var smtpClient = new SmtpClient("your-smtp-server-address")
            {
                Port = 587,
                Credentials = new NetworkCredential("your-email-account", "your-email-password"),
                EnableSsl = true
            };

            // 构建邮件内容
            using var mailMessage = new MailMessage
            {
                From = new MailAddress("episerver-notifications@yourdomain.com"),
                Subject = subject,
                Body = message,
                IsBodyHtml = false
            };
            // 添加接收人邮箱,支持多个
            mailMessage.To.Add("admin@yourdomain.com");
            mailMessage.To.Add("content-editor@yourdomain.com");

            // 发送邮件
            smtpClient.Send(mailMessage);
        }
    }

    // 定义通知服务接口,方便后续扩展其他通知方式
    public interface INotificationService
    {
        void SendNotification(string message, string subject);
    }
}

额外注意事项

  • 避免内存泄漏:一定要在Uninitialize方法中取消事件订阅,否则模块卸载时会有内存泄漏风险。
  • 定制通知内容:你可以从ContentEventArgs中获取更多信息,比如发布者(e.Content.CreatedBy)、内容链接(e.ContentLink)等,让通知更详细。
  • 处理批量发布:如果是批量发布内容,e.Content可能是批量操作中的单个内容,你可以根据需求调整逻辑,比如批量汇总通知。
  • 扩展通知方式:除了邮件,你可以实现INotificationService的其他子类,比如发送Slack消息、Teams消息,甚至是Episerver后台的用户弹窗通知。

内容的提问来源于stack exchange,提问作者Ayo Adesina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:26:24