所以我正在处理一些电子邮件标题数据,对于to:,from:,cc:和bcc:fields,电子邮件地址可以用多种不同的方式表示:
First Last <name@domain.com>
Last, First <name@domain.com>
name@domain.com
Run Code Online (Sandbox Code Playgroud)
这些变体可以以任何顺序出现在同一个消息中,所有这些变量都以逗号分隔的字符串形式出现:
First, Last <name@domain.com>, name@domain.com, First Last <name@domain.com>
Run Code Online (Sandbox Code Playgroud)
我一直试图想出一种方法将这个字符串解析成单独的名字,姓氏,每个人的电子邮件(如果只提供了一个电子邮件地址,则省略名称).
有人可以建议最好的方法吗?
我试图在逗号上拆分,除了在第一个放置姓氏的第二个例子之外,它会起作用.我想这个方法可以工作,如果我拆分后,我检查每个元素,看它是否包含'@'或'<'/'>',如果没有,那么可以假设下一个元素是名字.这是解决这个问题的好方法吗?我是否忽略了地址可能存在的另一种格式?
更新:也许我应该澄清一点,基本上我要做的就是将包含多个地址的字符串分解为包含地址的单个字符串,无论发送的格式是什么.我有自己的方法来验证和提取信息从一个地址来看,找出分隔每个地址的最佳方法对我来说简直太棘手了.
以下是我想出的解决方案:
String str = "Last, First <name@domain.com>, name@domain.com, First Last <name@domain.com>, \"First Last\" <name@domain.com>";
List<string> addresses = new List<string>();
int atIdx = 0;
int commaIdx = 0;
int lastComma = 0;
for (int c = 0; c < str.Length; c++)
{
if (str[c] == '@')
atIdx = c;
if (str[c] == ',')
commaIdx = c;
if (commaIdx > atIdx && atIdx > 0)
{
string temp = str.Substring(lastComma, commaIdx - lastComma);
addresses.Add(temp);
lastComma = commaIdx;
atIdx = commaIdx;
}
if (c == str.Length -1)
{
string temp = str.Substring(lastComma, str.Legth - lastComma);
addresses.Add(temp);
}
}
if (commaIdx < 2)
{
// if we get here we can assume either there was no comma, or there was only one comma as part of the last, first combo
addresses.Add(str);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码生成了我可以进一步处理的各个地址.
小智 5
有一个内部System.Net.Mail.MailAddressParser
类,它具有的方法ParseMultipleAddresses
可以完全满足您的要求。您可以通过反射或调用MailMessage.To.Add
接受电子邮件列表字符串的方法直接访问它。
private static IEnumerable<MailAddress> ParseAddress(string addresses)
{
var mailAddressParserClass = Type.GetType("System.Net.Mail.MailAddressParser");
var parseMultipleAddressesMethod = mailAddressParserClass.GetMethod("ParseMultipleAddresses", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
return (IList<MailAddress>)parseMultipleAddressesMethod.Invoke(null, new object[0]);
}
private static IEnumerable<MailAddress> ParseAddress(string addresses)
{
MailMessage message = new MailMessage();
message.To.Add(addresses);
return new List<MailAddress>(message.To); //new List, because we don't want to hold reference on Disposable object
}
Run Code Online (Sandbox Code Playgroud)
这是我为实现此目的而提出的解决方案:
String str = "Last, First <name@domain.com>, name@domain.com, First Last <name@domain.com>, \"First Last\" <name@domain.com>";
List<string> addresses = new List<string>();
int atIdx = 0;
int commaIdx = 0;
int lastComma = 0;
for (int c = 0; c < str.Length; c++)
{
if (str[c] == '@')
atIdx = c;
if (str[c] == ',')
commaIdx = c;
if (commaIdx > atIdx && atIdx > 0)
{
string temp = str.Substring(lastComma, commaIdx - lastComma);
addresses.Add(temp);
lastComma = commaIdx;
atIdx = commaIdx;
}
if (c == str.Length -1)
{
string temp = str.Substring(lastComma, str.Legth - lastComma);
addresses.Add(temp);
}
}
if (commaIdx < 2)
{
// if we get here we can assume either there was no comma, or there was only one comma as part of the last, first combo
addresses.Add(str);
}
Run Code Online (Sandbox Code Playgroud)