I'm writing a C project that needs to validate the emails before they are stored. I will have to cross compile, so using regex.h
is not an option.
Is there a surefire way to validate an email by checking if it is a deliverable address without actually having to send an email in C?
Alternatives or suggestions are welcome if there is no way of doing this.
You can perform a DNS query of the domain name (the part of the e-mail-address after the @
symbol) to determine whether the domain is valid and has an MX record. To verify the username (the part of the e-mail-address before the @
symbol), you will have to query the destination mail server itself, for example using the VRFY
SMTP command. However, for security reasons, some mail servers may not support this command.
The C language itself does not provide any functions with which you can send network packets. However, most platforms provide an API which offers this functionality (e.g. POSIX sockets, Windows sockets). Some platforms also provide an API which performs the DNS query for you. Also, several libraries exist which provide this functionality.