SmtpClient.SendCompleted Event
.NET Framework 3.0
Occurs when an asynchronous e-mail send operation completes.
Namespace: System.Net.Mail
Assembly: System (in system.dll)
Assembly: System (in system.dll)
public: event SendCompletedEventHandler^ SendCompleted { void add (SendCompletedEventHandler^ value); void remove (SendCompletedEventHandler^ value); }
/** @event */ public void add_SendCompleted (SendCompletedEventHandler value) /** @event */ public void remove_SendCompleted (SendCompletedEventHandler value)
In JScript, you can handle the events defined by a class, but you cannot define your own.
Not applicable.
The SendCompleted event is raised each time an e-mail message is sent asynchronously when the send operation completes. To send an e-mail message asynchronously, use the SendAsync methods.
SendCompletedEventHandler is the delegate for SendCompleted. The AsyncCompletedEventArgs class provides the event handler with event data.
The following code example demonstrates sending an e-mail message asynchronously.
#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 the the SMTP host. SmtpClient^ client = gcnew SmtpClient(args[1]); // Specify the e-mail 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 e-mail message. MailAddress^ to = gcnew MailAddress("ben@contoso.com"); // Specify the message content. MailMessage^ message = gcnew 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 = 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!"); } }
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: