rfc822 格式的电子邮件地址是否有一个好的解析器
is there a good parser for rfc822 formatted email address
通常,这些电子邮件以表单名称的形式出现。我正在尝试使用 MailBoxAddress.Parse 获取姓名和电子邮件地址。我在这里遇到了太多错误,因为人们似乎以他们想要的任何格式输入他们的名字。例如,以下会触发错误:
Alert: xyz's Weather Now - West Association <emailxx@insignificantstylise.com>
Auto Insurance @ full-auto-coverage.com <emailxx@bigwigfeast.com>
我建议这样做:
static MailboxAddress ParseAddr (string input)
{
int lt = input.IndexOf ('<');
if (lt == -1)
throw new FormatException ("Invalid address format");
int gt = input.IndexOf ('>', lt);
if (gt == -1)
throw new FormatException ("Invalid address format");
var name = input.Substring (0, lt).TrimEnd ();
var addr = input.Substring (lt + 1, gt - (lt + 1));
return new MailboxAddress (name, addr);
}
通常,这些电子邮件以表单名称的形式出现。我正在尝试使用 MailBoxAddress.Parse 获取姓名和电子邮件地址。我在这里遇到了太多错误,因为人们似乎以他们想要的任何格式输入他们的名字。例如,以下会触发错误:
Alert: xyz's Weather Now - West Association <emailxx@insignificantstylise.com>
Auto Insurance @ full-auto-coverage.com <emailxx@bigwigfeast.com>
我建议这样做:
static MailboxAddress ParseAddr (string input)
{
int lt = input.IndexOf ('<');
if (lt == -1)
throw new FormatException ("Invalid address format");
int gt = input.IndexOf ('>', lt);
if (gt == -1)
throw new FormatException ("Invalid address format");
var name = input.Substring (0, lt).TrimEnd ();
var addr = input.Substring (lt + 1, gt - (lt + 1));
return new MailboxAddress (name, addr);
}