简体   繁体   中英

Stop propagation of SendingMail event in .NET PasswordRecovery control

I have a PasswordRecovery control whose behavior I need to modify. When the SendingMail event is fired, the event handler is called. In the event handler, I have defined behavior to write a MailMessage to a queue (for processing later). So far, so good. The problem is the event continues on and tries to use the built-in method LoginUtil.SendPasswordMail . This requires smtp server information, which I don't want to provide (since it would violate the design of our application).

Here's the event handler:

protected void vendorWebPasswordRecovery_SendingMail(object sender, MailMessageEventArgs e)
{
    string userName = this.vendorWebPasswordRecovery.UserName;

    try
    {
        MailMessage emailMessage = e.Message;

        var messageData = new Dictionary<string, string>();

        messageData.Add("toEmail", emailMessage.To[0].ToString());
        messageData.Add("subject", emailMessage.Subject + " from the queue!");
        messageData.Add("body", emailMessage.Body);

        //Write the message out to the queue
        SendPasswordResetEmail(messageData, userName);
    }
    catch (Exception ex)
    {
        LoggingHandler.Write("MailMessageEventArgs did not contain expected data: - " + ex.Message, LogCategoryType.Verbose, TraceEventType.Verbose, 1, ex);
    }                

    //Compilation error: "MailMessageEventArgs doesn't contain a definition for 'Handled'"
    e.Handled = true;
}

How can I stop the event from continuing on and calling the built-in method? I tried setting the MailMessageEventArgs to handled, but that's a compilation error. Is this even the right approach?

You should use e.Cancel to cancel the event and stop sending default email.

Sample here: scottonwriting.net/sowblog/archive/2009/11/10/163371.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM