Why doesn't MailAddress quote the DisplayName when there is a comma? - c#

We've recently migrated from .NET 2 to .NET 4 and the System.Net.Mail.MailAddress class is giving me a headache. Previously, if I had an email (joe#example.com) and a displayname (Joe Smith, CEO®) you could do:
MailAddress from = new MailAddress("joe#example.com", "Joe Smith, CEO®");
And you'd get a properly formatted "Joe Smith, CEO" <joe#example.com> when emailed. This was viewable/readable/etc. by all mailers.
With .NET 4 Outlook/Exchange are throwing a fit on how this is encoded, splitting it on the Comma: <=?utf-8?Q?Joe#gwm.example.com>, CEO=C2=AE?= <joe#example.com> which causes it to not be decoded properly.
From Microsoft:
A comma is used to separate elements in a list of mail addresses. As a
result, a comma should not be used in unquoted display names in a
list.
Which is fine, but when you do:
MailAddress from = new MailAddress("joe#example.com", "\"Joe Smith, CEO®\"");
The quotes are stripped because;
This method removes surrounding quotes not displayed by the
DisplayName property.
So how do you tell MailAddress that the comma it doesn't want should be quoted, without adding an extra space (such as "\u200B\"Joe...\"") that makes the address indent in the mail reader?
UPDATE
Microsoft's Answer (see response comments for link):
Posted by Microsoft on 8/17/2011 -- Thank you for your
feedback. This is a well known issue and a patch is currently being
created for .NET 4.0. To obtain the patch please contact Microsoft
Support directly regarding KB 2576045.

didn't test it but try
MailAddress from = new MailAddress("joe#nospam.com", "Joe Smith\",\" CEO®");
EDIT - another option:
MailAddress from = new MailAddress("joe#nospam.com", "Joe Smith\x2C CEO®");

Related

Why does EmailAddressAttribute.IsValid and MailAddress think that emails which contain "ª" are valid? [duplicate]

This question already has answers here:
Can an email address contain international (non-english) characters?
(7 answers)
Closed 1 year ago.
I have this C# code:
void Main()
{
// method 1 - using MailAddress
var email = "fooªbar#cander.com";
Console.WriteLine(IsValidEmail(email));
// method 2 - using EmailAddressAttribute
var validator = new System.ComponentModel.DataAnnotations.EmailAddressAttribute();
Console.WriteLine(validator.IsValid(email));
}
bool IsValidEmail(string email)
{
try
{
var addr = new System.Net.Mail.MailAddress(email);
return addr.Address == email;
}
catch
{
return false;
}
}
That validates the fooªbar#cander.com email address. And... It validates it althougt it has the "ª" symbol. Why? According to: What characters are allowed in an email address? it shoudn't be valid
It validates it althougt it has the "ª" symbol. Why?
Because your Regex allows "one or more \word characters" before the #, and ª is a word character:
RegexStorm uses the .net engine: you can see that the \w pattern (a single word character) has successfully matched an ª (one match)
According to: What characters are allowed in an email address? it shoudn't be valid
Alas, the regular expression you have used does not accurately implement the specification given in the linked question
When it comes to validating email addresses, genuinely I don't think you should try and control it to a very fine degree - it's a headache to form and maintain a complex Regex that considers every variation and it doesn't really bring much benefit, it just generates a pain point for users whose valid emails don't validate because of a bug in your Regex.
When we test for email validity, we basically only check that it contains an #.. what's the worst that can happen if a user types it in wrong?
(apologies if that picture appears huge; it looks reasonable on a cellphone but I recall that iPhone screenshots sometimes end up looking a bit oversized on web)

Separate email content from signature

Is there a way to separate email content (body text) from an added signature using IMap packages?
IEnumerable MailList = Client.Search(SearchCondition.Unseen());
var email = Client.GetMessage(MailList[0]);
string body = email.Body;
Thanks
This is a rather difficult problem.
For text/plain, you can look for the line "-- " (three characters, including the trailing space). For text/html, you can look for the CSS classes gmail_signature and moz-signature. For all mail, you can look for trailing text that matches the trailing text of the previous message from the same address.
However, none of this is foolproof. Lots of HTML sigs don't use those CSS rules (Outlook, for example, uses no relevant CSS), lots of plaintext sigs don't use --, and lots of middlecrapware inserts text after the signature so the "trailing text" may not be the at the very end.

