将(多封)电子邮件从用户输入的文本中提取为 MailAddress 格式 (.NET)

发布于 2024-09-08 09:57:50 字数 1155 浏览 3 评论 0原文

MailAddress 类不提供解析具有多个电子邮件的字符串的方法。 MailAddressCollection确实,但它只接受 CSV 并且不允许引号内有逗号< /强>。我正在寻找一个文本处理器来根据用户输入创建电子邮件集合,而不受这些限制。

处理器应采用以下任意格式的逗号或分号分隔值:

"First Middle Last" <[email protected]>
First Middle Last <[email protected]>
[email protected]
"Last, First" <[email protected]>

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" <[email protected]>
First Middle Last <[email protected]>
[email protected]
"Last, First" <[email protected]>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

要走就滚别墨迹 2024-09-15 09:57:50

MailAddressCollection.Add() 例程支持逗号分隔的地址列表。

Dim mc As New Net.Mail.MailAddressCollection()
mc.Add("Bob <[email protected]>, [email protected], ""John Doe"" <[email protected]>")
For Each m As Net.Mail.MailAddress In mc
    Debug.Print("{0} ({1})", m.DisplayName, m.Address)
Next

输出:

Bob ([email protected])
([email protected])
John Doe ([email protected])

The MailAddressCollection.Add() routine supports a comma delimited address list.

Dim mc As New Net.Mail.MailAddressCollection()
mc.Add("Bob <[email protected]>, [email protected], ""John Doe"" <[email protected]>")
For Each m As Net.Mail.MailAddress In mc
    Debug.Print("{0} ({1})", m.DisplayName, m.Address)
Next

Output:

Bob ([email protected])
([email protected])
John Doe ([email protected])
醉生梦死 2024-09-15 09:57:50

开源库DotNetOpenMail(旧)有一个EmailAddress类,可以解析几乎所有合法形式的电子邮件地址,和 EmailAddressCollection。你可以从那里开始。

The open source library DotNetOpenMail (old) has an EmailAddress class that can parse almost all legal forms of email addresses, and an EmailAddressCollection. You could start there.

我一向站在原地 2024-09-15 09:57:50

在提出相关问题,我意识到一个更好的方法:

/// <summary>
/// Extracts email addresses in the following formats:
/// "Tom W. Smith" <[email protected]>
/// "Smith, Tom" <[email protected]>
/// Tom W. Smith <[email protected]>
/// [email protected]
/// 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;
}

该方法通过了以下测试:

[TestMethod]
public void ExtractEmails_SingleEmail_Matches()
{
    string value = "[email protected]";
    var expected = new List<MailAddress>
        {
            new MailAddress("[email protected]"),
        };

    var actual = value.ExtractEmailAddresses();

    CollectionAssert.AreEqual(expected, actual.ToList());
}

[TestMethod()]
public void ExtractEmails_JustEmailCSV_Matches()
{
    string value = "[email protected]; [email protected]";
    var expected = new List<MailAddress>
        {
            new MailAddress("[email protected]"),
            new MailAddress("[email protected]"),
        };

    var actual = value.ExtractEmailAddresses();

    CollectionAssert.AreEqual(expected, actual.ToList());
}

[TestMethod]
public void ExtractEmails_MultipleWordNameThenEmailSemicolonSV_Matches()
{
    string value = "a a a <[email protected]>; a a a <[email protected]>";
    var expected = new List<MailAddress>
        {
            new MailAddress("a a a <[email protected]>"),
            new MailAddress("a a a <[email protected]>"),
        };

    var actual = value.ExtractEmailAddresses();

    CollectionAssert.AreEqual(expected, actual.ToList());
}

[TestMethod]
public void ExtractEmails_JustEmailsSemicolonSV_Matches()
{
    string value = "[email protected]; [email protected]";
    var expected = new List<MailAddress>
        {
            new MailAddress("[email protected]"),
            new MailAddress("[email protected]"),
        };

    var actual = value.ExtractEmailAddresses();

    CollectionAssert.AreEqual(expected, actual.ToList());
}

[TestMethod]
public void ExtractEmails_NameInQuotesWithCommaThenEmailsCSV_Matches()
{
    string value = "\"a, a\" <[email protected]>; \"a, a\" <[email protected]>";
    var expected = new List<MailAddress>
        {
            new MailAddress("\"a, a\" <[email protected]>"),
            new MailAddress("\"a, a\" <[email protected]>"),
        };

    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();
}

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

/// <summary>
/// Extracts email addresses in the following formats:
/// "Tom W. Smith" <[email protected]>
/// "Smith, Tom" <[email protected]>
/// Tom W. Smith <[email protected]>
/// [email protected]
/// 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 = "[email protected]";
    var expected = new List<MailAddress>
        {
            new MailAddress("[email protected]"),
        };

    var actual = value.ExtractEmailAddresses();

    CollectionAssert.AreEqual(expected, actual.ToList());
}

[TestMethod()]
public void ExtractEmails_JustEmailCSV_Matches()
{
    string value = "[email protected]; [email protected]";
    var expected = new List<MailAddress>
        {
            new MailAddress("[email protected]"),
            new MailAddress("[email protected]"),
        };

    var actual = value.ExtractEmailAddresses();

    CollectionAssert.AreEqual(expected, actual.ToList());
}

[TestMethod]
public void ExtractEmails_MultipleWordNameThenEmailSemicolonSV_Matches()
{
    string value = "a a a <[email protected]>; a a a <[email protected]>";
    var expected = new List<MailAddress>
        {
            new MailAddress("a a a <[email protected]>"),
            new MailAddress("a a a <[email protected]>"),
        };

    var actual = value.ExtractEmailAddresses();

    CollectionAssert.AreEqual(expected, actual.ToList());
}

[TestMethod]
public void ExtractEmails_JustEmailsSemicolonSV_Matches()
{
    string value = "[email protected]; [email protected]";
    var expected = new List<MailAddress>
        {
            new MailAddress("[email protected]"),
            new MailAddress("[email protected]"),
        };

    var actual = value.ExtractEmailAddresses();

    CollectionAssert.AreEqual(expected, actual.ToList());
}

[TestMethod]
public void ExtractEmails_NameInQuotesWithCommaThenEmailsCSV_Matches()
{
    string value = "\"a, a\" <[email protected]>; \"a, a\" <[email protected]>";
    var expected = new List<MailAddress>
        {
            new MailAddress("\"a, a\" <[email protected]>"),
            new MailAddress("\"a, a\" <[email protected]>"),
        };

    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();
}
说好的呢 2024-09-15 09:57:50

实际上,MailAddressCollection 确实支持逗号分隔的地址,即使引号内有逗号也是如此。 我最近发现的真正问题,是 CSV 列表必须已经编码为 ASCII 字符集,即。 Unicode 地址的 Q 编码或 B 编码。

尽管我在 Sasa 中提供了 B 编码,但基类库中没有执行此编码的函数。我还刚刚添加了一个电子邮件解析功能,可以解决该线程中的问题。

Actually, MailAddressCollection DOES support comma-delimited addresses, even with the commas inside the quotes. The real problem I recently discovered, is that the CSV list must already be encoded into the ASCII character set, ie. Q-encoded or B-encoded for Unicode addresses.

There is no function in the base class libraries to perform this encoding, although I provide B-encoding in Sasa. I also just added an e-mail parsing function which addresses the question in this thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文