Using the same credentials, I'm able to use the SMTP server to send emails in my C# code. But can't seem to make it work on Python for some reason.
This is my code:
def sendEmail(e):
SMTPserver = 'outlook.office365.com'
sender = 'x'
destination = ['x']
USERNAME = "x"
PASSWORD = "x"
text_subtype = 'plain'
content="""\
Test message
"""
subject = str(e)
try:
msg = MIMEText(content, text_subtype)
msg['Subject'] = subject
msg['From'] = sender
conn = SMTP(SMTPserver) #THIS IS WHERE IT GETS STUCK
conn.set_debuglevel(False)
conn.login(USERNAME, PASSWORD)
try:
conn.sendmail(sender, destination, msg.as_string())
finally:
conn.quit()
except Exception:
print( "mail failed" ) # give a error message
print(traceback.format_exc())
It gets stuck here: "conn = SMTP(SMTPserver)".
Any ideas?
Edit: Also, weirdly telnet gives me this ->
C:\Users\x>telnet outlook.office365.com Connecting To outlook.office365.com...Could not open connection to the host, on port 23: Connect failed