SMTP Details are correct but getting authentication error

32 Views Asked by At

I am trying to send mail in python using smtp credentials, the same credentials are working for grafana but not with my python script.

import smtplib

port=portno
server_address = 'server address'
smtp_username = 'username'
smtp_password = 'password'

try:
    server = smtplib.SMTP(server_address,port)
    server.starttls()
    server.login(smtp_username, smtp_password)
    print("Login to SMTP server successful.")
except smtplib.SMTPAuthenticationError:
    print("Failed to authenticate. Please check your username and password.")
except smtplib.SMTPException as e:
    print("An error occurred while sending the email:", str(e))
except Exception as e:
    print("An unexpected error occurred:", str(e))
finally:
    if 'server' in locals():
        server.quit()

here is the output.

Failed to authenticate. Please check your username and password.

I am expecting to get the output as Login to smtp server is successful.

0

There are 0 best solutions below