views:

251

answers:

2

Hello.

Since Delphi does not have any regular expressions library built-in, have you seen a good function to validate email addresses at least to some degree in using only Delphi RTL/VCL?

I don't want to link additional *.dll to my product integrating regular expression support and I need also Delphi 7 compatibility. Embedding regex library into exe increases it's size and I doubt it worths adding next 100k because you just need 10-50-lines function of email validation.

+3  A: 

The big problem on email address validation is that RFC 822 is so broad, that no regular expression will help.

The article I Knew How To Validate An Email Address Until I Read The RFC describes this well.
It quotes the RFC the local-part MUST be interpreted and assigned semantics only by the host specified in the domain part of the address (the local part is the part before the @ sign in an email address).

So:
the only way to check if an email address is valid, is to setup an SMTP connection to the host accepting mail for that particular domain, and start up the email handshaking process with the email address you are trying to verify.

This is what a lot of anti-SPAM software does to verify sender email addresses: they contact the SMTP server of the sender of the email, try to setup an SMTP handshake, and if that goes OK, they rate the incoming email as more like to not be SPAM.

You can use the Indy SMTP client component to send mail; the accepted answer to this question explains how.

--jeroen

Jeroen Pluimers
Personally I would only enforce:1. Have only one @ char in the string (with index > 0)2. Have no characters in range [0..32] in the string.3. Have at least one "." character after the @'s index (with a distance of 1 or more).4. No : or / or \ characters in the domain name.
alex
RFC 822 had been obsoleted by 2822 which removed some syntax allowed by 822. Anyway one rarely needs the full 2822 syntax - allowing for a much simpler regex
ldsandon
@Jeroen: you do not need to do an SMTP email handshake just to validate an address. Yes, you have to connect to the email domain's SMTP server, but after that you can use the SMTP `VRFY` command to ask the server if the address is valid. Indy's TIdSMTP component has a Verify() method for that.
Remy Lebeau - TeamB
@Remy: I totally forgot about the VRFY, but now that you mention it, I do remember that not all SMTP servers support it. But it has been a long time since I came across one that didn't support it. So thanks for mentioning VRFY.
Jeroen Pluimers
+3  A: 

Hi everyone

As requested I'm making my comment an answer..

http://www.howtodothings.com/computers/a1169-validating-email-addresses-in-delphi.html

Thanks!

Marko
Note that that code implements an obsolete spec. It was even obsolete at the time the code was first published.
Rob Kennedy
@Rob Kennedy: Do you mean CharInSet warning?
FractalizeR
No, I mean that the code implements RFC 822. The code was published in May 2001. RFC 2822 was published in April 2001.
Rob Kennedy
@Rob Kennedy: Ah, I see, thanks. But that would be probably enough for me anyway. 2822 is more strict as I know.
FractalizeR