Python 3.5 个 imaplib 电子邮件
Python 3.5 imaplib emails
我在互联网上找到了一些关于 imaplib 的代码,并使用我的信息进行了配置,我的 gmail 帐户可以正常使用它(但我必须更改一些 gmail 配置才能做到这一点)。
我尝试使用我的 Azure 电子邮件服务使用相同的代码,但每次都收到相同的错误:
> Traceback (most recent call last): File "C:\Users\Carlo\Desktop\try.py", line 66, in <module>
> M = imaplib.IMAP4_SSL('mail.example.com') File "C:\Python34-32\lib\imaplib.py", line 1221, in __init__
> IMAP4.__init__(self, host, port) File "C:\Python34-32\lib\imaplib.py", line 181, in __init__
> self.open(host, port) File "C:\Python34-32\lib\imaplib.py", line 1234, in open
> IMAP4.open(self, host, port) File "C:\Python34-32\lib\imaplib.py", line 257, in open
> self.sock = self._create_socket() File "C:\Python34-32\lib\imaplib.py", line 1224, in _create_socket
> sock = IMAP4._create_socket(self) File "C:\Python34-32\lib\imaplib.py", line 247, in _create_socket
> return socket.create_connection((self.host, self.port)) File "C:\Python34-32\lib\socket.py", line 494, in create_connection
> for res in getaddrinfo(host, port, 0, SOCK_STREAM): File "C:\Python34-32\lib\socket.py", line 533, in getaddrinfo
> for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11004] getaddrinfo failed
问题是我不明白为什么,所以我也不知道集中精力解决我的问题...
你能帮帮我吗?
这是我的代码:
import sys
import imaplib
import getpass
import email
import email.header
import datetime
def process_mailbox(M):
rv, data = M.search(None, '(UNSEEN)')#select just un-read documents
if rv != 'OK':
print("No messages found!")
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print("ERROR getting message", num)
return
msg = email.message_from_bytes(data[0][1])
print (msg)
hdr = email.header.make_header(email.header.decode_header(msg['Subject']))
subject = str(hdr)
print('Message %s: %s' % (num, subject))
print('Raw Date:', msg['Date'])
# Now convert to local date-time
date_tuple = email.utils.parsedate_tz(msg['Date'])
if date_tuple:
local_date = datetime.datetime.fromtimestamp(
email.utils.mktime_tz(date_tuple))
print ("Local Date:", \
local_date.strftime("%a, %d %b %Y %H:%M:%S"))
M = imaplib.IMAP4_SSL('IMAP')#imap of my azure service
try:
rv, data = M.login("email", "password")#my username and password except imaplib.IMAP4.error:
print ("LOGIN FAILED!!! ")
sys.exit(1)
rv, data = M.select("Inbox") #select data from inbox folder if rv == 'OK':
print("Processing mailbox...\n")
process_mailbox(M)
M.close() else:
print("ERROR: Unable to open mailbox ", rv)
M.logout()
新信息:
伙计们,到目前为止我没有得到太多帮助,所以我正在准备文档并自己尝试,我尝试使用机器的 IP 地址和 IMAP 指定端口,这次我遇到了不同的错误。 ...
这是我编辑的代码:
M = imaplib.IMAP4_SSL('IMAP or IP Address','143')
try:
rv, data = M.login(EMAIL_ACCOUNT, passwd)
except imaplib.IMAP4.error:
print ("LOGIN FAILED!!! ")
sys.exit(1)
这次错误是这个:
TimeoutError: [WinError 10060] A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to
respond
请帮忙!!?!?!?
这是一个 DNS 错误。它找不到您尝试连接的主机的名称。
我在互联网上找到了一些关于 imaplib 的代码,并使用我的信息进行了配置,我的 gmail 帐户可以正常使用它(但我必须更改一些 gmail 配置才能做到这一点)。
我尝试使用我的 Azure 电子邮件服务使用相同的代码,但每次都收到相同的错误:
> Traceback (most recent call last): File "C:\Users\Carlo\Desktop\try.py", line 66, in <module>
> M = imaplib.IMAP4_SSL('mail.example.com') File "C:\Python34-32\lib\imaplib.py", line 1221, in __init__
> IMAP4.__init__(self, host, port) File "C:\Python34-32\lib\imaplib.py", line 181, in __init__
> self.open(host, port) File "C:\Python34-32\lib\imaplib.py", line 1234, in open
> IMAP4.open(self, host, port) File "C:\Python34-32\lib\imaplib.py", line 257, in open
> self.sock = self._create_socket() File "C:\Python34-32\lib\imaplib.py", line 1224, in _create_socket
> sock = IMAP4._create_socket(self) File "C:\Python34-32\lib\imaplib.py", line 247, in _create_socket
> return socket.create_connection((self.host, self.port)) File "C:\Python34-32\lib\socket.py", line 494, in create_connection
> for res in getaddrinfo(host, port, 0, SOCK_STREAM): File "C:\Python34-32\lib\socket.py", line 533, in getaddrinfo
> for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11004] getaddrinfo failed
问题是我不明白为什么,所以我也不知道集中精力解决我的问题...
你能帮帮我吗?
这是我的代码:
import sys
import imaplib
import getpass
import email
import email.header
import datetime
def process_mailbox(M):
rv, data = M.search(None, '(UNSEEN)')#select just un-read documents
if rv != 'OK':
print("No messages found!")
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print("ERROR getting message", num)
return
msg = email.message_from_bytes(data[0][1])
print (msg)
hdr = email.header.make_header(email.header.decode_header(msg['Subject']))
subject = str(hdr)
print('Message %s: %s' % (num, subject))
print('Raw Date:', msg['Date'])
# Now convert to local date-time
date_tuple = email.utils.parsedate_tz(msg['Date'])
if date_tuple:
local_date = datetime.datetime.fromtimestamp(
email.utils.mktime_tz(date_tuple))
print ("Local Date:", \
local_date.strftime("%a, %d %b %Y %H:%M:%S"))
M = imaplib.IMAP4_SSL('IMAP')#imap of my azure service
try:
rv, data = M.login("email", "password")#my username and password except imaplib.IMAP4.error:
print ("LOGIN FAILED!!! ")
sys.exit(1)
rv, data = M.select("Inbox") #select data from inbox folder if rv == 'OK':
print("Processing mailbox...\n")
process_mailbox(M)
M.close() else:
print("ERROR: Unable to open mailbox ", rv)
M.logout()
新信息:
伙计们,到目前为止我没有得到太多帮助,所以我正在准备文档并自己尝试,我尝试使用机器的 IP 地址和 IMAP 指定端口,这次我遇到了不同的错误。 ... 这是我编辑的代码:
M = imaplib.IMAP4_SSL('IMAP or IP Address','143')
try:
rv, data = M.login(EMAIL_ACCOUNT, passwd)
except imaplib.IMAP4.error:
print ("LOGIN FAILED!!! ")
sys.exit(1)
这次错误是这个:
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
请帮忙!!?!?!?
这是一个 DNS 错误。它找不到您尝试连接的主机的名称。