4

I am a python newbie trying to email using Python 3.7. I am referring to the following python web page to put together my code. https://docs.python.org/3.7/library/email.examples.html I am not able to understand what is wrong. Do I have to update any settings in Outlook for the email to go through? Weird that the above documentation doesn't mention anything about my email password

    #!/usr/bin/env python3
    import smtplib
    from email.message import EmailMessage 
    body = "Hello User,\nThis is to notify you that...."
    msg = EmailMessage()
    msg.set_content(body)
    msg['Subject'] = 'Name Clash.....'
    msg['From'] = 'pukav@outlook.com'
    msg['To'] = 'iampukav@gmail.com'
    s = smtplib.SMTP('smtp-mail.outlook.com', 587)
    s.login('pukav@outlook.com', 'mypassword')
    s.send_message(msg)
    s.quit()

ERRORS:

in login "SMTP AUTH extension not supported by server."

smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server.

  • Have you checked if the Outlook account needs to be configurated to accept third party smtp clients ? It's very common with gmail. Never tried with Outlook though. It could be an option like _"Allow insecure applications"_ or something like that. – francovici Apr 22 '19 at 21:32
  • So far, I haven't found anything like that in Outlook. I tried using a gmail account as well and enabled the 3rd party option. But I kept getting the error "smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server." –  Apr 23 '19 at 13:46

4 Answers4

4

I had the same error for Gmail server and found the solution below.
(I am not sure this solution will be perfectly shaped for Outlook)

import smtplib
from typing import List

SMTP_HOST = "smtp.gmail.com"
SMTP_PORT = 587


def send_email(from_addr: str, to_addr: List[str], subject: str) -> None:
    msg = f"From: {from_addr}\r\nTo: {','.join(to_addr)}\r\nSubject: {subject}\r\n"

    with smtplib.SMTP(host=SMTP_HOST, port=SMTP_PORT) as server:
        server.starttls()
        server.login(SMTP_USER, SMTP_PASSWORD)
        server.sendmail(from_addr, to_addr, msg)

Important note:
When using Gmail, please remember to turn on access for less secure apps

Hunter_71
  • 649
  • 1
  • 9
  • 16
1

After creating the SMTP connection, you should login: s.login('email', 'password')

EDIT: Here is the code I have used for years. I used this to send email from a G-Suite account:

server = smtplib.SMTP_SSL(serverAddress, portNumber)
server.ehlo()
server.login(username, password)
thedadams
  • 106
  • 4
  • Yes, I tried doing that as well. "SMTP AUTH extension not supported by server.") smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server. –  Apr 22 '19 at 21:07
  • Thank you @thedadams. But, I don't think Python 3.7.3 supports SSL –  Apr 22 '19 at 21:29
  • I said that because, it gave me the following error: AttributeError: module 'smtplib' has no attribute 'SMTP_SSL' –  Apr 23 '19 at 14:23
1

Steps:

  1. Try this:

    s = smtplib.SMTP('smtp.gmail.com', 587)
    s.starttls()
    s.ehlo()
    s.login(username, password)
    
  2. If email is ***@gmail.com

    Turn this ON: https://myaccount.google.com/lesssecureapps
    If email is something else (ex. ***@outlook.com), then no need to do step 2

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
0

This works for me!

import smtplib
    
server_365 = smtplib.SMTP('smtp.office365.com', '587')
    
server_365.ehlo('mylowercasehost')

server_365.starttls()
    
server_365.ehlo('mylowercasehost')

Reference: Office365 smtp server does not respond to ehlo() in python

carkod
  • 1,844
  • 19
  • 32