gpt4 book ai didi

python - 尝试获取电子邮件 poplib 时出现错误的帮助

转载 作者:太空宇宙 更新时间:2023-11-03 19:27:12 49 4
gpt4 key购买 nike

这是我到目前为止尝试过的:

# view and delete e-mail using the POP3 protocol

import sys, getpass, poplib, re

# change according to your needs
POPHOST = "pop3.126.com"
POPUSER = "Username"
POPPASS = "pass"

# the number of message body lines to retrieve
MAXLINES = 10
HEADERS = "From To Subject".split()

# headers you're actually interested in
rx_headers = re.compile('|'.join(HEADERS), re.IGNORECASE)

try:
# connect to POP3 and identify user
pop = poplib.POP3(POPHOST)
pop.user(POPUSER)

if not POPPASS or POPPASS=='=':
# if no password was supplied, ask for it
POPPASS = getpass.getpass("Password for %s@%s:" % (POPUSER, POPHOST))

# authenticate user
pop.pass_(POPPASS)

# get general information (msg_count, box_size)
stat = pop.stat( )

# print some information
print "Logged in as %s@%s" % (POPUSER, POPHOST)
print "Status: %d message(s), %d bytes" % stat

bye = 0
count_del = 0
for n in range(stat[0]):

msgnum = n+1

# retrieve headers
response, lines, bytes = pop.top(msgnum, MAXLINES)

# print message info and headers you're interested in
print "Message %d (%d bytes)" % (msgnum, bytes)
print "-" * 30
print "\n".join(filter(rx_headers.match, lines))
print "-" * 30

# input loop
while 1:
k = raw_input("(d=delete, s=skip, v=view, q=quit) What?")
k = k[:1].lower( )
if k == 'd':
# Mark message for deletion
k = raw_input("Delete message %d? (y/n)" % msgnum)
if k in "yY":
pop.dele(msgnum)
print "Message %d marked for deletion" % msgnum
count_del += 1
break
elif k == 's':
print "Message %d left on server" % msgnum
break
elif k == 'v':
print "-" * 30
print "\n".join(lines)
print "-" * 30
elif k == 'q':
bye = 1
break

# done ...
if bye:
print "Bye"
break

# summary
print "Deleting %d message(s) in mailbox %s@%s" % (
count_del, POPUSER, POPHOST)

# close operations and disconnect from server
print "Closing POP3 session"
pop.quit( )

except poplib.error_proto, detail:

# possible error
print "POP3 Protocol Error:", detail

这给了我错误:

POP3 Protocol Error: -ERR Unable to log on

有谁知道问题出在哪里吗?我使用的是 Gmail 帐户,并且所有登录信息都是正确的。

编辑:

我稍微修改了代码,现在我得到了他的错误......这里是代码和错误:

import poplib
user = "username@gmail.com"
passs = "password"
pop = poplib.POP3("pop.gmail.com",995)
pop.user(user)
pop.pass_(passs)

我收到此错误:

  File "C:\Python27\lib\poplib.py", line 86, in __init__
self.welcome = self._getresp()
File "C:\Python27\lib\poplib.py", line 124, in _getresp
resp, o = self._getline()
File "C:\Python27\lib\poplib.py", line 108, in _getline
if not line: raise error_proto('-ERR EOF')
error_proto: -ERR EOF

最佳答案

我找到了有效的代码......这里是:

import poplib

M = poplib.POP3_SSL('pop3.live.com', 995) #Connect to hotmail pop3 server
M.set_debuglevel(2)
success = False;
user = "email@hotmail.com"

while success == False:
try:
password = raw_input("password: ")
M.user(user)
M.pass_(password)
except:
print "Invalid credentials"
else:
print "Successful login"
success = True

关于python - 尝试获取电子邮件 poplib 时出现错误的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7734837/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com