ASP.NET 2.0中的SmtpClient.SendAsync错误

时间:2008-09-17 06:58:23

标签: asynchronous smtp asp.net-2.0

我可能错了,但是如果你在ASP.NET中使用SmtpClient.SendAsync 2.0并且它抛出异常,处理请求的线程等待 无限期地完成操作。

要重现此问题,只需为主机使用无效的SMTP地址即可 发送电子邮件时无法解决。

请注意,您应将Page.Async = true设置为使用SendAsync。

如果Page.Async设置为false,则Send会抛出该线程的异常 不会阻止,页面处理正确。

TIA。

3 个答案:

答案 0 :(得分:2)

<击>

<击>
  

请注意,您应将Page.Async = true设置为使用SendAsync。

请解释一下这背后的理由。误解Page.Async可能是导致问题的原因。

抱歉,我无法找到一个能够重现问题的例子。

请参阅http://msdn.microsoft.com/en-us/magazine/cc163725.aspx(WICKED CODE:ASP.NET 2.0中的异步页面)

编辑:查看代码示例,我发现您没有使用RegisterAsyncTask()PageAsyncTask类。我认为您必须在@Async设置为true的页面上执行异步任务时执行此操作。 MSDN杂志的例子如下:

protected void Page_Load(object sender, EventArgs e)
{
    PageAsyncTask task = new PageAsyncTask(
        new BeginEventHandler(BeginAsyncOperation),
        new EndEventHandler(EndAsyncOperation),
        new EndEventHandler(TimeoutAsyncOperation),
        null
        );
    RegisterAsyncTask(task);
}

BeginAsyncOperation内,您应该异步发送邮件。

答案 1 :(得分:1)

无法使用RegisterAsyncTask。 查看BeginEventHandler委托:

公共委托IAsyncResult BeginEventHandler(     对象发送者,     EventArgs e,     AsyncCallback cb,     对象extraData )

它应该返回一个IAsyncResult。 现在看一下SmtpClient.SendAsync函数:

public void SendAsync(     MailMessage消息,     对象userToken )

没有返回值。

无论如何,只要SmtpClient.SendAsync不会抛出异常,这个工作正常。

答案 2 :(得分:0)

这是我的。试一试。

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Using an incorrect SMTP server
        SmtpClient client = new SmtpClient(@"smtp.nowhere.private");
        // Specify the e-mail sender.
        // Create a mailing address that includes a UTF8 character
        // in the display name.
        MailAddress from = new MailAddress("someone@somewhere.com",
           "SOMEONE" + (char)0xD8 + " SOMEWHERE",
        System.Text.Encoding.UTF8);
        // Set destinations for the e-mail message.
        MailAddress to = new MailAddress("someone@somewhere.com");
        // Specify the message content.
        MailMessage message = new MailMessage(from, to);
        message.Body = "This is a test e-mail message sent by an application. ";
        // Include some non-ASCII characters in body and subject.
        string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
        message.Body += Environment.NewLine + someArrows;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.Subject = "test message 1" + someArrows;
        message.SubjectEncoding = System.Text.Encoding.UTF8;
        // Set the method that is called back when the send operation ends.
        client.SendCompleted += new
        SendCompletedEventHandler(SendCompletedCallback);
        // The userState can be any object that allows your callback 
        // method to identify this send operation.
        // For this example, the userToken is a string constant.
        string userState = "test message1";
        try
        {
            client.SendAsync(message, userState);
        }
        catch (System.Net.Mail.SmtpException ex)
        {
            Response.Write(string.Format("Send Error [{0}].", ex.InnerException.Message));
        }
        finally
        {
        }
    }
    private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
    {
        // Get the unique identifier for this asynchronous operation.
        String token = (string)e.UserState;

        if (e.Cancelled)
        {
            Response.Write(string.Format("[{0}] Send canceled.", token));
        }
        if (e.Error != null)
        {
            Response.Write(string.Format("[{0}] {1}", token, e.Error.ToString()));
        }
        else
        {
            Response.Write("Message sent.");
        }
    }

}