0
votes

I am trying to send an email to the administrator when a new user is created in the CMS. But when I create a new user while debugging in VS, the first breakpoint at "umbraco.BusinessLogic.User.New += User_New;" is never hit. I am using version 7.3.4 of Umbraco.

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);
    } 
  }
}
1

1 Answers

2
votes

I think because you are using ApplicationEventHandler, you'll want to override the ApplicationStarted method instead of use your constructor.

The way I used needs 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);
} 

Further reading here.