python - 使用 Python 的 Gmail 中的 IMAP 问题

标签 python gmail imap imaplib

我在 Python 2.7 中遇到 IMAP 问题 出于测试目的,我使用密码 testing123testing 创建了 foobar306@gmail.com 我正在关注 this tutorial并将其输入到我的 Python Iteractive Shell 中:

    Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('foobar306@gmail.com', 'testing123testing')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
>>> 

没有任何反应,甚至没有错误消息。 注意:我在 Gmail 中启用了 IMAP 谢谢,-蒂姆

更新:回应这条评论:

Did you do the next section after the code you quoted above? – Amber

我试过这个:

    Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('myusername@gmail.com', 'mypassword')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
result, data = mail.search(None, "ALL")

ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_id = id_list[-1] # get the latest

result, data = mail.fetch(latest_email_id, "(RFC822)") # fetch the email body (RFC822) for the given ID

raw_email = data[0] # here's the body, which is raw text of the whole email
# including headers and alternate payloads

>>>

还是什么都没做

最佳答案

这似乎对我有用;我通过 python API 创建了一个 sarnoldwashere 文件夹:

>>> mail.create("sarnoldwashere")
('OK', ['Success'])
>>> mail.list()
('OK', ['(\\HasNoChildren) "/" "INBOX"',
'(\\HasNoChildren) "/" "Personal"',
'(\\HasNoChildren) "/" "Receipts"',
'(\\HasNoChildren) "/" "Travel"',
'(\\HasNoChildren) "/" "Work"',
'(\\Noselect \\HasChildren) "/" "[Gmail]"',
'(\\HasNoChildren) "/" "[Gmail]/All Mail"',
'(\\HasNoChildren) "/" "[Gmail]/Drafts"',
'(\\HasNoChildren) "/" "[Gmail]/Sent Mail"',
'(\\HasNoChildren) "/" "[Gmail]/Spam"',
'(\\HasNoChildren) "/" "[Gmail]/Starred"',
'(\\HasChildren \\HasNoChildren) "/" "[Gmail]/Trash"',
'(\\HasNoChildren) "/" "sarnoldwashere"'])
>>> mail.logout()
('BYE', ['LOGOUT Requested'])

它应该仍然存在于网络界面中。 (除非其他人同时删除它。)

编辑以包括 session 的全部内容,甚至包括我重新学习 The Way of Python 的无聊部分:

>>> import imaplib
>>> mail = imaplib.IMAP4_SSL('imap.gmail.com')
>>> mail.login('foobar306@gmail.com', 'testing123testing')
('OK', ['foobar306@gmail.com .. .. authenticated (Success)'])
>>> mail.list()
('OK', ['(\\HasNoChildren) "/" "INBOX"', '(\\HasNoChildren) "/" "Personal"', '(\\HasNoChildren) "/" "Receipts"', '(\\HasNoChildren) "/" "Travel"', '(\\HasNoChildren) "/" "Work"', '(\\Noselect \\HasChildren) "/" "[Gmail]"', '(\\HasNoChildren) "/" "[Gmail]/All Mail"', '(\\HasNoChildren) "/" "[Gmail]/Drafts"', '(\\HasNoChildren) "/" "[Gmail]/Sent Mail"', '(\\HasNoChildren) "/" "[Gmail]/Spam"', '(\\HasNoChildren) "/" "[Gmail]/Starred"', '(\\HasChildren \\HasNoChildren) "/" "[Gmail]/Trash"'])
>>> # Out: list of "folders" aka labels in gmail.
... mail.select("inbox") # connect to inbox.
('OK', ['3'])
>>> mail.dir()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/imaplib.py", line 214, in __getattr__
    raise AttributeError("Unknown IMAP4 command: '%s'" % attr)
