See Also: SmtpClient Members
The System.Net.Mail.SmtpClient class is used to send e-mail to an SMTP server for delivery. The SMTP protocol is defined in RFC 2821, which is available at tp://www.ietf.org/.
The classes shown in the following table are used to construct e-mail messages that can be sent using System.Net.Mail.SmtpClient.
System.Net.Mail.Attachment |
Represents file attachments. This class allows you to attach files, streams, or text to an e-mail message. |
System.Net.Mail.MailAddress |
Represents the e-mail address of the sender and recipients. |
System.Net.Mail.MailMessage |
Represents an e-mail message. |
To construct and send an e-mail message by using System.Net.Mail.SmtpClient, you must specify the following information:
The SMTP host server that you use to send e-mail. See the SmtpClient.Host and SmtpClient.Port properties.
Credentials for authentication, if required by the SMTP server. See the SmtpClient.Credentials property.
The e-mail address of the sender. See the erload:System.Net.Mail.SmtpClient.Send and erload:System.Net.Mail.SmtpClient.SendAsync methods that take a from parameter. Also see the MailMessage.From property.
The e-mail address or addresses of the recipients. See the erload:System.Net.Mail.SmtpClient.Send and erload:System.Net.Mail.SmtpClient.SendAsync methods that take a recipient parameter. Also see the MailMessage.To property.
The message content. See the erload:System.Net.Mail.SmtpClient.Send and erload:System.Net.Mail.SmtpClient.SendAsync methods that take a body parameter. Also see the MailMessage.Body property.
To include an attachment with an e-mail message, first create the attachment by using the System.Net.Mail.Attachment class, and then add it to the message by using the MailMessage.Attachments property. Depending on the e-mail reader used by the recipients and the file type of the attachment, some recipients might not be able to read the attachment. For clients that cannot display the attachment in its original form, you can specify alternate views by using the MailMessage.AlternateViews property.
You can use the application or machine configuration files to specify default host, port, and credentials values for all System.Net.Mail.SmtpClient objects. For more information, see mailSettings for system.net.
To send the e-mail message and block while waiting for the e-mail to be transmitted to the SMTP server, use one of the synchronous erload:System.Net.Mail.SmtpClient.Send methods. To allow your program's main thread to continue executing while the e-mail is transmitted, use one of the asynchronous erload:System.Net.Mail.SmtpClient.SendAsync methods. The SmtpClient.SendCompleted event is raised when a erload:System.Net.Mail.SmtpClient.SendAsync operation completes. To receive this event, you must add a System.Net.Mail.SendCompletedEventHandler delegate to SmtpClient.SendCompleted. The System.Net.Mail.SendCompletedEventHandler delegate must reference a callback method that handles notification of SmtpClient.SendCompleted events. To cancel an asynchronous e-mail transmission, use the SmtpClient.SendAsyncCancel method.
If there is an e-mail transmission in progress and you call erload:System.Net.Mail.SmtpClient.SendAsync or erload:System.Net.Mail.SmtpClient.Send again, you will receive an InvalidOperationException.
The connection established by the current instance of the System.Net.Mail.SmtpClient class to the SMTP server may be re-used if an application wishes to send multiple messages to the same SMTP server. This is particularly useful when authentication or encryption are used establish a connection to the SMTP server. The process of authenticating and establishing a TLS session can be expensive operations. 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. 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. 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.
The System.Net.Mail.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. An application may re-use the same System.Net.Mail.SmtpClient object to send many different emails to the same SMTP server and to many different SMTP servers. As a result, there is no way to determine when an application is finished using the System.Net.Mail.SmtpClient object and it should be cleaned up.
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.
The System.Net.Mail.SmtpClient class has no Finalize method, so an application must call erload:System.Net.Mail.SmtpClient.Dispose to explicitly free up resources. The erload:System.Net.Mail.SmtpClient.Dispose method iterates through all established connections to the SMTP server specified in the SmtpClient.Host property and sends a QUIT message followed by gracefully ending the TCP connection. The erload:System.Net.Mail.SmtpClient.Dispose method also releases the unmanaged resources used by the System.Net.Sockets.Socket and optionally disposes of the managed resources.
Call erload:System.Net.Mail.SmtpClient.Dispose when you are finished using the System.Net.Mail.SmtpClient. The erload:System.Net.Mail.SmtpClient.Dispose method leaves the System.Net.Mail.SmtpClient in an unusable state. After calling erload:System.Net.Mail.SmtpClient.Dispose, you must release all references to the System.Net.Mail.SmtpClient so the garbage collector can reclaim the memory that the System.Net.Mail.SmtpClient was occupying.