2010-03-02 29 views
2

我想修復ASP.NET 2.0中CreateUserWizard控件的行爲。如果您輸入的電子郵件地址不存在,或者發送電子郵件時發生其他錯誤,您將收到一個顯示SMTP錯誤的醜陋細節的YSOD,以及無論如何創建用戶帳戶。處理SendMailError似乎沒有幫助,因爲在之後用戶已被創建,所以它被觸發CreateUserWizard - 防止用戶創建,如果確認電子郵件無法發送

理想情況下,電子郵件錯誤會導致顯示「無效電子郵件地址」錯誤消息(或其他影響)。似乎這應該很容易,但經過相當多的環顧四周,我還沒有找到答案。任何人有任何解決方案?

回答

2

這就是我所做的。這並不完美,但它有幫助。

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; 
    } 
} 
0

你不能這樣;您需要使用大多數企業所做的工作:發送確認電子郵件,他們必須在X個小時內點擊,然後才能創建該帳戶。這與CUW的工作方式相比略有改變,因此您必須從基本功能中脫離出來,然後利用事件來防止默認功能。

HTH。

+0

確實夠了,但是如何讓用戶知道電子郵件無法發送? –

+0

查看一些API,例如GMAIL,或者查看Exchange是否具有該功能,因爲沒有事件在電子郵件失敗時觸發,傳遞失敗將被髮回給您,並且您必須處理那個消息來解決這個問題。這可能是幾天/幾周後,這就是問題所在。 –