System.Net.Mail

Provides classes for sending electronic mail (e-mail) using the Simple Mail Transfer Protocol (SMTP).

Classes

  • MailAddress

    Represents an email address, with properties for the display name and the URI.

  • MailMessage

    Represents an email message that can be sent using the SmtpClient class.

  • SmtpClient

    Sends an e-mail message by using the Simple Mail Transfer Protocol (SMTP).

  • AttachmentBase

    Abstract base class that represents an attachment in an email message.

  • Attachment

    Represents an e-mail attachment.

  • LinkedResource

    Represents a resource that is embedded in an e-mail message and referenced using a URI.

  • DeliveryNotificationOptions

    Specifies the conditions under which the sender requests that the mail server send a delivery notification.

  • MailPriority

    Specifies the priority of an email message.

Usage Example

Here's a basic example of how to send an email using the System.Net.Mail namespace:


using System;
using System.Net;
using System.Net.Mail;

public class EmailSender
{
    public static void SendTestEmail()
    {
        try
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("sender@example.com");
            mail.To.Add(new MailAddress("recipient@example.com"));
            mail.Subject = "Test Email from .NET";
            mail.Body = "This is a test email sent using the System.Net.Mail namespace.";
            mail.IsBodyHtml = false;

            SmtpClient smtpClient = new SmtpClient("smtp.example.com");
            smtpClient.Port = 587;
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential("sender@example.com", "your_password");

            smtpClient.Send(mail);
            Console.WriteLine("Email sent successfully!");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error sending email: {ex.Message}");
        }
    }
}