Click
here for MSDN docs.
The SmtpClient class is responsible for sending or transporting the email.
The SmtpClient can transport the email content over the network, or it can
actually write them to the filesystem in the MS IIS Smtp Service Pickup
Directory format, which resembles a RFC821 formatted message. Emails can be sent
either synchronously or asynchronously. The SmtpClient also supports
sending email via SSL for security purposes. The following list of
properties are the most common used on the SmtpClient class.
Name |
Description |
Credentials |
Gets or sets the credentials used to authenticate the sender. |
DeliveryMethod |
Specifies how outgoing email messages will be handled. |
EnableSsl |
Specify whether the SmtpClient uses Secure Sockets Layer (SSL) to
encrypt the connection. |
Host |
Gets or sets the name or IP address of the host used for SMTP
transactions. |
Port |
Gets or sets the port used for SMTP transactions. |
Below is an example demonstrating the SmtpClient class.
[ C# ]
static void Authenticate()
{
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
//to authenticate we set the username and password properites on the SmtpClient
smtp.Credentials = new NetworkCredential("username", "secret");
smtp.Send(mail);
}
[ VB.NET ]
Sub Authenticate()
'create the mail message
Dim mail As New MailMessage()
'set the addresses
mail.From = New MailAddress("me@mycompany.com")
mail.To.Add("you@yourcompany.com")
'set the content
mail.Subject = "This is an email"
mail.Body = "this is the body content of the email."
'send the message
Dim smtp As New SmtpClient("127.0.0.1")
'to authenticate we set the username and password properites on the SmtpClient
smtp.Credentials = New NetworkCredential("username", "secret")
smtp.Send(mail)
End Sub 'Authenticate