![](/img/trans.png)
[英]Python Celery socket.error: [Errno 61] Connection refused
[英]Email cannot be sent in Python.[Errno 61] Connection refused error happens
無法使用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()
在settings.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 (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
我真的不明白為什么會發生這樣的錯誤我的代碼有什么問題我該如何解決?
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]是由於服務器拒絕了連接。 看看這是否有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.