无法使用python3检索G-mail

问题描述 投票:0回答:1

我找不到任何可用来检索Gmail的代码。

import poplib
from email import parser

SERVER = "pop.gmail.com"
USER  = "[email protected]"
PASSWORD = "password"

pop_conn = poplib.POP3_SSL(SERVER)
pop_conn.user(USER)
pop_conn.pass_(PASSWORD)
#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
    print(message['subject'])
    print(message['body'])

仅产生:

TypeError Traceback(最近一次通话)()中的12条消息= [对于位于范围(1,len(pop_conn.list()[1])+ 1)中的i,为pop_conn.retr(i)13#Concat消息片段:---> 14条消息= [“ \ n” .join(mssg [1])for mssg in messages]15#将消息解析为电子邮件对象:16条消息= [消息中mssg的parser.Parser()。parsestr(mssg)]

在(.0)中12条消息= [对于位于范围(1,len(pop_conn.list()[1])+ 1)中的i,为pop_conn.retr(i)13#Concat消息片段:---> 14条消息= [“ \ n” .join(mssg [1])for mssg in messages]15#将消息解析为电子邮件对象:16条消息= [消息中mssg的parser.Parser()。parsestr(mssg)]

TypeError:序列项0:预期的str实例,找到的字节

我花了几天的时间试图进行“简单的”电子邮件检索,而我发现的每段代码都是完全不起作用的。

任何人都可以实际获得带有主题的Gmail,以及其他内容吗?

python python-3.x gmail pop3 poplib
1个回答
0
投票

此代码不完整,但可能会对您有所帮助。

来源: https://www.code-learner.com/python-use-pop3-to-read-email-example/

import poplib
import smtplib, ssl

# input email address, password and pop3 server domain or ip address
email = '[email protected]'
password = 'password'

# connect to pop3 server:
server = poplib.POP3_SSL('pop.gmail.com')
# open debug switch to print debug information between client and pop3 server.
server.set_debuglevel(1)
# get pop3 server welcome message.
pop3_server_welcome_msg = server.getwelcome().decode('utf-8')
# print out the pop3 server welcome message.
print(server.getwelcome().decode('utf-8'))

# user account authentication
server.user(email)
server.pass_(password)

# stat() function return email count and occupied disk size
print('Messages: %s. Size: %s' % server.stat())
# list() function return all email list
resp, mails, octets = server.list()
print(mails)

# retrieve the newest email index number
index = len(mails)
# server.retr function can get the contents of the email with index variable value index number.
resp, lines, octets = server.retr(index)

# lines stores each line of the original text of the message
# so that you can get the original text of the entire message use the join function and lines variable. 
msg_content = b'\r\n'.join(lines).decode('utf-8')
# now parse out the email object.

from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr

import poplib

# parse the email content to a message object.
msg = Parser().parsestr(msg_content)

# get email from, to, subject attribute value.
email_from = msg.get('From')
email_to = msg.get('To')
email_subject = msg.get('Subject')
print('From ' + email_from)
print('To ' + email_to)
print('Subject ' + email_subject)

# delete the email from pop3 server directly by email index.
# server.dele(index)
# close pop3 server connection.
server.quit()
© www.soinside.com 2019 - 2024. All rights reserved.