Objective: Verify if an email address is valid.
The easiest way to verify an email address is to send an email to that email address and if your email does not get bounced, then there is a high chance that it’s a valid email address. This will work most of the time, but not necessarily if there’s a catch-all email account defined. A catch-all usually refers to a mailbox on a domain that will “catch all” of the emails addressed to the domain that do not exist in the mail server.
The other way is to check the SMTP (Simple Mail Transfer Protocol) response of the mail server. How to do that? Let’s look at an example. Let’s say that we want to check if the email address “404@gmail.com
” is valid. First, we need to determine the SMTP servers for “@gmail.com
“. This can be done by querying the DNS MX records for the domain gmail.com
.
1 2 3 4 5 6 |
# dig gmail.com mx +short 5 gmail-smtp-in.l.google.com. 10 alt1.gmail-smtp-in.l.google.com. 20 alt2.gmail-smtp-in.l.google.com. 30 alt3.gmail-smtp-in.l.google.com. 40 alt4.gmail-smtp-in.l.google.com. |
Based on the output of the dig query, we have received 5 SMTP server addresses with different priorities. The smaller the number, the higher the priority, so let’s choose “gmail-smtp-in.l.google.com
” as the SMTP server that we want to use.
First, we connect to the SMTP server on port 25 – the well known port for SMTP servers. I am using netcat
(nc) to connect to the SMTP server. If you do not have netcat
installed, you can use the telnet
command instead.
1 2 |
$ nc gmail-smtp-in.l.google.com 25 220 mx.google.com ESMTP v95si24868247ioi.98 - gsmtp |
Once connected, the server prints a short message and waits for input. We start the conversation by sending the “HELO
” command.
1 2 |
HELO 250 mx.google.com at your service |
Once we send “HELO
“, the server responds with another message (starting with 250). Now we send the sender’s email address to the server using the “MAIL FROM
” command. Let’s use “foo@bar.com
” as the sender’s email address.
1 2 |
MAIL FROM:<foo@bar.com> 250 2.1.0 OK v95si24868247ioi.98 - gsmtp |
Note that the server responds with a “250 OK”, it means we can proceed further. Now, we send the recipient email address or the email address that we want to verify using the “RCPT TO
” command. We are going to verify the email address “404@gmail.com
“.
1 2 3 4 5 |
RCPT TO:<404@gmail.com> 550-5.1.1 The email account that you tried to reach does not exist. Please try 550-5.1.1 double-checking the recipient's email address for typos or 550-5.1.1 unnecessary spaces. Learn more at 550 5.1.1 https://support.google.com/mail/answer/6596 v95si24868247ioi.98 - gsmtp |
If you see the server response, it is saying that the email address does not exist. This means that the email address is not valid. The response could be different for some other type of email addresses. For example, the response for “support@gmail.com
” is different – is says that the account is disabled.
1 2 |
RCPT TO:<support@gmail.com> 550 5.2.1 The email account that you tried to reach is disabled. t76si23530949pfi.226 - gsmtp |
Note the server response code is 550 when an email address is either not valid or disabled. If you enter your own gmail account, you will get a response of 250. Replace “validemail@gmail.com
” with your own email address.
1 2 |
RCPT TO:<validemail@gmail.com> 250 2.1.5 OK a123si24991430ioe.104 - gsmtp |
Finally to close the connection, use the “QUIT
” command.
1 2 |
QUIT 221 2.0.0 closing connection m65si23697811pfi.251 - gsmtp |
In summary, you will need to use the “HELO”, “MAIL FROM” and “RCPT TO” commands when talking to a SMTP server. You can find the full SMTP conversation below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ nc gmail-smtp-in.l.google.com 25 220 mx.google.com ESMTP m65si23697811pfi.251 - gsmtp HELO 250 mx.google.com at your service MAIL FROM:<foo@bar.com> 250 2.1.0 OK m65si23697811pfi.251 - gsmtp RCPT TO:<404@gmail.com> 550-5.1.1 The email account that you tried to reach does not exist. Please try 550-5.1.1 double-checking the recipient's email address for typos or 550-5.1.1 unnecessary spaces. Learn more at 550 5.1.1 https://support.google.com/mail/answer/6596 v95si24868247ioi.98 - gsmtp QUIT 221 2.0.0 closing connection m65si23697811pfi.251 - gsmtp |