L
L
Lelouch2020-04-29 00:44:11
Python
Lelouch, 2020-04-29 00:44:11

Why can't SMTP verify an email address on port 465?

Tell me, please, what am I doing wrong? I'm trying to check if the address exists before sending an email, but for some reason this trick only works on port 25. If you try to connect to the server via SSL, then some kind of mysticism begins.

Example 1. If connects on port 25, then everything works as expected with any server.
The code:

from dns import resolver
import smtplib

email_to_test = "[email protected]"
domain_to_test = email_to_test.split('@')[1]

email_from = '[email protected]'
host_from = email_from.split('@')[1]

def get_mx(domain):
    try:
        r = resolver.Resolver()
        r.nameservers = ['8.8.8.8', '8.8.4.4']
        mx_records = r.query(domain, 'MX')

        mx_records = [exchange.to_text().split() for exchange in mx_records]
    except (resolver.NoAnswer, resolver.NXDOMAIN, resolver.NoNameservers):
        mx_records = []

    return mx_records

def check_smtp(email_to_test, mx_record, email_from, host_from):
        print("Creating server")
        server = smtplib.SMTP(timeout = 10)
        server.set_debuglevel(0)
        print("Server defined, trying to connect")
        server.connect(mx_record, 25)
        print("Connected, trying helo")
        server.helo(host_from)
        print('Helo is ok, trying mail & rcpt')
        server.mail(email_from)
        code, message = server.rcpt(email_to_test)
        server.quit()
        return (code, message)

mx_records = get_mx(domain_to_test)

print('MX records is:')
for record in mx_records:
    print(record[1])

print('SMTP test:')
print(check_smtp(email_to_test, mx_records[0][1], email_from,host_from))

Result:
MX records is:
gmail-smtp-in.l.google.com.
alt3.gmail-smtp-in.l.google.com.
alt1.gmail-smtp-in.l.google.com.
alt4.gmail-smtp-in.l.google.com.
alt2.gmail-smtp-in.l.google.com.
SMTP test:
Creating server
Server defined, trying to connect
Connected, trying helo
Helo is ok, trying mail & rcpt
(550, b"5.1.1 The email account that you tried to reach does not exist. Please try\n5.1.1 double-checking the recipient's email address for typos or\n5.1.1 unnecessary spaces. Learn more at\n5.1.1  https://support.google.com/mail/?p=NoSuchUser p124si2609112oib.236 - gsmtp")


Example 2. Now let's try with SSL.
The code:
def check_smtp(email_to_test, mx_record, email_from, host_from):
        print("Creating server")
        server = smtplib.SMTP_SSL(timeout = 10)
        server.set_debuglevel(1)
        print("Server defined, trying to connect")
        server.connect(mx_record, 465)
        print("Connected, trying helo")
        server.helo(host_from)
        print('Helo is ok, trying mail & rcpt')
        server.mail(email_from)
        code, message = server.rcpt(email_to_test)
        server.quit()
        return (code, message)


We get socket.timeout from Gmail and most other servers:
SMTP test:
Creating server
Server defined, trying to connect
connect: ('alt1.gmail-smtp-in.l.google.com.', 465)
connect: ('alt1.gmail-smtp-in.l.google.com.', 465)
Traceback (most recent call last):
...
socket.timeout: timed out


But mail.ru suddenly says Connection refused:
SMTP test:
Creating server
Server defined, trying to connect
connect: ('mxs.mail.ru.', 465)
connect: ('mxs.mail.ru.', 465)
Traceback (most recent call last):
...
ConnectionRefusedError: [Errno 61] Connection refused

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SagePtr, 2020-04-29
@Lelouch

smtp.gmail.com - for Google users (ports 25, 465, 587 are open there).
MX-records - for other servers, only port 25 is open there, because it is used to receive letters to user boxes. Therefore, you can only connect to servers in MX records on port 25, use STARTTLS if you need encryption. Of course, the settings may differ on other mail servers, the same server may be used both for forwarding and for sending by users, or there may be several different ones configured in completely different ways.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question