我可能是錯的,但如果你是用SmtpClient.SendAsync在ASP.NET 2.0 工作,它拋出一個異常,線程處理請求等待 無限期地操作完成。SmtpClient.SendAsync錯誤2.0
若要重現此問題,只需使用主機 發送電子郵件時無法解析的無效SMTP地址。
請注意,您應該設置Page.Async = true以使用SendAsync。
如果Page.Async設置爲false並且發送異常,線程 不會阻止,並且頁面處理正確。
TIA。
我可能是錯的,但如果你是用SmtpClient.SendAsync在ASP.NET 2.0 工作,它拋出一個異常,線程處理請求等待 無限期地操作完成。SmtpClient.SendAsync錯誤2.0
若要重現此問題,只需使用主機 發送電子郵件時無法解析的無效SMTP地址。
請注意,您應該設置Page.Async = true以使用SendAsync。
如果Page.Async設置爲false並且發送異常,線程 不會阻止,並且頁面處理正確。
TIA。
請注意,您應該設置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
,那麼,你應該異步發送郵件。
這是我的。試一試。
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.");
}
}
}
RegisterAsyncTask無法使用。 看那BeginEventHandler委託:
公衆委託的IAsyncResult BeginEventHandler( 對象發件人, EventArgs的, AsyncCallback的CB, 而額外對象 )
它應該返回一個IAsyncResult。 現在看SmtpClient.SendAsync功能:
公共無效SendAsync( 消息MAILMESSAGE, 對象userToken )
沒有返回值。
無論如何,這是工作正常,只要SmtpClient.SendAsync不會引發異常。