2

I am trying to fix the behavior of a CreateUserWizard control in ASP.NET 2.0. With a fairly naive, out-of-the-box implementation, if you enter an email address that does not exist, or there is some other error sending the email, you get a YSOD showing the ugly details of the SMTP error, plus the user account is created anyway. Handling the SendMailError does not seem to help, as it is fired after the user is already created.

Ideally, an email error would cause an "invalid email address" error message (or something to that effect) to be displayed. Seems like this should be pretty easy, but after quite a bit of looking around I haven't found an answer. Anybody have any solutions?

Eric Pohl
  • 2,324
  • 1
  • 21
  • 31

2 Answers2

2

This is what I do. It's not perfect, but it helps.

protected void CreateUserWizard1_SendMailError(object sender, SendMailErrorEventArgs e)
{
    // e.Exception can be one of the exceptions generated from SmtpClient.Send(MailMessage)
    if (e.Exception is SmtpFailedRecipientException)
    {
        // The message could not be delivered to one or more of the recipients in To, CC, or BCC()()().
        // TODO: Set an error message on the page
        e.Handled = true;

        // Since the user has already been created at this point, we need to remove them.
        Membership.DeleteUser(CreateUserWizard1.UserName);

        // Set an internal flag for use in the ActiveStepChanged event.
        emailFailed = true;

        return;
    }
}

protected void CreateUserWizard1_ActiveStepChanged(object sender, EventArgs e)
{
    if (CreateUserWizard1.ActiveStep != CreateUserWizard1.CompleteStep)
        return;

    if (emailFailed)
    {
        // If the email failed, keep the user on the first step.
        CreateUserWizard1.ActiveStepIndex = 0;
        return;
    }
}
Greg
  • 16,540
  • 9
  • 51
  • 97
0

You cannot in that way; you would need to use what most businesses do: send a confirmation email that they must click on in X number of hours, and only then creating the account. THis is a modest change from the way the CUW works, so you'd have to break away somewhat from the base functionality and tap into events to prevent the default functionality for that.

HTH.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • True enough, but how to let the user know that the email could not be sent? – Eric Pohl Mar 03 '10 at 13:58
  • Look at some of the API's out there, like GMAIL, or see if Exchange has the feature, because there isn't an event that fires when the email fails, a delivery failure gets sent back to you, and you would have to process that message to figure that out. That could be days/weeks later, that's the issue. – Brian Mains Mar 03 '10 at 15:32