【问题标题】:Django mail sending error:[ Error 10061]Django 邮件发送错误:[错误 10061]
【发布时间】:2013-03-02 08:00:37
【问题描述】:

我跟着 The Django Book 到了第 7 章。当我尝试联系的例子时,我遇到了这个邮件发送错误。

有谁知道如何解决这个问题?

这是错误信息:

/contact/ 处出错
[错误号 10061]
请求方法:POST

Request URL:    http://127.0.0.1:8000/contact/<br>

Django 版本:1.5
异常类型:错误
异常值:[Errno 10061]
异常位置:create_connection 中的 C:\Python27\lib\socket.py 第 571 行
Python 可执行文件:C:\Python27\python.exe
Python 版本:2.7.2
Python 路径:
['D:\Workspace\DJhello',
'D:\Workspace\DJhello',
'D:\Workspace\DJhello\src',
'C:\Python27\DLLs',
'C:\Python27\lib',
'C:\Python27\lib\plat-win',
'C:\Python27\lib\lib-tk',
'C:\Python27',
'C:\Python27\lib\site-packages',
'C:\Python27\lib\site-packages\win32',
'C:\Python27\lib\site-packages\win32\lib',
'C:\Python27\lib\site-packages\Pythonwin',
'C:\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg-info',
'C:\Windows\system32\python27.zip']
服务器时间:2013年3月14日星期四12:08:14 +0800enter code here

这是我的代码:

def contact(request):
    errors = []
    if request.method == 'POST':
        if not request.POST.get('subject', ''):
            errors.append('Enter a subject.')
        if not request.POST.get('message', ''):
            errors.append('Enter a message.')
        if request.POST.get('email') and '@' not in request.POST['email']:
            errors.append('Enter a valid e-mail address.')
        if not errors:
            send_mail(
                request.POST['subject'],
                request.POST['message'],
                request.POST.get('email', 'noreply@example.com'),
                ['siteowner@example.com'],
            )
            return HttpResponseRedirect('/contact/thanks/')
    return render_to_response('contact_form.html',
        {'errors': errors, 
        'subject': request.POST.get('subject', ''),
        'message': request.POST.get('message', ''), 
        'email': request.POST.get('email', '')}, 
        context_instance=RequestContext(request))

【问题讨论】:

  • 您的邮件服务器是否正在运行并等待指定 IP 和端口的连接?
  • 您的设置中有电子邮件配置吗?

标签: django email


【解决方案1】:

检查您的电子邮件设置是否正确:

settings.py:

DEFAULT_FROM_EMAIL = "youremail@gmail.com"

EMAIL_HOST = "smtp.gmail.com"

EMAIL_HOST_USER = "youremail@gmail.com"

EMAIL_HOST_PASSWORD = "yourpasswordforyourmailaccount"

EMAIL_PORT = 587

EMAIL_USE_TLS = 真

据我所知,这些是 gmail 的设置。

【讨论】:

    【解决方案2】:

    在您发送邮件之前,您必须在settings.py 中设置一些变量:

    EMAIL_HOST = 'smtp.163.com' # replace with your smtp server's address
    EMAIL_POST = 25 # replace with your smtp server's port
    EMAIL_HOST_USER = 'username' # replace with your smtp server account username
    EMAIL_HOST_PASSWORD = 'abc' # replace with your smtp server account password
    

    注意:您应该有一个 smtp 服务器或一个 smtp 服务器帐户。

    如何获得免费的 smtp 服务器帐号? This site offers free smtp services.

    也许你想深入研究 django 文档,https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-EMAIL_HOST

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-16
      • 2012-07-20
      • 2018-08-09
      • 2010-10-18
      • 2018-05-31
      • 2014-09-04
      • 1970-01-01
      • 2020-01-13
      相关资源
      最近更新 更多