0 votes

How can I parse:

"Name" <username@domain.com>

I try using "MailAddressParser.ParseOne()" but cant do it.

Dim obj_MailBuilder As New MailBuilder()
obj_MailBuilder.From.Add(...)

Thanks.

by (200 points)

1 Answer

0 votes

Are you sure you actually need to parse such string?
If you are creating email message and you are using MailBuilder class you just need to create appropriate MailBox object. Use its constructor:

var m = new MailBox("Name", "username@domain.com");

or

var m = new MailBox("username@domain.com");

If you definitely need to do parse such string, you should use MailAddressParser class. ParseOne returns MailAddress class. Depending on the string provided, the actual class is MailGroup (representing an email group) or MailBox (representing a single email):

MailAddress address = new MailAddressParser().ParseOne(@"""Name"" <username@domain.com>");
MailBox m = (MailBox)address;
string name = m.Name;  // "Name"
string address = m.Address; // "username@domain.com"
by (300k points)
...