SMTP on AWS private subnet EC2, Network unreachable [errorno 101]

727 views Asked by At

Trying to send emails using SMTPlib in python script on private subnet EC2 machine using following code. EC2 machine has communication with internal SMTP server through PORT 25 , verified using telnet command. This code works fine from public subnet EC2 but throws error mentioned at the bottom on private subnet.

import smtplib
from email.MIMEMultipart import MIMEMultipart  #python 2


msg = MIMEMultipart()
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg['Subject'] = 'simple email in python'
message = 'here is the email'


mailserver = smtplib.SMTP('smtp.gmail.com',25)

mailserver.ehlo()

mailserver.starttls()

mailserver.ehlo()
mailserver.login('[email protected]', 'password')

mailserver.sendmail('[email protected]','[email protected]',msg.as_string())

mailserver.quit()

Getting this error socket.error: errorno[101] - Network is unreachable

2

There are 2 answers

0
Tomek Klas On
  • do you have a NAT gateway in the private subnet where your smtp is located?
  • do you have active access control layer on that private subnet where your server is located? Is it blocking anything?
  • also check ACL rules in public subnet
  • security group attached to the server, is it open?
0
Kunal Rathod On

Email server configuration was wrong and the email server did not require login.

mailserver = smtplib.SMTP(internal office server,25)


#mailserver.login('[email protected]', 'password') -- Not required for the server

Thank you.