C# Regular Expression Attribute not working as expected

I am using the RegularExpression attribute to verify multiple email addresses on one input in my view model. The ErrorMessage keeps coming up on field. I have validated my RegEx on 5 different online test sites and they all test positive.
Here is my code:
[RegularExpression(#"\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*([,;\s]+\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*){0,7}", ErrorMessage = "Please enter a valid email address. For multiple addresses please use a comma or semicolon to separate the email addresses.")]
public string EmailAddresses { get; set; }
If I enter an email address it works, if I enter two email addresses without spaces it works, but if I add a space it breaks. I added the '\s' to include white spaces and it does work on the online testers I have tried but it will not work in my application.
The expected valid result should be:
'test#test.com, test2#test.com, test3#test.com'
However, this it coming back as invalid. If I use the exact same sequence with no spaces it is valid.
Kendo UI is checking the validation of the form before sending it to the controller.
Any help is very appreciated. Thank you in advance.
Try this pattern:
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"
You don't need "#" symbol. I tested your pattern at http://regexstorm.net/tester and it is correct match for test#test.com, test2#test.com, test3#test.com . Use construct below
public class LoginViewModel
{
[RegularExpression("\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*([,;\s]+\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*){0,7}", ErrorMessage = "Please enter a valid email address. For multiple addresses please use a comma or semicolon to separate the email addresses.")]
public string EmailAddresses { get; set; }
}
For it to work in ASP.NET MVC Framework ensure you do the have the following on the view
<input type="text" id ="EmailAddresses" name="EmailAddresses"/>
The id and name attributes are required for it to validate. You can do this manually as above or use #Html.EditorFor(model => model.EmailAddresses) which will create the id and name attributes for you
I found the issue. In the end it had nothing to do with C#. The problem was I was using Kendo UI on the front end, as updated in the main post. Keno UI had to also receive the RegEx update sent by Andrew to solve the issue. I will make Andrew as the correct solution as his RegEx did work for me. I was totally turn around by the server side/client side difference as I am new to this. Thank you all for your help.

SmtpClient.Send with VALID email address that has a comma in it

SmtpClient.Send(...) is OK with simple email addresses (eg "JoeBlow#MyCo.com") or fancier addresses (eg "Joe Blow <JoeBlow#MyCo.com>").
But my company has its address book entries formatted like "Blow,Joe <JoeBlow#MyCo.com>". SmtpClient chokes on this because it assumes that the comma is dividing two separate addresses. Is there a way to tell Smtp that I am not using commas in this way? Or is the only option to reformat the company addresses? Don't tell me to just eliminate everything other than the actual address because this extra name info is very useful.
You need to use a string like "\"Blow, Joe\" <JoeBlow#MyCo.com>" so that the comma is within a quoted-string.
In .NET the MailAddress object has both an actual address field and a display name field. The display name CAN have commas (even if this is not encouraged). Using this object with the constructor with 2 parameters allows these funny display names to be used.

Mailto does not invoke outlook in chrome.

In view I rendered two links that have mailto. Both of them have body attributes passed to mailto. One has short body text, other very long. When I click on link that has shorter body, it works and outlook opens. Link with longer body does not work (I clicked and nothing happens). But that happens only in chrome. In other browsers both links work. I noticed that in Chrome page source longer body text is made shorter with some notation. This might be the issue.
Does anyone know how to solve this problem? Any help would be appreciated.
Checking for spaces (and removing them) between the colon and the recipients, and between multiple recipients.
Variables that can be used with mailto:
mailto: set the recipient, or recipients, separate with comma
&cc= set the CC recipient(s)
&bcc= set the BCC recipient(s)
&subject= set the email subject, URL encode for longer sentences, so replace spaces with %20, etc.
&body= set the body of the message, including line breaks. Line breaks should be converted to %0A.
A MailTo Generator can be found here.

Categories

Resources