Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 903129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:49:33+00:00 2026-05-15T15:49:33+00:00

The MailAddress class doesn’t provide a way to parse a string with multiple emails.

  • 0

The MailAddress class doesn’t provide a way to parse a string with multiple emails. The MailAddressCollection class does, but it only accepts CSV and does not allow commas inside of quotes. I am looking for a text processor to create a collection of emails from user input without these restrictions.

The processor should take comma- or semicolon-separated values in any of these formats:

"First Middle Last" <fml@example.com>
First Middle Last <fml@example.com>
fml@example.com
"Last, First" <fml@example.com>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:49:34+00:00Added an answer on May 15, 2026 at 3:49 pm

    After asking a related question, I became aware of a better method:

    /// <summary>
    /// Extracts email addresses in the following formats:
    /// "Tom W. Smith" &lt;tsmith@contoso.com&gt;
    /// "Smith, Tom" &lt;tsmith@contoso.com&gt;
    /// Tom W. Smith &lt;tsmith@contoso.com&gt;
    /// tsmith@contoso.com
    /// Multiple emails can be separated by a comma or semicolon.
    /// Watch out for <see cref="FormatException"/>s when enumerating.
    /// </summary>
    /// <param name="value">Collection of emails in the accepted formats.</param>
    /// <returns>
    /// A collection of <see cref="System.Net.Mail.MailAddress"/>es.
    /// </returns>
    /// <exception cref="ArgumentException">Thrown if the value is null, empty, or just whitespace.</exception>
    public static IEnumerable<MailAddress> ExtractEmailAddresses(this string value)
    {
        if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("The arg cannot be null, empty, or just whitespace.", "value");
    
        // Remove commas inside of quotes
        value = value.Replace(';', ',');
        var emails = value.SplitWhilePreservingQuotedValues(',');
        var mailAddresses = emails.Select(email => new MailAddress(email));
        return mailAddresses;
    }
    
    /// <summary>
    /// Splits the string while preserving quoted values (i.e. instances of the delimiter character inside of quotes will not be split apart).
    /// Trims leading and trailing whitespace from the individual string values.
    /// Does not include empty values.
    /// </summary>
    /// <param name="value">The string to be split.</param>
    /// <param name="delimiter">The delimiter to use to split the string, e.g. ',' for CSV.</param>
    /// <returns>A collection of individual strings parsed from the original value.</returns>
    public static IEnumerable<string> SplitWhilePreservingQuotedValues(this string value, char delimiter)
    {
        Regex csvPreservingQuotedStrings = new Regex(string.Format("(\"[^\"]*\"|[^{0}])+", delimiter));
        var values =
            csvPreservingQuotedStrings.Matches(value)
            .Cast<Match>()
            .Select(m => m.Value.Trim())
            .Where(v => !string.IsNullOrWhiteSpace(v));
        return values;
    }
    

    This method passes the following tests:

    [TestMethod]
    public void ExtractEmails_SingleEmail_Matches()
    {
        string value = "a@a.a";
        var expected = new List<MailAddress>
            {
                new MailAddress("a@a.a"),
            };
    
        var actual = value.ExtractEmailAddresses();
    
        CollectionAssert.AreEqual(expected, actual.ToList());
    }
    
    [TestMethod()]
    public void ExtractEmails_JustEmailCSV_Matches()
    {
        string value = "a@a.a; a@a.a";
        var expected = new List<MailAddress>
            {
                new MailAddress("a@a.a"),
                new MailAddress("a@a.a"),
            };
    
        var actual = value.ExtractEmailAddresses();
    
        CollectionAssert.AreEqual(expected, actual.ToList());
    }
    
    [TestMethod]
    public void ExtractEmails_MultipleWordNameThenEmailSemicolonSV_Matches()
    {
        string value = "a a a <a@a.a>; a a a <a@a.a>";
        var expected = new List<MailAddress>
            {
                new MailAddress("a a a <a@a.a>"),
                new MailAddress("a a a <a@a.a>"),
            };
    
        var actual = value.ExtractEmailAddresses();
    
        CollectionAssert.AreEqual(expected, actual.ToList());
    }
    
    [TestMethod]
    public void ExtractEmails_JustEmailsSemicolonSV_Matches()
    {
        string value = "a@a.a; a@a.a";
        var expected = new List<MailAddress>
            {
                new MailAddress("a@a.a"),
                new MailAddress("a@a.a"),
            };
    
        var actual = value.ExtractEmailAddresses();
    
        CollectionAssert.AreEqual(expected, actual.ToList());
    }
    
    [TestMethod]
    public void ExtractEmails_NameInQuotesWithCommaThenEmailsCSV_Matches()
    {
        string value = "\"a, a\" <a@a.a>; \"a, a\" <a@a.a>";
        var expected = new List<MailAddress>
            {
                new MailAddress("\"a, a\" <a@a.a>"),
                new MailAddress("\"a, a\" <a@a.a>"),
            };
    
        var actual = value.ExtractEmailAddresses();
    
        CollectionAssert.AreEqual(expected, actual.ToList());
    }
    
    [TestMethod]
    [ExpectedException(typeof(ArgumentException))]
    public void ExtractEmails_EmptyString_Throws()
    {
        string value = string.Empty;
    
        var actual = value.ExtractEmailAddresses();
    }
    
    [TestMethod]
    [ExpectedException(typeof(FormatException))]
    public void ExtractEmails_NonEmailValue_ThrowsOnEnumeration()
    {
        string value = "a";
    
        var actual = value.ExtractEmailAddresses();
    
        actual.ToList();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a custom string object, but it does not modelbind when I post
I have following model: public class FormularModel { [Required] public string Position { get;
I have a standard update happening via linq to sql but the data does
We've recently migrated from .NET 2 to .NET 4 and the System.Net.Mail.MailAddress class is
Is it me or is there a bug in the MailAddress class in System.Net.Mail?
public class FacebookConnect extends Activity { Facebook facebook = new Facebook(); String FILENAME =
i have a User class with a field for the e-mail-address and a password.
I have a e-mail address validator but I need to add special characters as
I'm trying to send an e-mail to multiple e-mail address in my database. Here
have downloaded page by webbrowser and need to get mail address. But it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.