SmtpClient.SendAsyncCancel 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
取消异步操作以发送电子邮件。
public:
void SendAsyncCancel();
public void SendAsyncCancel ();
member this.SendAsyncCancel : unit -> unit
Public Sub SendAsyncCancel ()
例外
已释放此对象。
示例
下面的代码示例演示如何以异步方式发送电子邮件。 如果用户尚未发送邮件,可以选择取消邮件。
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::Net::Mail;
using namespace System::Net::Mime;
using namespace System::Threading;
using namespace System::ComponentModel;
static bool mailSent;
static void SendCompletedCallback(Object^ sender, AsyncCompletedEventArgs^ e)
{
// Get the unique identifier for this asynchronous
// operation.
String^ token = (String^) e->UserState;
if (e->Cancelled)
{
Console::WriteLine("[{0}] Send canceled.", token);
}
if (e->Error != nullptr)
{
Console::WriteLine("[{0}] {1}", token,
e->Error->ToString());
} else
{
Console::WriteLine("Message sent.");
}
mailSent = true;
}
int main(array<String^>^ args)
{
if (args->Length > 1)
{
// Command-line argument must be the SMTP host.
SmtpClient^ client = gcnew SmtpClient(args[1]);
// Specify the email sender.
// Create a mailing address that includes a UTF8
// character in the display name.
MailAddress^ from = gcnew MailAddress("jane@contoso.com",
"Jane " + (wchar_t)0xD8 + " Clayton",
System::Text::Encoding::UTF8);
// Set destinations for the email message.
MailAddress^ to = gcnew MailAddress("ben@contoso.com");
// Specify the message content.
MailMessage^ message = gcnew MailMessage(from, to);
message->Body = "This is a test email message sent" +
" by an application. ";
// Include some non-ASCII characters in body and
// subject.
String^ someArrows = gcnew String(gcnew array<wchar_t>{L'\u2190',
L'\u2191', L'\u2192', L'\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 += gcnew
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";
client->SendAsync(message, userState);
Console::WriteLine("Sending message... press c to" +
" cancel mail. Press any other key to exit.");
String^ answer = Console::ReadLine();
// If the user canceled the send, and mail hasn't been
// sent yet,then cancel the pending operation.
if (answer->ToLower()->StartsWith("c") && mailSent == false)
{
client->SendAsyncCancel();
}
// Clean up.
delete message;
client = nullptr;
Console::WriteLine("Goodbye.");
}
else
{
Console::WriteLine("Please give SMTP server name!");
}
}
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;
namespace Examples.SmtpExamples.Async
{
public class SimpleAsynchronousExample
{
static bool mailSent = false;
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string) e.UserState;
if (e.Cancelled)
{
Console.WriteLine("[{0}] Send canceled.", token);
}
if (e.Error != null)
{
Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
} else
{
Console.WriteLine("Message sent.");
}
mailSent = true;
}
public static void Main(string[] args)
{
// Command-line argument must be the SMTP host.
SmtpClient client = new SmtpClient(args[0]);
// Specify the email sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("jane@contoso.com",
"Jane " + (char)0xD8+ " Clayton",
System.Text.Encoding.UTF8);
// Set destinations for the email message.
MailAddress to = new MailAddress("ben@contoso.com");
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test email 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";
client.SendAsync(message, userState);
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
string answer = Console.ReadLine();
// If the user canceled the send, and mail hasn't been sent yet,
// then cancel the pending operation.
if (answer.StartsWith("c") && mailSent == false)
{
client.SendAsyncCancel();
}
// Clean up.
message.Dispose();
Console.WriteLine("Goodbye.");
}
}
}
Imports System.Net
Imports System.Net.Mail
Imports System.Net.Mime
Imports System.Threading
Imports System.ComponentModel
Namespace Examples.SmtpExamples.Async
Public Class SimpleAsynchronousExample
Private Shared mailSent As Boolean = False
Private Shared Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
' Get the unique identifier for this asynchronous operation.
Dim token As String = CStr(e.UserState)
If e.Cancelled Then
Console.WriteLine("[{0}] Send canceled.", token)
End If
If e.Error IsNot Nothing Then
Console.WriteLine("[{0}] {1}", token, e.Error.ToString())
Else
Console.WriteLine("Message sent.")
End If
mailSent = True
End Sub
Public Shared Sub Main(ByVal args() As String)
' Command line argument must the SMTP host.
Dim client As New SmtpClient(args(0))
' Specify the email sender.
' Create a mailing address that includes a UTF8 character
' in the display name.
Dim mailFrom As New MailAddress("jane@contoso.com", "Jane " & ChrW(&HD8) & " Clayton", System.Text.Encoding.UTF8)
' Set destinations for the email message.
Dim mailTo As New MailAddress("ben@contoso.com")
' Specify the message content.
Dim message As New MailMessage(mailFrom, mailTo)
message.Body = "This is a test email message sent by an application. "
' Include some non-ASCII characters in body and subject.
Dim someArrows As New String(New Char() {ChrW(&H2190), ChrW(&H2191), ChrW(&H2192), ChrW(&H2193)})
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.
AddHandler client.SendCompleted, AddressOf 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.
Dim userState As String = "test message1"
client.SendAsync(message, userState)
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.")
Dim answer As String = Console.ReadLine()
' If the user canceled the send, and mail hasn't been sent yet,
' then cancel the pending operation.
If answer.StartsWith("c") AndAlso mailSent = False Then
client.SendAsyncCancel()
End If
' Clean up.
message.Dispose()
Console.WriteLine("Goodbye.")
End Sub
End Class
End Namespace
注解
SendAsyncCancel使用 方法取消挂起SendAsync的操作。 如果有邮件等待发送,此方法将释放用于存储邮件的资源。 如果没有等待发送邮件,此方法不执行任何操作。