2017-12-02 201 views
0

我必須通過從Django中發送我的電子郵件一個問題:Django的send_mail [錯誤111]拒絕連接

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 = '[email protected]' 
EMAIL_HOST_PASSWORD = '******' 
EMAIL_PORT = 587 

views.py

... 
from django.core.mail import send_mail 
... 
def testmail(request): 
    send_mail('subj2', 'message', '[email protected]', ['[email protected]']) 
    return HttpResponse('ok') 

但,當我嘗試從控制檯發送消息時:

python manage.py shell 
from django.core.mail import send_mail 
send_mail('subj2', 'message', '[email protected]', ['[email protected]']) 

它的工作,我sucessfuly收到我的消息。

+0

檢查你的防火牆 – eyllanesc

回答

0

這是我所做的。

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 = '[email protected]' 
EMAIL_HOST_PASSWORD = 'password' 
EMAIL_PORT = 587 

並確保已啓用https://myaccount.google.com/lesssecureapps

相關問題