SmtpClient.Dispose 方法

定义

向 SMTP 服务器发送一条 QUIT 消息,适当地结束 TCP 连接,并释放由 SmtpClient 类的当前实例使用的所有资源。Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, and releases all resources used by the current instance of the SmtpClient class.

重载

Dispose()

向 SMTP 服务器发送一条 QUIT 消息,适当地结束 TCP 连接,并释放由 SmtpClient 类的当前实例使用的所有资源。Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, and releases all resources used by the current instance of the SmtpClient class.

Dispose(Boolean)

向 SMTP 服务器发送一条 QUIT 消息,适当地结束 TCP 连接,释放由 SmtpClient 类的当前实例使用的所有资源,并根据需要释放托管资源。Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, releases all resources used by the current instance of the SmtpClient class, and optionally disposes of the managed resources.

注解

如果应用程序希望向同一 SMTP 服务器发送多条消息,则可以重新使用 SmtpClient 类的当前实例建立到 SMTP 服务器的连接。The connection established by the current instance of the SmtpClient class to the SMTP server may be re-used if an application wishes to send multiple messages to the same SMTP server. 当使用身份验证或加密建立与 SMTP 服务器的连接时,此方法特别有用。This is particularly useful when authentication or encryption are used establish a connection to the SMTP server. 身份验证和建立 TLS 会话的过程可能是成本高昂的操作。The process of authenticating and establishing a TLS session can be expensive operations. 如果将大量电子邮件发送到相同的 SMTP 服务器,则为每条消息重新建立连接的要求可能会对性能产生重大影响。A requirement to re-establish a connection for each message when sending a large quantity of email to the same SMTP server could have a significant impact on performance. 有大量的大量电子邮件应用程序可发送电子邮件状态更新、新闻稿分发或电子邮件警报。There are a number of high-volume email applications that send email status updates, newsletter distributions, or email alerts. 此外,许多电子邮件客户端应用程序都支持脱机模式,用户可在此模式下撰写多封电子邮件,这些电子邮件稍后会在与 SMTP 服务器建立连接时发送。Also many email client applications support an off-line mode where users can compose many email messages that are sent later when a connection to the SMTP server is established. 通常,电子邮件客户端将所有 SMTP 邮件发送到特定的 SMTP 服务器(由 Internet 服务提供商提供),然后将此电子邮件转发给其他 SMTP 服务器。It is typical for an email client to send all SMTP messages to a specific SMTP server (provided by the Internet service provider) that then forwards this email to other SMTP servers.

SmtpClient 类实现对 SMTP 连接进行缓冲,使其可以避免在将每条消息重新建立到同一个服务器的连接时产生的开销。The SmtpClient class implementation pools SMTP connections so that it can avoid the overhead of re-establishing a connection for every message to the same server. 应用程序可以重复使用同一个 SmtpClient 对象将许多不同的电子邮件发送到相同的 SMTP 服务器和许多不同的 SMTP 服务器。An application may re-use the same SmtpClient object to send many different emails to the same SMTP server and to many different SMTP servers. 因此,无法确定何时使用 SmtpClient 对象完成应用程序的操作,并且应清理该对象。As a result, there is no way to determine when an application is finished using the SmtpClient object and it should be cleaned up.

当 SMTP 会话完成并且客户端希望终止连接时,它必须向服务器发送一条 QUIT 消息,指示它没有更多要发送的消息。When an SMTP session is finished and the client wishes to terminate the connection, it must send a QUIT message to the server to indicate that it has no more messages to send. 这允许服务器释放与客户端的连接关联的资源,并处理客户端发送的消息。This allows the server to free up resources associated with the connection from the client and process the messages which were sent by the client.

Dispose 方法将循环访问所有已建立的连接,并向每个 SMTP 服务器发送一条 QUIT 消息,然后正确地结束 TCP 连接。The Dispose methods iterates through all established connections and send a QUIT message to each SMTP server, followed by gracefully ending the TCP connection. 这些方法还释放 Socket 使用的非托管资源,并可以选择释放托管资源。These methods also release the unmanaged resources used by the Socket and optionally dispose of the managed resources.

Dispose()

向 SMTP 服务器发送一条 QUIT 消息,适当地结束 TCP 连接,并释放由 SmtpClient 类的当前实例使用的所有资源。Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, and releases all resources used by the current instance of the SmtpClient class.

public:
 virtual void Dispose();
public void Dispose ();
abstract member Dispose : unit -> unit
override this.Dispose : unit -> unit
Public Sub Dispose ()

实现

注解

