views:

2979

answers:

4

Hello, I may be wrong, but if you are working with SmtpClient.SendAsync in ASP.NET 2.0 and it throws an exception, the thread processing the request waits indefinitely for the operation to complete.

To reproduce this problem, simply use an invalid SMTP address for the host that could not be resolved when sending an email.

Note that you should set Page.Async = true to use SendAsync.

If Page.Async is set to false and Send throws an exception the thread does not block, and the page is processed correctly.

TIA.

+2  A: 
bzlm
A: 

There was a typo in the last sentence of my question. You should read "If Page.Async is set to false and Send throws an exception..."

SendAsync requires Page.Async to be set to true.

omatrot
You know you can edit your own questions and answers?
Eduardo Molteni
A: 

Here is mine. Give it a try.

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("[email protected]",
           "SOMEONE" + (char)0xD8 + " SOMEWHERE",
        System.Text.Encoding.UTF8);
        // Set destinations for the e-mail message.
        MailAddress to = new MailAddress("[email protected]");
        // 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.");
        }
    }

}
omatrot
+1  A: 

RegisterAsyncTask could not be used. Look at the BeginEventHandler delegate:

public delegate IAsyncResult BeginEventHandler( Object sender, EventArgs e, AsyncCallback cb, Object extraData )

It should return an IAsyncResult. Now look at the SmtpClient.SendAsync function :

public void SendAsync( MailMessage message, Object userToken )

There is no return value.

Anyway this is working fine, as long as SmtpClient.SendAsync does not throw an exception.

omatrot