Python SMTP - [Errno 101] Network is unreachable on AWS EC2

Viewed 964

I am trying to verify the email address of the user.

When trying to connect to the user's host using MX record I always get a 'Network is unreachable' error on the AWS EC2 instance. However, it works perfectly fine locally.

records = dns.resolver.query(domain, 'MX')
mx_record = records[0].exchange
mx = str(mx_record)
smtp_server = smtplib.SMTP(host=mx)

If I try to use:

smtp_server = smtplib.SMTP_SSL(host=mx, port=465)

I get the same issue.

Please share any ideas. Thanks!

1 Answers

It does not work probably because:

AWS blocks outbound traffic on port 25 (SMTP) of all EC2 instances and Lambda functions by default. If you want to send outbound traffic on port 25, you can request for this restriction to be removed.

Although not explicitly stated I would say it is same for 465.

As explained in the link, you have to contact AWS support to remove this restriction, or use AWS SES.

Related