如果应用程序希望向同一 SMTP 服务器发送多条消息,则可以重新使用 SmtpClient 类的当前实例建立到 SMTP 服务器的连接。The connection established by the current instance of the SmtpClient class to the SMTP server may be re-used if an application wishes to send multiple messages to the same SMTP server. 当使用身份验证或加密建立与 SMTP 服务器的连接时,此方法特别有用。This is particularly useful when authentication or encryption are used establish a connection to the SMTP server. 身份验证和建立 TLS 会话的过程可能是成本高昂的操作。The process of authenticating and establishing a TLS session can be expensive operations. 如果将大量电子邮件发送到相同的 SMTP 服务器,则为每条消息重新建立连接的要求可能会对性能产生重大影响。A requirement to re-establish a connection for each message when sending a large quantity of email to the same SMTP server could have a significant impact on performance. 有大量的大量电子邮件应用程序可发送电子邮件状态更新、新闻稿分发或电子邮件警报。There are a number of high-volume email applications that send email status updates, newsletter distributions, or email alerts. 此外,许多电子邮件客户端应用程序都支持脱机模式,用户可在此模式下撰写多封电子邮件,这些电子邮件稍后会在与 SMTP 服务器建立连接时发送。Also many email client applications support an off-line mode where users can compose many email messages that are sent later when a connection to the SMTP server is established. 通常,电子邮件客户端将所有 SMTP 邮件发送到特定的 SMTP 服务器(由 Internet 服务提供商提供),然后将此电子邮件转发给其他 SMTP 服务器。It is typical for an email client to send all SMTP messages to a specific SMTP server (provided by the Internet service provider) that then forwards this email to other SMTP servers.

SmtpClient 类实现对 SMTP 连接进行缓冲,使其可以避免在将每条消息重新建立到同一个服务器的连接时产生的开销。The SmtpClient class implementation pools SMTP connections so that it can avoid the overhead of re-establishing a connection for every message to the same server. 应用程序可以重复使用同一个 SmtpClient 对象将许多不同的电子邮件发送到相同的 SMTP 服务器和许多不同的 SMTP 服务器。An application may re-use the same SmtpClient object to send many different emails to the same SMTP server and to many different SMTP servers. 因此,无法确定何时使用 SmtpClient 对象完成应用程序的操作,并且应清理该对象。As a result, there is no way to determine when an application is finished using the SmtpClient object and it should be cleaned up.

当 SMTP 会话完成并且客户端希望终止连接时,它必须向服务器发送一条 QUIT 消息,指示它没有更多要发送的消息。When an SMTP session is finished and the client wishes to terminate the connection, it must send a QUIT message to the server to indicate that it has no more messages to send. 这允许服务器释放与客户端的连接关联的资源,并处理客户端发送的消息。This allows the server to free up resources associated with the connection from the client and process the messages which were sent by the client.

对执行异步 SendAsync 操作的 SmtpClient 对象调用 Dispose 方法将取消发送操作,就好像已调用 SendAsyncCancel 方法一样。Calling the Dispose method on a SmtpClient object that is executing an asynchronous SendAsync operation will cancel the send operation as though the SendAsyncCancel method had been called.

SmtpClient 类没有 Finalize 方法。The SmtpClient class has no Finalize method. 因此,应用程序必须调用 Dispose 才能显式释放资源。So an application must call Dispose to explicitly free up resources.

Dispose 方法可循环访问在 Host 属性中指定的 SMTP 服务器的所有已建立连接,并发送一条 QUIT 消息,并正确地结束 TCP 连接。The Dispose method iterates through all established connections to the SMTP server specified in the Host property and sends a QUIT message followed by gracefully ending the TCP connection. Dispose 方法还释放基础 Socket使用的非托管资源。The Dispose method also releases the unmanaged resources used by the underlying Socket.

在完成使用 Dispose 后,应调用 SmtpClientCall Dispose when you are finished using the SmtpClient. Dispose 方法使 SmtpClient 处于不可用状态。The Dispose method leaves the SmtpClient in an unusable state. 调用 Dispose后,必须释放对 SmtpClient 的所有引用,以便垃圾回收器可以回收 SmtpClient 占用的内存。After calling Dispose, you must release all references to the SmtpClient so the garbage collector can reclaim the memory that the SmtpClient was occupying.

有关详细信息,请参阅清理非托管资源实现 Dispose 方法For more information, see Cleaning Up Unmanaged Resources and Implementing a Dispose Method.

备注

每次释放对 Dispose 的最后一个引用前,均应调用 SmtpClientAlways call Dispose before you release your last reference to the SmtpClient. 否则,将不会释放它所使用的资源,因此垃圾回收器可以回收内存。Otherwise, the resources it is using will not be freed so the garbage collector can reclaim the memory.

Dispose(Boolean)

向 SMTP 服务器发送一条 QUIT 消息,适当地结束 TCP 连接,释放由 SmtpClient 类的当前实例使用的所有资源,并根据需要释放托管资源。Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, releases all resources used by the current instance of the SmtpClient class, and optionally disposes of the managed resources.

protected:
 virtual void Dispose(bool disposing);
protected virtual void Dispose (bool disposing);
abstract member Dispose : bool -> unit
override this.Dispose : bool -> unit
Protected Overridable Sub Dispose (disposing As Boolean)

参数

disposing
Boolean

