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.
Related
We have a requirement to Parse the email and get certain fields like Name, Address and from email address from the mail body and Header. The Problem lies in the fact that the format of email is not fixed which means that the fields can come in any order in the email body but we only need to get the values of the above mentioned fields.
I am able to get the whole email but not able to understand how to handle the scenario of getting the value from my required fields.
Request you to please have a look and help me out.
Regards
Vineet More
How can I validate the following incorrect email addresses with regex in c#?
eg:
test#test.com.net.com
or
test#test.net.net.org
These are being validated as correct email addresses. Any thoughts? Thanks
While both test#test.com.net.com and test#test.net.net.org are valid, from a syntactic point of view, their domain parts do not point to existing domains.
For this kind of test, you may want to extract the domain part you are interest in and query the DNS (see RFC 2821 and RFC 2822) to see if it exists.
Since you are using .NET, by the way, I would suggest you to take a look at our EmailVerify.NET, a leading email validation library which can validate the syntax (according to the latest IETF standards), the domain parts and the presence of a mailbox for your email addresses.
You may want to just use something like:
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
For a list, please see this page.
Please consider this regex:
([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})
It will match all the email-address, if it did not match the email address, the email-ad is not in correct format, Hope it helps :)
How would I check on registration that a user types in a specific email address? For example, i want my registration form to only allow these email addresses:
#gmail.com
#yahoo.com
#live.com
Like this:
[RegularExpression( #"#(gmail|yahoo|live)\.com$", ErrorMessage = "Invalid domain in email address. The domain must be gmail.com, yahoo.com or live.com")]
public string EmailAddress { get ; set ; }
You don't even need a regular expression; you can just use the split() function to obtain the part of the email address after the "#" and check it against your list of allowed providers.
This by itself doesn't guarantee that it's a well-formed email address (that may require a regex, and a somewhat complicated one), but it will make sure that the address ends with one of the domains on your list.
You could use a RegularExpressionValidator control and an expression to look for the email domains. You can find a sample at http://www.regexplib.com if you don't already have one.
You're probably going to want a CustomValidator as well that performs an identical server-side check. Users can circumvent your RegularExpressionValidator if they disable Javascript.
There is not built in but you can use [RegularExpression].You can write custom EmailAttribute deriving from RegularExpressionAttribute.
A very well implementation is done here
You can use following regular expression to check email:
^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)#[a-z0-9]+(\.[a-z0-9]+)\.([a-z]{2,4})$
Beside this, you can have Data Annotation Extension which has [Email] attribute that allows for validating an email address.
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®");
I'm using IPAddress.TryParse() to parse IP addresses. However, it's a little too permissive (parsing "1" returns 0.0.0.1). I'd like to limit the input to dotted octet notation. What's the best way to do this?
(Note: I'm using .NET 2.0)
Edit
Let me clarify:
I'm writing an app that will scan a range of IPs looking for certain devices (basically a port scanner). When the user enters "192.168.0.1" for the starting address, I want to automatically fill in "192.168.0.255" as the ending address. The problem is that when they type "1", it parses as "0.0.0.1" and the ending address fills in as "0.0.0.255" - which looks goofy.
If you are interested in parsing the format, then I'd use a regular expression. Here's a good one (source):
bool IsDottedDecimalIP(string possibleIP)
{
Regex R = New Regex(#"\b(?:\d{1,3}\.){3}\d{1,3}\b");
return R.IsMatch(possibleIP) && Net.IPAddress.TryParse(possibleIP, null);
}
That regex doesn't catch invalid IPs but does enforce your pattern. The TryParse checks their validity.
An IP address is actually a 32 bit number - it is not xxx.xxx.xxx.xxx - that's just a human readable format for the same. So IP address 1 is actually 0.0.0.1.
EDIT: Given the clarification, you could either go with a regex as has been suggested, or you could format the short cuts to your liking, so if you want "1" to appears as "1.0.0.0". you could append that and still use the parse method.