AttributeError: Unknown IMAP4 command: 'dir'
>>> dir(mail)
['PROTOCOL_VERSION', '_CRAM_MD5_AUTH', '__doc__', '__getattr__', '__init__', '__module__', '_append_untagged', '_check_bye', '_checkquote', '_cmd_log', '_cmd_log_idx', '_cmd_log_len', '_command', '_command_complete', '_dump_ur', '_get_line', '_get_response', '_get_tagged_response', '_log', '_match', '_mesg', '_new_tag', '_quote', '_simple_command', '_untagged_response', 'abort', 'append', 'authenticate', 'capabilities', 'capability', 'certfile', 'check', 'close', 'continuation_response', 'copy', 'create', 'debug', 'delete', 'deleteacl', 'error', 'expunge', 'fetch', 'getacl', 'getannotation', 'getquota', 'getquotaroot', 'host', 'is_readonly', 'keyfile', 'list', 'literal', 'login', 'login_cram_md5', 'logout', 'lsub', 'mo', 'mustquote', 'myrights', 'namespace', 'noop', 'open', 'partial', 'port', 'print_log', 'proxyauth', 'read', 'readline', 'readonly', 'recent', 'rename', 'response', 'search', 'select', 'send', 'setacl', 'setannotation', 'setquota', 'shutdown', 'sock', 'socket', 'sort', 'ssl', 'sslobj', 'state', 'status', 'store', 'subscribe', 'tagged_commands', 'tagnum', 'tagpre', 'tagre', 'thread', 'uid', 'unsubscribe', 'untagged_responses', 'welcome', 'xatom']
>>> dir(mail).sort()
>>> d=dir(mail)
>>> d.sort()
>>> d
['PROTOCOL_VERSION', '_CRAM_MD5_AUTH', '__doc__', '__getattr__', '__init__', '__module__', '_append_untagged', '_check_bye', '_checkquote', '_cmd_log', '_cmd_log_idx', '_cmd_log_len', '_command', '_command_complete', '_dump_ur', '_get_line', '_get_response', '_get_tagged_response', '_log', '_match', '_mesg', '_new_tag', '_quote', '_simple_command', '_untagged_response', 'abort', 'append', 'authenticate', 'capabilities', 'capability', 'certfile', 'check', 'close', 'continuation_response', 'copy', 'create', 'debug', 'delete', 'deleteacl', 'error', 'expunge', 'fetch', 'getacl', 'getannotation', 'getquota', 'getquotaroot', 'host', 'is_readonly', 'keyfile', 'list', 'literal', 'login', 'login_cram_md5', 'logout', 'lsub', 'mo', 'mustquote', 'myrights', 'namespace', 'noop', 'open', 'partial', 'port', 'print_log', 'proxyauth', 'read', 'readline', 'readonly', 'recent', 'rename', 'response', 'search', 'select', 'send', 'setacl', 'setannotation', 'setquota', 'shutdown', 'sock', 'socket', 'sort', 'ssl', 'sslobj', 'state', 'status', 'store', 'subscribe', 'tagged_commands', 'tagnum', 'tagpre', 'tagre', 'thread', 'uid', 'unsubscribe', 'untagged_responses', 'welcome', 'xatom']
>>> mail.list()
('OK', ['(\\HasNoChildren) "/" "INBOX"', '(\\HasNoChildren) "/" "Personal"', '(\\HasNoChildren) "/" "Receipts"', '(\\HasNoChildren) "/" "Travel"', '(\\HasNoChildren) "/" "Work"', '(\\Noselect \\HasChildren) "/" "[Gmail]"', '(\\HasNoChildren) "/" "[Gmail]/All Mail"', '(\\HasNoChildren) "/" "[Gmail]/Drafts"', '(\\HasNoChildren) "/" "[Gmail]/Sent Mail"', '(\\HasNoChildren) "/" "[Gmail]/Spam"', '(\\HasNoChildren) "/" "[Gmail]/Starred"', '(\\HasChildren \\HasNoChildren) "/" "[Gmail]/Trash"'])
>>> mail.select("INBOX") # connect to inbox.
('OK', ['3'])
>>> mail.list()
('OK', ['(\\HasNoChildren) "/" "INBOX"', '(\\HasNoChildren) "/" "Personal"', '(\\HasNoChildren) "/" "Receipts"', '(\\HasNoChildren) "/" "Travel"', '(\\HasNoChildren) "/" "Work"', '(\\Noselect \\HasChildren) "/" "[Gmail]"', '(\\HasNoChildren) "/" "[Gmail]/All Mail"', '(\\HasNoChildren) "/" "[Gmail]/Drafts"', '(\\HasNoChildren) "/" "[Gmail]/Sent Mail"', '(\\HasNoChildren) "/" "[Gmail]/Spam"', '(\\HasNoChildren) "/" "[Gmail]/Starred"', '(\\HasChildren \\HasNoChildren) "/" "[Gmail]/Trash"'])
>>> mail.list("INBOX")
('OK', ['(\\HasNoChildren) "/" "INBOX"'])
>>> mail.open("INBOX")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/imaplib.py", line 1149, in open
    self.sock = socket.create_connection((host, port))
  File "/usr/lib/python2.6/socket.py", line 547, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
>>> mail.recent()
('OK', ['0'])
>>> mail.create("sarnoldwashere")
('OK', ['Success'])
>>> mail.list()
('OK', ['(\\HasNoChildren) "/" "INBOX"', '(\\HasNoChildren) "/" "Personal"', '(\\HasNoChildren) "/" "Receipts"', '(\\HasNoChildren) "/" "Travel"', '(\\HasNoChildren) "/" "Work"', '(\\Noselect \\HasChildren) "/" "[Gmail]"', '(\\HasNoChildren) "/" "[Gmail]/All Mail"', '(\\HasNoChildren) "/" "[Gmail]/Drafts"', '(\\HasNoChildren) "/" "[Gmail]/Sent Mail"', '(\\HasNoChildren) "/" "[Gmail]/Spam"', '(\\HasNoChildren) "/" "[Gmail]/Starred"', '(\\HasChildren \\HasNoChildren) "/" "[Gmail]/Trash"', '(\\HasNoChildren) "/" "sarnoldwashere"'])
>>> mail.logout()
('BYE', ['LOGOUT Requested'])
>>> 

关于python - 使用 Python 的 Gmail 中的 IMAP 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6481771/

相关文章:

smtp - 如何使用 IMAP 发送邮件?

ssl - Mule ESB 3.3 - 接收 IMAPS 邮件 (Gmail)

jakarta-mail - javax.mail.AuthenticationFailedException : AUTHENTICATE failed. IMAP 存储未连接

python - 使用列表理解调用 for 循环内的方法

html - 保存到 Google 云端硬盘时,HTML 电子邮件中文本上的背景图像消失

python - 获取 Numpy 数组的反对角线

python - 如何在不下载附件的情况下转发完整的电子邮件?

iOS:谷歌验证码

python - 在键上切片 numpy 字典数组

python - 为数百万数据运行选择查询的有效方法