devesh2791
devesh2791

Reputation: 21

Sending email through Python

#!/usr/bin/python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
fromaddr = "sender mail id"
toaddr = "receiver mail id"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Test"
body = "Test mail"
msg.attach(MIMEText(body, 'plain'))
filename = "foo.txt"
attachment = open(r"F:\python\foo.txt", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

this script is working fine when I running on my local system but on running on private network its giving me a error.

OSError: [Win Error 10051] A socket operation was attempted to an unreachable network.

Upvotes: 2

Views: 519

Answers (1)

Marty
Marty

Reputation: 2963

You may want to look into if you have access from your private network to smtp.gmail.com on port 587

The private network admin may have setup the network to block outbound traffic on port 587

Try:

telnet smtp.gmail.com 587

If you get an unreachable network error its definitely a network issue.

Upvotes: 1

Related Questions