0

I am now using Django as the backend API endpoints and I want to send out email to for notification.

settings.py

#Email settings
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'test.'
EMAIL_HOST_USER = 'test@gmail.com'
EMAIL_PORT = 587

views.py

    def GetEmailNotification(request):
       subject = "This is a test email"
       message = "This is a test message"
       from_email = 'test@gmail.com'
       send_mail(subject, message, from_email, ['test@gmail.com'], fail_silently=False)
       return HttpResponse("sent!")

urls.py

urlpatterns = [path('emailNotification/', GetEmailNotification)]

But when I did the get request. it gave the error

OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions

After I changed the port in the setting.py from 457 to 1000, it gave the error of

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

How to solve this issue? Any ideas?

fancylynn
  • 139
  • 1
  • 12

1 Answers1

0

Try to run your Django-server on different port like example : 8080

python manage.py runserver 0.0.0.0:8080

and try to hit the same URL with postman

and also modify urls.py

path('emailNotification', views.GetEmailNotification, name='GetEmailNotification'),

also check if you have CRF enabled and that is preventing API call (security issue shouldn't be disabled on production).

#'django.middleware.csrf.CsrfViewMiddleware',
Ashfaque Ali Solangi
  • 1,883
  • 3
  • 22
  • 34