Umbraco ApplicationEventHandler Tidak Diaktifkan Untuk Pengguna

Saya mencoba mengirim email ke administrator ketika pengguna baru dibuat di CMS. Tetapi ketika saya membuat pengguna baru saat melakukan debug di VS, breakpoint pertama di "umbraco.BusinessLogic.User.New += User_New;" tidak pernah terkena. Saya menggunakan Umbraco versi 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("[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);
    } 
  }
}

person NickWojo531    schedule 19.01.2016    source sumber


Jawaban (1)


Saya pikir karena Anda menggunakan ApplicationEventHandler, Anda sebaiknya mengganti metode ApplicationStarted daripada menggunakan konstruktor Anda.

Cara yang saya gunakan membutuhkan 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);
} 

Bacaan lebih lanjut di sini.

person Matthew Allen    schedule 19.01.2016
comment
Berhasil, saya tidak tahu tentang UserService. Terima kasih banyak! - person NickWojo531; 19.01.2016