如果释放托管资源和非托管资源,则为 true;如果仅释放非托管资源,则为 falsetrue to release both managed and unmanaged resources; false to releases only unmanaged resources.

注解

如果应用程序希望向同一 SMTP 服务器发送多条消息,则可以重新使用 SmtpClient 类的当前实例建立到 SMTP 服务器的连接。The connection established by the current instance of the SmtpClient class to the SMTP server may be re-used if an application wishes to send multiple messages to the same SMTP server. 当使用身份验证或加密建立与 SMTP 服务器的连接时,此方法特别有用。This is particularly useful when authentication or encryption are used establish a connection to the SMTP server. 身份验证和建立 TLS 会话的过程可能是成本高昂的操作。The process of authenticating and establishing a TLS session can be expensive operations. 如果将大量电子邮件发送到相同的 SMTP 服务器,则为每条消息重新建立连接的要求可能会对性能产生重大影响。A requirement to re-establish a connection for each message when sending a large quantity of email to the same SMTP server could have a significant impact on performance. 有大量的大量电子邮件应用程序可发送电子邮件状态更新、新闻稿分发或电子邮件警报。There are a number of high-volume email applications that send email status updates, newsletter distributions, or email alerts. 此外,许多电子邮件客户端应用程序都支持脱机模式,用户可在此模式下撰写多封电子邮件,这些电子邮件稍后会在与 SMTP 服务器建立连接时发送。Also many email client applications support an off-line mode where users can compose many email messages that are sent later when a connection to the SMTP server is established. 通常,电子邮件客户端将所有 SMTP 邮件发送到特定的 SMTP 服务器(由 Internet 服务提供商提供),然后将此电子邮件转发给其他 SMTP 服务器。It is typical for an email client to send all SMTP messages to a specific SMTP server (provided by the Internet service provider) that then forwards this email to other SMTP servers.

SmtpClient 类实现对 SMTP 连接进行缓冲,使其可以避免在将每条消息重新建立到同一个服务器的连接时产生的开销。The SmtpClient class implementation pools SMTP connections so that it can avoid the overhead of re-establishing a connection for every message to the same server. 应用程序可以重复使用同一个 SmtpClient 对象将许多不同的电子邮件发送到相同的 SMTP 服务器和许多不同的 SMTP 服务器。An application may re-use the same SmtpClient object to send many different emails to the same SMTP server and to many different SMTP servers. 因此,无法确定何时使用 SmtpClient 对象完成应用程序的操作,并且应清理该对象。As a result, there is no way to determine when an application is finished using the SmtpClient object and it should be cleaned up.

当 SMTP 会话完成并且客户端希望终止连接时,它必须向服务器发送一条 QUIT 消息,指示它没有更多要发送的消息。When an SMTP session is finished and the client wishes to terminate the connection, it must send a QUIT message to the server to indicate that it has no more messages to send. 这允许服务器释放与客户端的连接关联的资源,并处理客户端发送的消息。This allows the server to free up resources associated with the connection from the client and process the messages which were sent by the client.

对执行异步 SendAsync 操作的 SmtpClient 对象调用 Dispose 方法将取消发送操作,就好像已调用 SendAsyncCancel 方法一样。Calling the Dispose method on a SmtpClient object that is executing an asynchronous SendAsync operation will cancel the send operation as though the SendAsyncCancel method had been called.

SmtpClient 类没有 Finalize 方法。The SmtpClient class has no Finalize method. 因此,应用程序必须调用 Dispose 才能显式释放资源。So an application must call Dispose to explicitly free up resources.

Dispose 方法可循环访问在 Host 属性中指定的 SMTP 服务器的所有已建立连接,并发送一条 QUIT 消息,并正确地结束 TCP 连接。The Dispose method iterates through all established connections to the SMTP server specified in the Host property and sends a QUIT message followed by gracefully ending the TCP connection. Dispose 方法还会释放 Socket 使用的非托管资源,并可以选择释放托管资源。The Dispose method also releases the unmanaged resources used by the Socket and optionally disposes of the managed resources.

在完成使用 Dispose 后,应调用 SmtpClientCall Dispose when you are finished using the SmtpClient. Dispose 方法使 SmtpClient 处于不可用状态。The Dispose method leaves the SmtpClient in an unusable state. 调用 Dispose后,必须释放对 SmtpClient 的所有引用,以便垃圾回收器可以回收 SmtpClient 占用的内存。After calling Dispose, you must release all references to the SmtpClient so the garbage collector can reclaim the memory that the SmtpClient was occupying.

有关详细信息,请参阅清理非托管资源实现 Dispose 方法For more information, see Cleaning Up Unmanaged Resources and Implementing a Dispose Method.

备注

每次释放对 Dispose 的最后一个引用前,均应调用 SmtpClientAlways call Dispose before you release your last reference to the SmtpClient. 否则,将不会释放它所使用的资源,因此垃圾回收器可以回收内存。Otherwise, the resources it is using will not be freed so the garbage collector can reclaim the memory.

适用于