适合Python iMAP电子邮件访问的格式化示例? [英] Properly formatted example for Python iMAP email access?
问题描述
从
https://docs.python.org/2.4/lib/imap4-example.html
import getpass,imaplib
M = imaplib.IMAP4()
M.login(getpass.getuser(),getpass.getpass())
M.select()
typ,data = M.search(None,'ALL')
for num data [0] .split():
typ,data = M.fetch(num,'(RFC822)')
print'Message%s\\\
%s\\\
'% ,data [0] [1])$ b $ b M.close()
M.logout()
假设我的电子邮件是email@gmail.com,密码是密码,应该怎么样?我试过 M.login(getpass.getuser(email@gmail.com),getpass.getpass(password))
,它超时。完成newb这里,所以很有可能我错过了一些明显的事情(比如先创建一个iMAP对象?不确定)。
import imaplib
#你想连接到服务器;指定哪个服务器
server = imaplib.IMAP4_SSL('imap.googlemail.com')
#连接后,告诉服务器你是谁
server.login('email@gmail.com' ,'password')
#这将显示一个可用的文件夹列表
#可能您的收件箱被称为INBOX,但检查邮箱列表
code,mailboxen = server.list()
print mailboxen
#如果它被称为INBOX,那么...
server.select(INBOX)
您的其余代码似乎是正确的。
tldr: Can someone show me how to properly format this Python iMAP example so it works?
from https://docs.python.org/2.4/lib/imap4-example.html
import getpass, imaplib M = imaplib.IMAP4() M.login(getpass.getuser(), getpass.getpass()) M.select() typ, data = M.search(None, 'ALL') for num in data[0].split(): typ, data = M.fetch(num, '(RFC822)') print 'Message %s\n%s\n' % (num, data[0][1]) M.close() M.logout()
Assuming my email is "email@gmail.com" and the password is "password," how should this look? I tried M.login(getpass.getuser(email@gmail.com), getpass.getpass(password))
and it timed out. Complete newb here, so it's very likely I missed something obvious (like creating an iMAP object first? Not sure).
import imaplib
# you want to connect to a server; specify which server
server= imaplib.IMAP4_SSL('imap.googlemail.com')
# after connecting, tell the server who you are
server.login('email@gmail.com', 'password')
# this will show you a list of available folders
# possibly your Inbox is called INBOX, but check the list of mailboxes
code, mailboxen= server.list()
print mailboxen
# if it's called INBOX, then…
server.select("INBOX")
The rest of your code seems correct.
这篇关于适合Python iMAP电子邮件访问的格式化示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!