电子邮件无法使用Python发送。我想使用Gmail发送电子邮件。 我在views.py中写了代码,例如
> aovModel
Call:
aov(formula = value ~ group, data = narrowData)
Terms:
group Residuals
Sum of Squares 34.86206 216.92436
Deg. of Freedom 6 28
Residual standard error: 2.783397
Estimated effects may be unbalanced
> summary(aovModel)
Df Sum Sq Mean Sq F value Pr(>F)
group 6 34.86 5.810 0.75 0.615
Residuals 28 216.92 7.747
>
在settings.py
def mail(mail_adress, docx):
msg = MIMEMultipart()
msg["Subject"] = "TEST"
msg["From"] = settings.EMAIL_HOST_USER
msg["To"] = email
message = "TEST"
body = MIMEText(message)
msg.attach(body)
mime = {'type': 'text', 'subtype': 'docx'}
attach_file = {'name': 'test.docx', 'path': '/Users/test.docx'}
attachment = MIMEBase(mime['type'], mime['subtype'])
file = open(attach_file['path'], 'rb')
attachment.set_payload(file.read())
file.close()
encoders.encode_base64(attachment)
msg.attach(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=attach_file['name'])
return msg
def send(from_addr, to_addrs, msg):
smtp = smtplib.SMTP()
smtp.connect()
from_addr = 'xxxx@gmail.com'
from_addr_name = "test"
to_addr= "yyyyy@gmail.com"
subject = u"test"
message = "test"
body = MIMEText(message)
mime = {'type': 'text', 'subtype': 'docx'}
attach_file = {'name': 'test.docx', 'path': '/Users/test.docx'}
msg = mail(from_addr, from_addr_name, to_addr, subject, body, mime, attach_file)
smtp.sendmail(from_addr, to_addrs, msg)
smtp.close()
send()
运行代码时, [Errno 61]发生连接拒绝错误。Traceback说
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'xxxx@gmail.com'
EMAIL_HOST_PASSWORD = 'xxxxxxxxx'
EMAIL_USE_TLS = True
我真的不明白为什么会发生这样的错误。我的代码有什么问题?我该如何解决?
答案 0 :(得分:0)
Settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'xxxx@gmail.com'
EMAIL_HOST_PASSWORD = 'xxxxxxxxx'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
转到您的Google帐户设置,找到安全性->帐户权限->访问安全性较低的应用程序,启用安全性较低的应用程序登录选项。 https://support.google.com/accounts/answer/6010255
[Errorno 61]是由于服务器拒绝了连接。看看这是否有帮助。