-1

usually, these emails come in the form name. I am trying to use MailBoxAddress.Parse to take the name and email address. I am getting too many errors here as it seems that people put their name in any format they want. For example, the following triggers an error:

Alert: xyz's Weather Now - West Association <emailxx@insignificantstylise.com>
Auto Insurance @ full-auto-coverage.com <emailxx@bigwigfeast.com>
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
Samuel
  • 1,949
  • 4
  • 18
  • 30

1 Answers1

1

I would recommend doing this:

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);
}
jstedfast
  • 35,744
  • 5
  • 97
  • 110