+1 vote
by (170 points)
edited

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

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

var test = new MailAddress("kevinlab@email.com", "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 <kevinlab@email.com>");

The easy fix is to simply parse out the comma.

var toAddresses = new MailAddressCollection();
toAddresses.Add("Lab Kevin <kevinlab@email.com>");

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
by (58.9k points)
edited
 
Best answer

Mail address in format "Lab, Kevin kevinlab@email.com", 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 kevinlab@email.com";
int i = a.LastIndexOf(" ");
string name = a.Substring(0, i);
string addr = a.Substring(i + 1);

MailAddress address = new MailAddress(addr, name);
...