MailMessage Class

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

Syntax

public class MailMessage

Members

Constructors

Initializes a new instance of the MailMessage class.

Initializes a new instance of the MailMessage class with the specified sender and recipients.

Initializes a new instance of the MailMessage class with the specified sender, recipients, subject, and body.

Properties

Name Description
Attachments Gets a collection of attachments associated with this email message.
Body Gets or sets the body content of this email message.
BodyEncoding Gets or sets the character encoding for the body content of this email message.
CC Gets or sets a comma-separated list of e-mail addresses to carbon copy (CC) the message to.
DeliveryNotificationOptions Gets or sets the delivery notification options for this email message.
From Gets or sets the sender's e-mail address.
Headers Gets a collection of the header fields and values for this email message.
IsBodyHtml Gets or sets a value indicating whether the body content of this email message is in HTML format.
Priority Gets or sets the priority of this email message.
ReplyTo Gets or sets the e-mail address to use when an e-mail client responds to this email message.
Sender Gets or sets the e-mail address of the client that submitted the e-mail message to the SMTP server.
Subject Gets or sets the subject line of this email message.
SubjectEncoding Gets or sets the character encoding for the subject line of this email message.
To Gets or sets a comma-separated list of e-mail addresses to send the message to.

Methods

Releases the unmanaged resources used by the MailMessage and optionally releases the managed resources.

Returns a string that represents the current object.

Example

The following C# code example demonstrates how to create and send an email message using the MailMessage and SmtpClient classes.

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

public class SendMailExample
{
    public static void Main(string[] args)
    {
        // Sender and recipient email addresses
        string senderEmail = "your_email@example.com";
        string senderPassword = "your_password";
        string recipientEmail = "recipient@example.com";
        string smtpAddress = "smtp.example.com";
        int port = 587;

        // Create the mail message
        MailMessage mailMessage = new MailMessage();
        mailMessage.From = new MailAddress(senderEmail);
        mailMessage.To.Add(recipientEmail);
        mailMessage.Subject = "Test Email from .NET";
        mailMessage.Body = "This is a test email sent from a .NET application using the MailMessage class.";
        mailMessage.IsBodyHtml = false;

        // Create the SMTP client
        SmtpClient smtpClient = new SmtpClient(smtpAddress, port);
        smtpClient.EnableSsl = true;
        smtpClient.Credentials = new NetworkCredential(senderEmail, senderPassword);

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

Note: When sending emails, ensure you have the correct SMTP server address, port, and authentication credentials. For security reasons, avoid hardcoding sensitive information like passwords directly in your code. Consider using configuration files or secure credential management techniques.