.NET3.5中SendAsync()发生了什么
What is happening in SendAsync () in.NET3.5
所以在我的程序中,我正在使用 .NET 3.5 中的 STMPClient 构建并发送电子邮件。由于我无法控制的情况,我必须使用 .NET 3.5。我选择使用 SendAysnc() 发送电子邮件。如
SmtpClient client = new SmtpClient(emailsrvr, emailport);
smtpclient.Credentials = //Credentials from server
MailMessage email = new MailMessage();
/*
Skipping lines
*/
Object x;
smtpClient.SendAsync(email, x);
该方法调用 MailMessage 和对象。在 MSDN 文档示例中,他们使用字符串变量。在我的代码中,我尝试使用 CallBack 函数,该函数将日志中发送的电子邮件记录为对象,但这不是对象。当我给它任何其他东西时,例如字符串或整数变量,我可以毫无问题地发送电子邮件。
我正在尝试查看对象的使用情况并更好地了解正在发生的事情。
https://msdn.microsoft.com/en-us/library/x5x13z6h(v=vs.110).aspx
userToken: A user-defined object that is passed to the method invoked when the asynchronous operation completes
您根本不必使用该参数。你可以通过null
。在没有更多上下文的情况下不确定推荐什么。
In another project I worked on, I sent HTTP request asynchronously using the WebRequest.BeginGetResponse that asked for an AsyncCallback function that I used to process the results of the GetResponse.
那是 WebRequest
,它使用“Asynchronous Programming Model (APM)" (using XxxBegin(
and passing in a callback or calling the XxxEnd(
method). SmtpClient uses the "Event-based Asynchronous Pattern (EAP)”(运行 一种立即 returns 并等待事件触发的方法)。
SendAsync
中的第二个参数的作用是让您知道哪个方法引发了事件,用它完成的所有操作都会传递给事件处理程序。如果您不对多个事件处理程序使用相同的方法,您可以直接将 null
传递给它,无需声明变量。
这是一个有用的例子
void Example()
{
SmtpClient client1 = new SmtpClient(emailsrvr, emailport);
client1.SendCompleted += OnSendCompleted;
SmtpClient client2 = new SmtpClient(emailsrvr, emailport);
client2.SendCompleted += OnSendCompleted;
MailMessage message1 = //...
MailMessage message2 = //...
client1.SendAsync(message1, "1");
client2.SendAsync(message2, "2");
}
void OnSendCompleted(object sender, AsyncCompletedEventArgs args)
{
Console.WriteLine("Message {0} sent!", args.UserState);
}
//Prints out:
//Message 1 sent!
//Message 2 sent!
对于你的代码,因为你没有订阅 SendCompleted
你可以传递 null。
SmtpClient client = new SmtpClient(emailsrvr, emailport);
smtpclient.Credentials = //Credentials from server
MailMessage email = new MailMessage();
/*
Skipping lines
*/
smtpClient.SendAsync(email, null);
所以在我的程序中,我正在使用 .NET 3.5 中的 STMPClient 构建并发送电子邮件。由于我无法控制的情况,我必须使用 .NET 3.5。我选择使用 SendAysnc() 发送电子邮件。如
SmtpClient client = new SmtpClient(emailsrvr, emailport);
smtpclient.Credentials = //Credentials from server
MailMessage email = new MailMessage();
/*
Skipping lines
*/
Object x;
smtpClient.SendAsync(email, x);
该方法调用 MailMessage 和对象。在 MSDN 文档示例中,他们使用字符串变量。在我的代码中,我尝试使用 CallBack 函数,该函数将日志中发送的电子邮件记录为对象,但这不是对象。当我给它任何其他东西时,例如字符串或整数变量,我可以毫无问题地发送电子邮件。
我正在尝试查看对象的使用情况并更好地了解正在发生的事情。
https://msdn.microsoft.com/en-us/library/x5x13z6h(v=vs.110).aspx
userToken: A user-defined object that is passed to the method invoked when the asynchronous operation completes
您根本不必使用该参数。你可以通过null
。在没有更多上下文的情况下不确定推荐什么。
In another project I worked on, I sent HTTP request asynchronously using the WebRequest.BeginGetResponse that asked for an AsyncCallback function that I used to process the results of the GetResponse.
那是 WebRequest
,它使用“Asynchronous Programming Model (APM)" (using XxxBegin(
and passing in a callback or calling the XxxEnd(
method). SmtpClient uses the "Event-based Asynchronous Pattern (EAP)”(运行 一种立即 returns 并等待事件触发的方法)。
SendAsync
中的第二个参数的作用是让您知道哪个方法引发了事件,用它完成的所有操作都会传递给事件处理程序。如果您不对多个事件处理程序使用相同的方法,您可以直接将 null
传递给它,无需声明变量。
这是一个有用的例子
void Example()
{
SmtpClient client1 = new SmtpClient(emailsrvr, emailport);
client1.SendCompleted += OnSendCompleted;
SmtpClient client2 = new SmtpClient(emailsrvr, emailport);
client2.SendCompleted += OnSendCompleted;
MailMessage message1 = //...
MailMessage message2 = //...
client1.SendAsync(message1, "1");
client2.SendAsync(message2, "2");
}
void OnSendCompleted(object sender, AsyncCompletedEventArgs args)
{
Console.WriteLine("Message {0} sent!", args.UserState);
}
//Prints out:
//Message 1 sent!
//Message 2 sent!
对于你的代码,因为你没有订阅 SendCompleted
你可以传递 null。
SmtpClient client = new SmtpClient(emailsrvr, emailport);
smtpclient.Credentials = //Credentials from server
MailMessage email = new MailMessage();
/*
Skipping lines
*/
smtpClient.SendAsync(email, null);