如何阅读 Python 中的电子邮件内容 3
How to read email content in Python 3
我已经尝试了很多代码来访问和阅读电子邮件内容,例如,关于 Gmail,我只能进行身份验证,而对于 Outlook,我有可以读取电子邮件的代码,但它是加密的...但现在它只能访问电子邮件并且不输出加密信息。所以我需要帮助来解决这个问题。谢谢
代码如下:
import imaplib
import base64
email_user = input('Email: ')
email_pass = input('Password: ')
M = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993)
M.login(email_user, email_pass)
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
num1 = base64.b64decode(num1)
data1 = base64.b64decode(data)
print('Message %s\n%s\n' % (num, data[0][1]))
M.close()
M.logout()
无论如何我已经检查过你的代码,并且有一些评论。第一个是当您在此处粘贴代码时,逗号似乎已被删除。我已尝试将它们放回去,因此请检查我的代码是否与您的相符。请记住,如果无法访问 Outlook,我无法测试我的建议。
import imaplib
import base64
email_user = input('Email: ')
email_pass = input('Password: ')
M = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993)
M.login(email_user, email_pass)
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)') # data is being redefined here, that's probably not right
num1 = base64.b64decode(num1) # should this be (num) rather than (num1) ?
data1 = base64.b64decode(data)
print('Message %s\n%s\n' % (num, data[0][1])) # don't you want to print num1 and data1 here?
M.close()
M.logout()
假设已正确重构,您在第 10 行调用消息列表数据,然后在第 13 行将调用 fetch() 的结果重新分配给数据。
在第 14 行,您对尚未定义的 num1 进行解码。我不确定这些数字是否需要解码,这似乎有点奇怪。
在第 16 行,您打印的是编码值,而不是解码后的值。我想你可能想要像
这样的东西
import imaplib
import base64
email_user = input('Email: ')
email_pass = input('Password: ')
M = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993)
M.login(email_user, email_pass)
M.select()
typ, message_numbers = M.search(None, 'ALL') # change variable name, and use new name in for loop
for num in message_numbers[0].split():
typ, data = M.fetch(num, '(RFC822)')
# num1 = base64.b64decode(num) # unnecessary, I think
print(data) # check what you've actually got. That will help with the next line
data1 = base64.b64decode(data[0][1])
print('Message %s\n%s\n' % (num, data1))
M.close()
M.logout()
希望对您有所帮助。
使用数据[0][1].decode('utf-8')
它会起作用的。我也面临着同样的问题。它对我有用。
您只需要在您的 Gmail 帐户中启用安全性较低的应用程序,如果未启用,即使您的代码没问题,您也会收到身份验证错误。我也得到了所以我研究并得到了这个错误
我已经尝试了很多代码来访问和阅读电子邮件内容,例如,关于 Gmail,我只能进行身份验证,而对于 Outlook,我有可以读取电子邮件的代码,但它是加密的...但现在它只能访问电子邮件并且不输出加密信息。所以我需要帮助来解决这个问题。谢谢
代码如下:
import imaplib
import base64
email_user = input('Email: ')
email_pass = input('Password: ')
M = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993)
M.login(email_user, email_pass)
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
num1 = base64.b64decode(num1)
data1 = base64.b64decode(data)
print('Message %s\n%s\n' % (num, data[0][1]))
M.close()
M.logout()
无论如何我已经检查过你的代码,并且有一些评论。第一个是当您在此处粘贴代码时,逗号似乎已被删除。我已尝试将它们放回去,因此请检查我的代码是否与您的相符。请记住,如果无法访问 Outlook,我无法测试我的建议。
import imaplib
import base64
email_user = input('Email: ')
email_pass = input('Password: ')
M = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993)
M.login(email_user, email_pass)
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)') # data is being redefined here, that's probably not right
num1 = base64.b64decode(num1) # should this be (num) rather than (num1) ?
data1 = base64.b64decode(data)
print('Message %s\n%s\n' % (num, data[0][1])) # don't you want to print num1 and data1 here?
M.close()
M.logout()
假设已正确重构,您在第 10 行调用消息列表数据,然后在第 13 行将调用 fetch() 的结果重新分配给数据。
在第 14 行,您对尚未定义的 num1 进行解码。我不确定这些数字是否需要解码,这似乎有点奇怪。
在第 16 行,您打印的是编码值,而不是解码后的值。我想你可能想要像
这样的东西import imaplib
import base64
email_user = input('Email: ')
email_pass = input('Password: ')
M = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993)
M.login(email_user, email_pass)
M.select()
typ, message_numbers = M.search(None, 'ALL') # change variable name, and use new name in for loop
for num in message_numbers[0].split():
typ, data = M.fetch(num, '(RFC822)')
# num1 = base64.b64decode(num) # unnecessary, I think
print(data) # check what you've actually got. That will help with the next line
data1 = base64.b64decode(data[0][1])
print('Message %s\n%s\n' % (num, data1))
M.close()
M.logout()
希望对您有所帮助。
使用数据[0][1].decode('utf-8')
它会起作用的。我也面临着同样的问题。它对我有用。
您只需要在您的 Gmail 帐户中启用安全性较低的应用程序,如果未启用,即使您的代码没问题,您也会收到身份验证错误。我也得到了所以我研究并得到了这个错误