django EmailMessage不起作用

时间:2016-03-04 20:00:30

标签: python django email

代码:

                email1 = EmailMessage(from_email='cc@outlook.com', to=['cc@outlook.com'])
                print "email1:", type(email1)
                email1.attach(title + '.pdf', pdf, 'application/pdf')
                try:
                        print "email........"
                        print email1.send(fail_silently=False)
                except Exception as e:
                        print "Exception when sending emails:", e

输出:

email1: <class 'django.core.mail.message.EmailMessage'>
email........
1

设定:

EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
DEFAULT_FROM_EMAIL = "firstname.lastname@company.com"
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

没有例外,没有错误信息。但它无法发送电子邮件。不知道为什么以及如何调试

当我将localhost更改为我的主机名时,出现以下错误:

Exception when sending emails: [Errno 111] Connection refused

由于

1 个答案:

答案 0 :(得分:0)

您需要允许安全性较低的应用程序,可以通过单击下面的链接来完成

https://www.google.com/settings/security/lesssecureapps

也尝试这个

https://accounts.google.com/DisplayUnlockCaptcha

如果仍然无法使用,请尝试更改Gmail密码

settings.py应该是

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your-acount@gmail.com'
EMAIL_HOST_PASSWORD = 'your-gmail-password'
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER