django发送邮件时报错 SMTPSenderRefused  (501, b'Mail from address must be same as authorization user.', 'webmaster@localhost') ,Connection unexpectedly closed

原因没有开启 TLS

应设置 EMAIL_USE_TLS = True       #    QQ邮箱必须开启

开启后又报错:Connection unexpectedly closed

原因:端口号可能有问题,换端口号

EMAIL_PORT = 25       成功

 

 完整设置如下:

settings.py

STATIC_URL = '/static/'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.qq.com' # 如果是 163 改成 smtp.163.com
EMAIL_PORT = 25 # 或者 995是设置了 SSL 加密方式
EMAIL_HOST_USER = '321459055@qq.com' # 发送者账号
EMAIL_HOST_PASSWORD = 'nweghpbjusqtbhie' # 授权码(****
EMAIL_USE_TLS = True # QQ邮箱为True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

views.py

 

def send_email(request):
from_email = settings.DEFAULT_FROM_EMAIL
send_mail('MyDjango test2', 'This is Django', from_email, ['1848872115@qq.com', ])
return HttpResponse('send success!')



 

urls.py

  urlpatterns = [
    path('admin/', admin.site.urls),
    path('send/',send_email),
  ]

 

django发送邮件时报错 SMTPSenderRefused  (501, b'Mail from address must be same as authorization user.', 'webmaster@localhost') ,Connection unexpectedly closed