简体   繁体   中英

Smtplib code working in python3.7 for linux but not for windows 10

I am trying to send automated mail through outlook using python3.7.The smtlib code is working fine for python installed in linux while the same code is returning me error for windows.

import time, os, smtplib
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders


print("sam")
s = smtplib.SMTP('smtp-mail.outlook.com', 587)
print("sam1")
s.starttls()
print("sam2")
s.login("samgupta@xyz.com", "Welcome")
message = "Hi"
print("sam3")
s.sendmail("samgupta@xyz.com", "abc@xyz.com", message)
s.quit()

This is the error message i am getting in windows python:

Traceback (most recent call last): File "C:/Users/sysadmin/Desktop/mail.py", line 10, in s = smtplib.SMTP('smtp-mail.outlook.com', 587) File "C:\Users\sysadmin\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 251, in init (code, msg) = self.connect(host, port) File "C:\Users\sysadmin\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 336, in connect self.sock = self._get_socket(host, port, self.timeout) File "C:\Users\sysadmin\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 307, in _get_socket self.source_address) File "C:\Users\sysadmin\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 727, in create_connection raise err File "C:\Users\sysadmin\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 716, in create_connection sock.connect(sa) TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Can you just try this bit? This should print the exceptions and we can fix the problem once we know the problem nature.

import smtplib
global server
try:
    server = smtplib.SMTP_SSL( < host >, < port >)
    except Exception as e:
    print("{}".format(e))

try:
    server.login("your username", "your password")
except Exception as e:
    print("{}".format(e))
server.sendmail(
    "from@address.com",
    "to@address.com",
    "this message is from python")
server.quit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM