Implementing Email Sending to Multiple Recipients with MailMessage

Nov 23, 2025 · Programming · 9 views · 7.8

Keywords: C# | MailMessage | Email Sending | Multiple Recipients | String Splitting

Abstract: This article provides an in-depth exploration of implementing email sending to multiple recipients using the MailMessage class in C#. By analyzing best practices, it demonstrates how to properly handle semicolon-separated email address lists through string splitting and iterative addition methods. The article compares different implementation approaches and provides complete code examples with detailed implementation steps to help developers master efficient and reliable bulk email sending techniques.

Introduction

In modern web application development, email functionality is a common business requirement. When sending the same email content to multiple recipients, efficiently and accurately handling email address lists becomes a critical challenge. This article delves into the implementation methods for sending emails to multiple recipients using C#'s MailMessage class.

Problem Analysis

In practical development scenarios, email addresses are typically stored in databases and separated by specific delimiters. Common separators include semicolons (;) or commas (,). After retrieving the email address list from the database, it needs to be properly parsed and added to the mail message object.

Core Implementation Methods

String Splitting and Iterative Addition

The best practice approach involves using string splitting methods to decompose a string containing multiple email addresses into individual addresses, then adding them to the mail message through iteration. This method offers advantages of clear code, easy understanding, and maintenance.

// Example: Handling semicolon-separated email address list
string addresses = "address1@example.com;address2@example.com";
MailMessage mailMessage = new MailMessage();

foreach (var address in addresses.Split(new [] {";"}, StringSplitOptions.RemoveEmptyEntries))
{
    mailMessage.To.Add(address);
}

In the above code:

Complete Implementation Example

Below is a complete email sending implementation covering the entire process from reading email addresses from the database to final sending:

public void SendBulkEmail(string fromEmail, string addresses)
{
    try
    {
        MailMessage Msg = new MailMessage();
        MailAddress fromMail = new MailAddress(fromEmail);
        Msg.From = fromMail;
        
        // Handle multiple recipients
        foreach (var address in addresses.Split(new [] {";"}, StringSplitOptions.RemoveEmptyEntries))
        {
            Msg.To.Add(new MailAddress(address.Trim()));
        }
        
        Msg.Subject = "Email Subject";
        Msg.Body = "Email Content";
        Msg.IsBodyHtml = true;
        
        SmtpClient smtpClient = new SmtpClient("smtp server name");
        smtpClient.Send(Msg);
    }
    catch (Exception ex)
    {
        // Exception handling logic
        Console.WriteLine($"Email sending failed: {ex.Message}");
    }
}

Alternative Approach Analysis

Besides the iterative addition method, another implementation exists: using MailMessage's constructor to directly pass a comma-separated address list. This method requires first replacing semicolons with commas:

MailMessage Msg = new MailMessage(fromMail, addresses.Replace(";", ","));

The advantages of this method include concise code, but it has the following limitations:

Best Practice Recommendations

Address Validation and Cleaning

In practical applications, it's recommended to validate and clean email addresses:

foreach (var address in addresses.Split(new [] {";"}, StringSplitOptions.RemoveEmptyEntries))
{
    string cleanAddress = address.Trim();
    if (IsValidEmail(cleanAddress))
    {
        mailMessage.To.Add(new MailAddress(cleanAddress));
    }
}

Error Handling Mechanism

A comprehensive error handling mechanism is crucial for email sending functionality:

try
{
    // Email sending logic
    smtpClient.Send(Msg);
    Console.WriteLine("Email sent successfully");
}
catch (SmtpException smtpEx)
{
    Console.WriteLine($"SMTP Error: {smtpEx.Message}");
}
catch (Exception ex)
{
    Console.WriteLine($"Sending failed: {ex.Message}");
}

Performance Optimization Considerations

When handling large numbers of recipients, performance optimization should be considered:

Conclusion

Through the analysis in this article, we can see that using string splitting and iterative addition is the best practice for handling multiple email recipients. This method offers clear code, easy maintenance, and good extensibility. In practical development, combined with appropriate error handling and address validation, stable and reliable email sending functionality can be built.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.