2016-01-19 20 views
0

我試圖在CMS中創建新用戶時向管理員發送電子郵件。但是,當我在VS中調試時創建新用戶時,「umbraco.BusinessLogic.User.New + = User_New」中的第一個斷點;從未被擊中。我正在使用Umbraco的7.3.4版本。Umbraco ApplicationEventHandler不爲用戶解僱

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("[email protected]"); 
     message.Subject = "This is the Subject line"; 
     message.From = new System.Net.Mail.MailAddress("[email protected]"); 
     message.Body = "This is the message body"; 
     System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost"); 
     smtp.Send(message); 
    } 
    } 
} 

回答

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("[email protected]"); 
    message.Subject = "This is the Subject line"; 
    message.From = new System.Net.Mail.MailAddress("[email protected]"); 
    message.Body = "This is the message body"; 
    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost"); 
    smtp.Send(message); 
} 

更多讀物here

+0

這工作,我不知道UserService。非常感謝你! – NickWojo531