Comma in display name for a MailAddress

+1 vote
asked Sep 26, 2013 by Kevin Lab (170 points)
edited Sep 27, 2013

Some emails we receive in our system have a comma in their display names. For example: "Lab, Kevin".

I noticed we could set the proper display name if we create the MailAddress ourselves.

var test = new MailAddress("", "Lab, Kevin");

However, this would require us to write the parsing logic of the inputs.

Attempting to add this to a new MailAddressCollection fails because the comma is interpreted as a delimiter, and just "Lab" is added.

var toAddresses = new MailAddressCollection();
toAddresses.Add("Lab, Kevin <>");

The easy fix is to simply parse out the comma.

var toAddresses = new MailAddressCollection();
toAddresses.Add("Lab Kevin <>");

but then the display name becomes "Lab Kevin". Is there any way to have it included in the display name (i.e. "Lab, Kevin")?

Applies to: Rebex Secure Mail

1 Answer

+1 vote
answered Sep 27, 2013 by Tomas Knopp (58,890 points)
edited Sep 27, 2013
Best answer

Mail address in format "Lab, Kevin", is not a supported MIME address format, because comma ',' as well as semicollon ';' are delimiters as defined by RFC 5322.

However writing a simple parser for this special case is not hard at all. Please try this code snippet:

string a = "Lab, Kevin";
int i = a.LastIndexOf(" ");
string name = a.Substring(0, i);
string addr = a.Substring(i + 1);

MailAddress address = new MailAddress(addr, name);