gpt4 book ai didi

python - 无法在Python中发送电子邮件。[Errno 61]发生连接拒绝错误

转载 作者:行者123 更新时间:2023-12-01 08:55:34 24 4
gpt4 key购买 nike

无法在 Python 中发送电子邮件。我想使用 Gmail 发送电子邮件。我在views.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()

在设置.py

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'xxxx@gmail.com'
EMAIL_HOST_PASSWORD = 'xxxxxxxxx'
EMAIL_USE_TLS = True

当我运行代码时,[Errno 61] 连接被拒绝错误发生。Traceback 说

Traceback (most recent call last):
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/xxxx/Downloads/legal_doc_mail/common/views.py", line 400, in make_document
send()
File "/Users/xxxx/Downloads/legal_doc_mail/common/views.py", line 454, in send
smtp.connect()
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/smtplib.py", line 307, in _get_socket
self.source_address)
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/Users/xxxx/anaconda3/envs/py36/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

我真的不明白为什么会发生这样的错误。我的代码出了什么问题?我应该如何解决这个问题?

最佳答案

  1. 设置.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
  2. 转至您的 Google 帐户设置,找到安全 -> 帐户权限 -> 访问安全性较低的应用,启用允许安全性较低的应用登录选项。 https://support.google.com/accounts/answer/6010255

[Errorno 61] 是由于服务器拒绝连接造成的。看看这是否有帮助。

关于python - 无法在Python中发送电子邮件。[Errno 61]发生连接拒绝错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52798560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com