Hunny Software Library Reference

SmtpClient.Cancel Method 

Cancels a currently executing command.

[Visual Basic]
Public Sub Cancel()
[C#]
public void Cancel();

Remarks

Since it is possible for a network connection to stop responding, it is important in interactive applications to have a way to cancel a command that is waiting on a network operation (connect, send, or receive). For this reason, the SmtpClient class provides a Cancel method, which promptly terminates any currently executing client command.

Cancel is the only method that is safe to call from another thread. In a typical situation, you have one thread that executes SMTP commands, and another thread that serves the user interface. A user action to cancel is serviced by the user interface thread, which calls Cancel on the SmtpClient instance to cancel a command. The command method may then throw an instance of Hunny.Mail.Exception with the value of ErrorCode set to ErrorCode.TimedOut. (It will throw the exception if the cancel operation occurred before the command was completed.)

Because it is difficult to recover the client/server state after a cancel operation, the library closes the connection whenever you call the Cancel method. You must call Connect again to reconnect to the SMTP server. The Connect method should not be called from the same thread thread that called Cancel.

Note: Since all network operations will timeout, you do not need to use Cancel in non-interactive applications. Instead, you can just let the timeout timer stop any stalled network operation.

See Also

SmtpClient Class | Hunny.Mail Namespace