Page 1 of 1

Regular expression email filtering

Posted: 26 Nov 2010, 15:17
by SykomantiS
Hi all.

So yesterday while mass mailing our students, one or more emails failed and consequently threw an unhandled exception in our application. before sending any mails however, the list of emails pulled from the db are checked for validity using the following regular expression: (vb code, VS2008)

Code: Select all

If Regex.Match(email, "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*").Success Then
Return True
Else
Return False
End If
Now, as stated, one of the email addies passed this test, but still threw an error. Now I've physically gone over the list and couldn't find the offending address (granted, it's a long list and I could have missed something). The problem is, I don't have a screenshot of the error, and to reproduce it would mean to physically resend all the hundreds of mails, in effect spamming our students. :? So all I can do is try and find the offending email address.

I'm not really good with regex and to be brutally honest, I didn't build the above expression, nor do I even completely understand it. (We don't use regular expressions all that much) Which is why I'm posting this here. Can anyone find fault with the expression used? Would it allow an address that would fail when sending?

The other thing that bugs me is that we are using vb with its inbuilt system.net.mail, and I'm unsure if this will throw an exception when, for instance, an address is valid, but spelt incorrectly. If this is the case then I well, **** :? (Also, then we probably would get more than one error, which we don't).

Right now I'm only concerned with allowing valid emails. I'll worry about incorrect spelling later.

Any help?

Re: Regular expression email filtering

Posted: 26 Nov 2010, 15:25
by Bladerunner
So take that part of the code and the list of emails. Write a small application that goes through the process of sending emails but instead of sending them for real, just send them to some dummy account (if that's even necessary). It won't take you more than 30 minutes and you'll replicate the error message and probably find the error too.

Re: Regular expression email filtering

Posted: 26 Nov 2010, 22:32
by SykomantiS
Did that before posting. Still in the dark. Which is why I made this thread.
Like I said, going through the list of emails I found nothing that would throw an application error. Yet it still did :?

Re: Regular expression email filtering

Posted: 26 Nov 2010, 22:57
by Ron2K
Are you sure it's the e-mail address format and not something else? I can't say for sure (combination of tiredness and Foundry at the moment), but that regex looks good to me...

Re: Regular expression email filtering

Posted: 26 Nov 2010, 23:02
by SykomantiS
As far as I can tell, the error occurred during the sending of an email (the physical mysmtp.sendmail(myEmail) line...) Or whatever it is, don't have the code in front of me right now. I compared that regex to another one I 'acquired' and the results are the same, so it should be good. Which is exactly why it's got me bugged :?

I actually don't know if it's the address format or something else, since testing would mean spamming. There's no way around this one :( Also, like I said, they didn't give me a screenie of said error.

Re: Regular expression email filtering

Posted: 26 Nov 2010, 23:23
by Ron2K
You would have gotten a FormatException if the e-mail address format was cooked; you did mention ApplicationException... which points to something else. ;)

Re: Regular expression email filtering

Posted: 26 Nov 2010, 23:33
by Bladerunner
Does the system have an event log? Is there no way of checking who did not receive the email?

Re: Regular expression email filtering

Posted: 26 Nov 2010, 23:55
by SykomantiS
Not that I'm aware of. I didn't write the thing. Just have to fix it. Far as I know though, no external logs...
As far as being something else, entirely possible of course, but since looping through 300-odd students who had to be mailed (moms and dads included, so that's triple the amount of emails) only ONE exception was thrown, and was not related to memory (or out of memory, if that was your thinking)- it threw the error early on and then continued afterwards.

Re: Regular expression email filtering

Posted: 27 Nov 2010, 06:12
by -Prometheus-
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................
........................................................................................................................................................................................................

Re: Regular expression email filtering

Posted: 13 May 2011, 15:26
by SykomantiS
Thread necro of note

Code: Select all

"\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b"

Code: Select all

"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
both of these expressions allowed the following examples as accepted:
"test,mail@domian.com" <--- note comma, not period
"test1.mail@domian.com; test2.mail@domian.com" <--- two addresses separated with a semi colon.
"name@surname@domain.com" <-- 2 @ signs

Can someone please tell me why these strings are allowed, when clearly they shouldn't be?

Re: Regular expression email filtering

Posted: 13 May 2011, 16:52
by Bladerunner
My guess is because you didn't enclose it in ^...$. But that might have undesired effects if you insert it, so test first.

Just get a new Regex off Google, there are millions of expressions available for free.

Re: Regular expression email filtering

Posted: 13 May 2011, 16:53
by SykomantiS
I've gone through a couple already. I've already tried the ^...$ doesn't seem to have the desired affect. :?