Umbraco ApplicationEventHandler不为用户射击

时间:2016-01-19 14:54:16

标签: umbraco

我正在尝试在CMS中创建新用户时向管理员发送电子邮件。但是当我在VS中调试时创建一个新用户时,第一个断点位于“umbraco.BusinessLogic.User.New + = User_New;”从未被击中过。我正在使用Umbraco的7.3.4版本。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web
using Umbraco.Core;
using umbraco.BusinessLogic;

namespace NewUserEmail
{
    /// <summary>
    /// Summary description for NewUserNotification
    /// </summary>
    public class NewUserNotification : ApplicationEventHandler
    {
        public NewUserNotification()
        {
            umbraco.BusinessLogic.User.New += User_New;
        }

    private void User_New(umbraco.BusinessLogic.User sender, EventArgs e)
    {
        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
        message.To.Add("nw@email.com");
        message.Subject = "This is the Subject line";
        message.From = new System.Net.Mail.MailAddress("From@online.microsoft.com");
        message.Body = "This is the message body";
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
        smtp.Send(message);
    } 
  }
}

1 个答案:

答案 0 :(得分:2)

我认为因为您正在使用ApplicationEventHandler,所以您需要覆盖ApplicationStarted方法而不是使用构造函数。

我使用的方式需要using Umbraco.Core.Services;

protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
    //I usually use Members for things like this but if you want user, it'll be UserService.SavedUser +=...
    MemberService.Saved += User_New;   
}

private void User_New(IMemberService sender, EventArgs e)
{
    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
    message.To.Add("nw@email.com");
    message.Subject = "This is the Subject line";
    message.From = new System.Net.Mail.MailAddress("From@online.microsoft.com");
    message.Body = "This is the message body";
    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
    smtp.Send(message);
} 

进一步阅读here