Umbraco ApplicationEventHandler 不为用户触发
Umbraco ApplicationEventHandler Not Firing For User
我正在尝试在 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);
}
}
}
我认为因为您使用的是 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。
我正在尝试在 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);
}
}
}
我认为因为您使用的是 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。