Unable to send email notification in outlook using paython

Mr Sonbir 40 Reputation points
2024-05-21T13:01:38.1333333+00:00

Hi Team,

Unable to send email notification in outlook using paython

import smtplib, ssl

smtp_server = "smtp-mail.outlook.com"

sender_email = "@.com"

receiver_email = "***@***l.com"

smtp_port = 467

password = input("test: ")

#print("Running mail file")

message = """\

Subject: Hi Outlook emiail recived.

This message is sent from Python."""

context = ssl.create_default_context()

with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:

server.login(sender_email, password)

print("logged in")

server.sendmail(sender_email, receiver_email, message)

Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
4,986 questions
0 comments No comments
{count} votes

Accepted answer
  1. Azizkhon Ishankhonov 355 Reputation points
    2024-05-21T18:38:15.97+00:00

    Hi

    Here are the potential causes and solutions based on the code you provided:

    1. Authentication Issues:

    • Incorrect Credentials: Double-check that you're entering the correct sender email address and password. Be mindful of typos or extra spaces.
    • Two-Factor Authentication (2FA): If 2FA is enabled for your Outlook account, you'll need to generate an app password:
      1. Go to your Microsoft account security settings (https://account.microsoft.com/account/manage-my-account).
        1. Under "App passwords," create a new password specifically for your Python script.
          1. Use the generated app password in your code instead of your regular Outlook password.

    2. Outlook Security Restrictions:

    • Less Secure App Access Disabled: Outlook might have disabled access for less secure apps. You can try temporarily enabling it, but it's generally recommended to use a more secure method (like app passwords). Here's how to enable it (not recommended for long-term use):
      1. Go to your Microsoft account security settings (https://account.microsoft.com/account/manage-my-account).
        1. Under "Advanced security," find "Less secure app access."
          1. Turn on the toggle.

    3. SMTP Port Misconfiguration:

    • Port 467: While port 465 is commonly used for SMTP with SSL, Outlook might require port 467. Try changing the smtp_port variable to 467.

    4. Code Enhancements:

    • Error Handling: Wrap the server.login and server.sendmail calls in a try-except block to catch potential errors and provide informative messages:

    Python

    try:
        server.login(sender_email, password)
        print("Logged in")
        server.sendmail(sender_email, receiver_email, message)
        print("Email sent successfully!")
    except Exception as e:
        print(f"Error sending email: {e}")
    

    Revised Code:

    Python

    import smtplib, ssl
    smtp_server = "smtp-mail.outlook.com"
    sender_email = "your_email@outlook.com"  # Replace with your actual email
    receiver_email = "recipient@example.com"
    smtp_port = 467  # Might be 465 depending on your Outlook configuration
    password = input("Enter your app password (or Outlook password if 2FA is disabled): ")
    message = """\
    Subject: Hi Outlook email received.
    This message is sent from Python."""
    context = ssl.create_default_context()
    with smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) as server:
        try:
            server.login(sender_email, password)
            print("Logged in")
            server.sendmail(sender_email, receiver_email, message)
            print("Email sent successfully!")
        except Exception as e:
            print(f"Error sending email: {e}")
    
    

    Additional Tips:

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful