DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

Test SMTP credentials using Python

You can run that test code directly in your local machine using a Python shell or by saving it into a .py file and running it from the terminal.


✅ Option 1: Run it in Python Shell (REPL)

  1. Open your terminal or command prompt.
  2. Start the Python shell:
   python
Enter fullscreen mode Exit fullscreen mode
  1. Paste the code line by line:
import smtplib

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('[email protected]', 'axifabumqxffftmnuyf')
print("Login success!")
server.quit()
Enter fullscreen mode Exit fullscreen mode

If it prints Login success!, the SMTP connection is working, and the issue is likely within your Django project config or a firewall block specific to that process.


✅ Option 2: Save and Run as a Python File

  1. Create a file, e.g., smtp_test.py.
  2. Paste the code:
import smtplib

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('[email protected]', 'axifabumqxffftmnuyf')
print("Login success!")
server.quit()
Enter fullscreen mode Exit fullscreen mode
  1. Run it in your terminal:
python smtp_test.py
Enter fullscreen mode Exit fullscreen mode

✅ Option 3: Save and Run as a Python File

To test if Django can send mail:

python manage.py shell
Enter fullscreen mode Exit fullscreen mode

Then paste:

from django.core.mail import send_mail

send_mail(
    'Test Subject',
    'This is the test message.',
    '[email protected]',  # From email
    ['[email protected]'],  # To email
    fail_silently=False
)
Enter fullscreen mode Exit fullscreen mode

If it succeeds, you’ll get an email. If it fails, it will show the real error.


🔍 What This Tells You

  • If it succeeds, your credentials and Gmail settings are correct, and the issue lies in how Django is attempting to connect (firewall, TLS settings, or environmental variable parsing).
  • If it fails with the same ConnectionRefusedError, your local machine is blocking the connection — usually a firewall, antivirus, or network setting.

Top comments (0)