通常,这些电子邮件以名称形式出现。我正在尝试使用 MailBoxAddress.Parse 来获取名称和电子邮件地址。我在这里遇到了太多错误,因为人们似乎以他们想要的任何格式来输入自己的名字。例如,以下内容会触发错误:
Alert: xyz's Weather Now - West Association <emailxx@insignificantstylise.com>
Auto Insurance @ full-auto-coverage.com <emailxx@bigwigfeast.com>
答案 0 :(得分:1)
我建议这样做:
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);
}