System.Net.Mail Namespace
Provides classes for sending email using the Simple Mail Transfer Protocol (SMTP).
SmtpClient Class
System.Net.Mail
Represents a client that sends email to an SMTP server for delivery.
Properties
DeliveryMethod
: Gets or sets the delivery method for the email.Host
: Gets or sets the name or IP address of the host used for sending email.Port
: Gets or sets the port number used for SMTP transactions.UseDefaultCredentials
: Gets or sets a value indicating whether the client uses the default credentials to send e-mail.Credentials
: Gets or sets the network credentials that are sent to the SMTP server.EnableSsl
: Gets or sets a value that indicates whether the SMTP client supports SSL.
Methods
-
Send(MailMessage message)
: Sends an email message to an SMTP server for delivery.Parameters:message
: AMailMessage
object that contains the content, sender, recipient, and other properties of the email message.
-
SendAsync(MailMessage message, Object userToken)
: Sends an email message to an SMTP server for delivery asynchronously.Parameters:message
: AMailMessage
object.userToken
: An object used to pass user state to the callback method.
MailMessage Class
System.Net.Mail
Represents an email message that can be sent using the SmtpClient
class.
Properties
From
: Gets or sets the sender's email address.To
: Gets the collection of recipients' email addresses.CC
: Gets the collection of CC recipients' email addresses.Bcc
: Gets the collection of Bcc recipients' email addresses.Subject
: Gets or sets the subject line of the email message.Body
: Gets or sets the body content of the email message.IsBodyHtml
: Gets or sets a value indicating whether the body content of the email message is in HTML format.Attachments
: Gets the collection of attachments for the email message.
Methods
-
Dispose()
: Releases the unmanaged resources used by theMailMessage
and optionally releases the managed resources.
Attachment Class
System.Net.Mail
Represents an attachment included in an email message.
Constructors
Attachment(string fileName)
: Initializes a new instance of theAttachment
class with the specified file name.Attachment(Stream contentStream, string name)
: Initializes a new instance of theAttachment
class with the specified stream and name.
Properties
ContentStream
: Gets the stream that contains the content of the attachment.Name
: Gets or sets the name of the attachment.ContentType
: Gets or sets the MIME type of the attachment.
NetworkCredential Class
System.Net
Represents a user name, password, and domain used to authenticate a network client.
Constructors
NetworkCredential(string userName, string password)
NetworkCredential(string userName, string password, string domain)
Properties
UserName
: Gets the user name.Password
: Gets the password.Domain
: Gets the domain.
Example Usage
Here's a simple example demonstrating how to send an email:
using System.Net;
using System.Net.Mail;
// Create a new MailMessage
MailMessage mail = new MailMessage();
mail.From = new MailAddress("sender@example.com");
mail.To.Add("recipient@example.com");
mail.CC.Add("cc_recipient@example.com");
mail.Subject = "Test Email from .NET";
mail.Body = "This is the body of the test email.";
mail.IsBodyHtml = false; // Set to true if you want HTML formatting
// Optional: Add attachments
// mail.Attachments.Add(new Attachment("path/to/your/file.pdf"));
// Create an SmtpClient
SmtpClient client = new SmtpClient();
client.Port = 587; // Common port for TLS/STARTTLS
client.EnableSsl = true; // Use SSL
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("your_email@example.com", "your_password");
client.Host = "smtp.example.com"; // Your SMTP server address
try
{
client.Send(mail);
Console.WriteLine("Email sent successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Error sending email: {ex.Message}");
}
finally
{
mail.Dispose(); // Release resources
}