0

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

90abyss
  • 7,037
  • 19
  • 63
  • 94
  • Can you try adding `conn.starttls()` on the line just before `conn.login` ? Are you sure you're connecting on the appropriate port? It should be `SMTP(servername, port_number)` – sytech Aug 10 '16 at 20:28
  • It gets stuck on this line "conn = SMTP(SMTPserver)", so doesn't go below only. – 90abyss Aug 10 '16 at 20:30
  • Shouldn't the server be `smtp.office365.com` on port 587? [Reference](https://support.office.com/en-us/article/POP-and-IMAP-settings-for-Outlook-Office-365-for-business-7fc677eb-2491-4cbc-8153-8e7113525f6c) – sytech Aug 10 '16 at 20:32
  • @Gator_Python It's giving me a different error now. Posted here: https://stackoverflow.com/questions/38886594/ – 90abyss Aug 11 '16 at 03:03

0 Answers0