Django -在shell中发送电子邮件,但不能在本地主机上发送- ConnectionRefusedError

pbwdgjma  于 2023-06-07  发布在  Go
关注(0)|答案(4)|浏览(139)

我是Django的新手,但我已经阅读了大约50页(主要是SO和Django文档),还没有找到任何适合我的情况的东西。
我只是想通过Django中的send_mail函数发送一封电子邮件;虽然我希望通过Gmail上的SMTP来实现这一点,但现在它甚至不能通过localhost工作。当我运行我的test_email.py文件来测试这个send_mail函数时,我得到以下错误:

ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝连接

当我在shell(通过python www.example.com shell)中运行相同的send_mail调用时manage.py,它完全正常,并显示了我所期望的电子邮件。只有一次我尝试在Django中运行它,错误才会弹出。请注意,无论我在settings.py文件中放置以下2组设置的哪种排列,都会出现上述错误:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False

我还在我的www.example.com文件中使用了以下设置settings.py,以及这些设置与前一个设置的各种混合(尽管我想我已经提前看到了localhost是如何不工作的):

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myemail@gmail.com' # Note I'm using my actual gmail address here in real life
EMAIL_HOST_PASSWORD = '****' # Note I'm using my actual gmail password here in real life

DEFAULT_FROM_EMAIL = 'myemail@gmail.com' # Note I'm using my actual gmail address here in real life
SERVER_EMAIL = 'myemail@gmail.com' # Note I'm using my actual gmail address here in real life

当我尝试运行localhost路由时,我在CMD提示符下运行以下命令:

python -m smtpd -n -c DebuggingServer localhost:1025

下面是我试图在Django中运行的test_email.py文件,在这里我得到了连接拒绝错误:

from django.core.mail import send_mail,EmailMessage

from django.conf import settings
settings.configure()

# Create your tests here.

# Email test #1: send email from my personal email to myself
send_mail(
    'Email Test #1',
    'Test if I can send email from my personal Gmail account to another email account via Django project.',
    settings.EMAIL_HOST_USER,
    ['myemail@gmail.com'],
    fail_silently=False
    )

任何关于为什么这甚至不能通过本地主机工作的想法或其他尝试的建议都将非常感谢。
谢谢!

t30tvxxf

t30tvxxf1#

我觉得gmail smtp服务有一些问题。尝试使用smtp服务表单sendgrid。基本使用也是免费的。我之前也遇到过同样的问题。
项目settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'sendgridusername'
EMAIL_HOST_PASSWORD = 'sedgridpasseord'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'yourmail@site.com'

静止误差

Try if `smtp` service is working on your server.
vc6uscn9

vc6uscn92#

以下配置对我有效:

settings.py

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = 'sendgrid-api-key' # this is your API key
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'your-email@example.com' # this is the sendgrid email

我使用了这个配置,它工作正常,我强烈建议使用环境变量来填充EMAIL_HOST_PASSWORDDEFAULT_FROM_EMAIL

import os # this should be at the top of the file

# ...

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD", "")
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL", "")

要发送电子邮件,请使用以下代码:

from django.conf import settings
from django.core.mail import send_mail

send_mail('This is the title of the email',
          'This is the message you want to send',
          settings.DEFAULT_FROM_EMAIL,
          [
              settings.EMAIL_HOST_USER, # add more emails to this list of you want to
          ]
)
wkyowqbh

wkyowqbh3#

确保您的设置位于project settings.py而不是app settings.py中,如下所示

EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False

注意EMAIL_HOST_USER的空字符串,我认为这是你的错误
一旦你运行你的虚拟python SMTP

python -m smtpd -n -c DebuggingServer localhost:1025

您将能够发送到您的本地smtp服务器没有问题。Source

h22fl7wq

h22fl7wq4#

在确认邮件和密码正确后,尝试在您的谷歌帐户中启用此选项。
https://myaccount.google.com/lesssecureapps?pli=1
Google阻止访问不安全的应用程序。
你的代码似乎是正确的!

相关问题