Python Send Email Error: smtplib.SMTPException: SMTP AUTH extension not supported by server

Python sends e-mail. The 126 e-mail box used before is normal, but today it is changed to Hotmail, and this error has been reported all the time

smtplib.SMTPE xception:SMTPAUTHextensionnotsupportedbyserver

my previous code is as follows:

def sendmail(receivers,mailtitle,mailcontent):

    ret=True
    # try:
    # msg=MIMEText(mailcontent,'plain','utf-8')
    msg = MIMEText(mailcontent, 'html', 'utf-8')
    msg['From']=formataddr(["I'm Black",sender]) # brackets correspond to sender's email nickname, sender's email account
    # msg['To']=formataddr(["Recipient nickname",receivers]) # The one in brackets corresponds to the recipient's email nickname, recipient's email account
    msg['Subject']=mailtitle # The subject of the message, also known as the title

    server = None
    # Determine if it is an SSL connection
    if flag_mail_ssl:
        server=smtplib.SMTP_SSL(mail_host, mail_port) # SMTP server in sender's mailbox
    else:
        server=smtplib.SMTP(mail_host, mail_port)#
    server.login(mail_user, mail_pass) # the brackets correspond to the sender's mailbox account, mailbox password
    server.sendmail(sender,receivers,msg.as_string()) # The brackets correspond to the sender's mailbox account, the recipient's mailbox account, and the sent mail
    server.quit()# close the connection
    # except Exception as e:# If the statement in try is not executed, the following ret=False will be executed
    # ret=False
    return ret

After searching the Internet, add the following two lines of code before login:

server.ehlo() # Send SMTP 'ehlo' command to Gamil
server.starttls()

That’s it. The final code is:

def sendmail(receivers,mailtitle,mailcontent):
    ret=True
    # try:
    # msg=MIMEText(mailcontent,'plain','utf-8')
    msg = MIMEText(mailcontent, 'html', 'utf-8')
    msg['From']=formataddr(["I'm Black",sender]) # brackets correspond to sender's email nickname, sender's email account
    # msg['To']=formataddr(["Recipient nickname",receivers]) # The one in brackets corresponds to the recipient's email nickname, recipient's email account
    msg['Subject']=mailtitle # The subject of the message, also known as the title

    server = None
    # Determine if it is an SSL connection
    if flag_mail_ssl:
        server=smtplib.SMTP_SSL(mail_host, mail_port) # SMTP server in sender's mailbox
    else:
        server=smtplib.SMTP(mail_host, mail_port)#
    server.ehlo() # Send SMTP 'ehlo' command to Gamil
    server.starttls()
    server.login(mail_user, mail_pass) # The corresponding mailbox account and mailbox password in parentheses is the sender's mailbox
    server.sendmail(sender,receivers,msg.as_string()) # The mail accounts in parentheses are the sender's mailbox account, the recipient's mailbox account, and the sent mail
    server.quit()# close the connection
    # except Exception as e:# If the statement in try is not executed, the following will be executed ret=False
    #     ret=False
    return ret

 

Similar Posts: