Sending email through Python -
#!/usr/bin/python import smtplib email.mime.multipart import mimemultipart email.mime.text import mimetext email.mime.base import mimebase 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 working fine when running on local system on running on private network giving me error.
oserror: [win error 10051] socket operation attempted unreachable network.
you may want if have access private network smtp.gmail.com
on port 587
the private network admin may have setup network block outbound traffic on port 587
try:
telnet smtp.gmail.com 587
if unreachable network error network issue.
Comments
Post a Comment