从imap或pop3服务器读取电子邮件

时间:2017-04-26 10:11:15

标签: python python-2.7 imap pop3 imaplib

我的任务是使用imap或pop3服务器地址阅读电子邮件。我正在尝试使用python完成此任务。可执行此任务的所有示例代码都会引发下面提到的错误

“错误:[Errno 10060]连接尝试失败,因为连接方在一段时间后未正确响应,或建立的连接失败,因为已连接的主机未能响应”

问题可能是什么,是我的本地防火墙设置还是由于某些其他原因...... ????

import imaplib

imap_host = 'imap.gmail.com'
imap_user = '****@gmail.com'
imap_pass = '****'

## open a connection 
imap = imaplib.IMAP4_SSL(imap_host)

## login
imap.login(imap_user, imap_pass) 

import sys
import chilkat

imap = chilkat.CkImap()

#  Anything unlocks the component and begins a fully-functional 30-day trial.
success = imap.UnlockComponent("Anything for 30-day trial")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

#  Turn on session logging:
imap.put_KeepSessionLog(True)

#  Connect to GMail
#  Use TLS
imap.put_Ssl(True)
imap.put_Port(993)
success = imap.Connect("imap.gmail.com")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

#  Login
#  Your login is typically your GMail email address.
success = imap.Login("***@gmail.com","*****")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

#  Select an IMAP mailbox
success = imap.SelectMailbox("Inbox")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

#  Show the session log.
print(imap.sessionLog())

#  Disconnect from the IMAP server.
success = imap.Disconnect()

1 个答案:

答案 0 :(得分:1)

首先,我建议进行简单的网络健全性测试,例如:使用Telnet进行如下操作:

telnet imap.gmail.com 993

如果网络很满意,您应该看到如下响应:

Trying 64.233.188.109...
Connected to gmail-imap.l.google.com.
Escape character is '^]'.

也可能值得查看IMAP的gmail帮助页面

https://support.google.com/mail/accounts/answer/7875