在 python 中查看电子邮件时出现 Imap 错误
Imap Error while checking email in python
我正在使用 Python 3.7.7 并想使用以下代码查看我的 gmail 电子邮件:
import smtplib
import time
import imaplib
import email
mail = imaplib.IMAP4_SSL("imap.gmail.com")
mail = imaplib.IMAP4_SSL("imap.gmail.com")
mail.login("tanmay.noreply@gmail.com","mypassword")
mail.select('inbox')
type, data = mail.search(None, 'ALL')
mail_ids = data[0]
id_list = mail_ids.split()
first_email_id = int(id_list[0])
latest_email_id = int(id_list[-1])
for i in range(latest_email_id,first_email_id, -1):
typ, data = mail.fetch(i, '(RFC822)' )
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
email_subject = msg['subject']
email_from = msg['from']
print('From : ') + email_from + '\n'
print('Subject : ')+ email_subject + '\n'
但是我收到这个错误:
Traceback (most recent call last):
File "C:/Users/jeeva/Desktop/Tanmay_new/python/stackimap.py", line 19, in read_email_from_gmail
typ, data = mail.fetch(i, '(RFC822)' )
File "C:\Program Files\Python37\lib\imaplib.py", line 534, in fetch
typ, dat = self._simple_command(name, message_set, message_parts)
File "C:\Program Files\Python37\lib\imaplib.py", line 1196, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "C:\Program Files\Python37\lib\imaplib.py", line 961, in _command
data = data + b' ' + arg
TypeError: can't concat int to bytes
请帮助我。我在 this website 中找到了 python 2 片段并转换为 python 3.
注意到作者喜欢imap_tools库,完整回复:
高级库简短示例:
from imap_tools import MailBox, AND
# get list of email subjects from INBOX folder
with MailBox('imap.mail.com').login('test@mail.com', 'password', 'INBOX') as mailbox:
subjects = [msg.subject for msg in mailbox.fetch()]
https://github.com/ikvk/imap_tools
- 已解析的电子邮件属性
- 用于搜索电子邮件的查询生成器
- 处理文件夹中的电子邮件(复制、删除、标记、移动、查看)
- 使用邮箱文件夹(列出、设置、获取、创建、存在、重命名、删除、状态)
我正在使用 Python 3.7.7 并想使用以下代码查看我的 gmail 电子邮件:
import smtplib
import time
import imaplib
import email
mail = imaplib.IMAP4_SSL("imap.gmail.com")
mail = imaplib.IMAP4_SSL("imap.gmail.com")
mail.login("tanmay.noreply@gmail.com","mypassword")
mail.select('inbox')
type, data = mail.search(None, 'ALL')
mail_ids = data[0]
id_list = mail_ids.split()
first_email_id = int(id_list[0])
latest_email_id = int(id_list[-1])
for i in range(latest_email_id,first_email_id, -1):
typ, data = mail.fetch(i, '(RFC822)' )
for response_part in data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1])
email_subject = msg['subject']
email_from = msg['from']
print('From : ') + email_from + '\n'
print('Subject : ')+ email_subject + '\n'
但是我收到这个错误:
Traceback (most recent call last):
File "C:/Users/jeeva/Desktop/Tanmay_new/python/stackimap.py", line 19, in read_email_from_gmail
typ, data = mail.fetch(i, '(RFC822)' )
File "C:\Program Files\Python37\lib\imaplib.py", line 534, in fetch
typ, dat = self._simple_command(name, message_set, message_parts)
File "C:\Program Files\Python37\lib\imaplib.py", line 1196, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "C:\Program Files\Python37\lib\imaplib.py", line 961, in _command
data = data + b' ' + arg
TypeError: can't concat int to bytes
请帮助我。我在 this website 中找到了 python 2 片段并转换为 python 3.
注意到作者喜欢imap_tools库,完整回复:
高级库简短示例:
from imap_tools import MailBox, AND
# get list of email subjects from INBOX folder
with MailBox('imap.mail.com').login('test@mail.com', 'password', 'INBOX') as mailbox:
subjects = [msg.subject for msg in mailbox.fetch()]
https://github.com/ikvk/imap_tools
- 已解析的电子邮件属性
- 用于搜索电子邮件的查询生成器
- 处理文件夹中的电子邮件(复制、删除、标记、移动、查看)
- 使用邮箱文件夹(列出、设置、获取、创建、存在、重命名、删除、状态)