使用Python和poplib获取电子邮件 [英] Get emails with Python and poplib
问题描述
我想使用Python登录我的帐户,并让python打印我在邮箱中收到的邮件。我知道如何连接
I would like to log into my account with Python and get python to print the messages I received in my mailbox. I know how to connect
import getpass, poplib
user = 'my_user_name'
Mailbox = poplib.POP3_SSL('pop.googlemail.com', '995')
Mailbox.user(user)
Mailbox.pass_('my_password')
我不知道如何让Python显示我的消息。我尝试了poplib文档中的所有功能。它们只显示数字。
I don't know how to get Python to display my messages. I tried all the functions in the poplib doc. They only display numbers.
推荐答案
您尚未发布源代码,但这里是我的回复:
You have not posted your source code, but here is my response:
如何获取消息总数:
(numMsgs, totalSize) = self.conn_pop3.stat()
如何获取特定邮件,知道邮箱中的号码:
(server_msg, body, octets) = self.conn_pop3.retr(number)
所以您可能需要的功能是 retr ,它返回一个元组。
请参阅
此处。
So the function you might need is retr, it returns a tuple. See here.
小心它还将相应的电子邮件设置为服务器上的SEEN!
你可以撤消这个,至少用IMAP你可以。
Careful it also sets the respective email as SEEN on the server! You can probably undo that, at least with IMAP you can.
我的一个pop3 lib电子邮件的实现读取:
And my implementation of a pop3 lib email read:
from poplib import POP3
...
if self.pop3_connected:
try:
#------Check if email number is valid----------------------
(numMsgs, totalSize) = self.conn_pop3.stat()
self.debug(200, "Total number of server messages: ", numMsgs)
self.debug(200, "Total size of server messages: ", totalSize)
if number>numMsgs:
self.debug(200, "\nSorry - there aren't that many messages in your inbox\n")
return False
else:
(server_msg, body, octets) = self.conn_pop3.retr(number)
self.debug(200, "Server Message: " , server_msg)
self.debug(200, "Number of Octets: " , octets)
self.debug(200, "Message body:")
for line in body:
print line
#end for
return True
#endif
finally:
self.__disconnect__()
#endif
另外这里是POP3连接,至少我如何实现它...使用字符串比较有点棘手,但它适用于我的应用程序:
Also here is the POP3 connection, at least how I implemented it...sort of tricky using a string comparison, but it worked for my app:
def __connect_pop3__(self):
"""\brief Method for connecting to POP3 server
\return True If connection to POP3 succeeds or if POP3 is already connected
\return False If connection to POP3 fails
"""
#------Check that POP3 is not already connected-----------------------
if not self.pop3_connected:
#------Connect POP3-----------------------------------------------
self.debug(100, 'Connecting POP3 with: ', self.host_name, self.user_name, self.pass_name)
self.conn_pop3 = POP3(self.host_name)
res1 = self.conn_pop3.user(self.user_name)
string1 = str(res1)
self.debug(100, 'User identification result:', string1)
res2 = self.conn_pop3.pass_(self.pass_name)
string2 = str(res2)
self.debug(100, 'Pass identification result:', string2)
#------Check if connection resulted in success--------------------
#------Server on DavMail returns 'User successfully logged on'----
if string2.find('User successfully logged on')<>-1 or string1.find('User successfully logged on')<>-1 :
self.pop3_connected = True
return True
else:
return False
#endif
else:
self.debug(255, 'POP3 already connected')
return True
#endif
这篇关于使用Python和poplib获取电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!