django send_mail [Errno 111]连接被拒绝

时间:2017-12-02 19:30:31

标签: python django

通过发送来自django的电子邮件我遇到了问题:

Django Version: 1.11.3
Exception Type: error
Exception Value:    
[Errno 111] Connection refused
Exception Location: /usr/lib/python2.7/socket.py in create_connection, line 571
Python Version: 2.7.6

settings.py

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'mymail@gmail.com'
EMAIL_HOST_PASSWORD = '******'
EMAIL_PORT = 587

views.py

...
from django.core.mail import send_mail
...
def testmail(request):
    send_mail('subj2', 'message', 'mymail@gmail.com', ['receiver@gmail.com'])
    return HttpResponse('ok')

但是,当我尝试从控制台发送消息时:

python manage.py shell
from django.core.mail import send_mail
send_mail('subj2', 'message', 'mymail@gmail.com', ['receiver@gmail.com'])

它正在工作,我非常接受我的信息。

1 个答案:

答案 0 :(得分:1)

这就是我所做的。

views.py

from django.core.mail import EmailMessage
...
email = EmailMessage('title', 'body', to=[mailid])
email.send()
...

settings.py

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'abcd@gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587

并确保您已启用https://myaccount.google.com/lesssecureapps