使用Python 3.6中的IMAPlib访问Gmail帐户

时间:2017-11-11 13:25:31

标签: python-2.7 python-3.6 imaplib

这是我在python 3.6中的代码:

import imaplib, email, os
from imaplib import *

username = 'username@gmail.com'
password = 'password'

print("Logging into your Gmail Account...")

mail = imaplib.IMAP4_SSL('imap.gmail.com',993)
mail.login(username,password)

运行上面的代码时,我收到以下错误:

Traceback (most recent call last):
File "C:/Users/user/AppData/Local/Programs/Python/Python36/imap.py", line 10, in <module>
mail = imaplib.IMAP4_SSL('imap.gmail.com',993)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 1283, in __init__
IMAP4.__init__(self, host, port)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 197, in __init__
self.open(host, port)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 1296, in open
IMAP4.open(self, host, port)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 294, in open
self.sock = self._create_socket()
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 1286, in _create_socket
sock = IMAP4._create_socket(self)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\imaplib.py", line 284, in _create_socket
return socket.create_connection((self.host, self.port))
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\socket.py", line 722, in create_connection
raise err
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\socket.py", line 713, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

请帮助我解决上述问题。我尝试了很多解决方案但没有得到答案。我在python 2.7中尝试过但是同样的问题仍然存在。

0 个答案:

没有答案