Python 3.5 imaplib emails

2019-09-16 12:59发布

I found some code in internet regarding imaplib and configuring with my information I got it work without problem with my gmail account (but I had to change some gmail configuration to do it).

I try the same code with my azure email service but I get every time the same error:

> 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

The problem is that I can't understand why so I don't neither know in what concentrate to solve my problem...

Could you please help me?

This is my code:

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()

NEW INFORMATION:

Guys I didn't receive so much help so far so I was readying documentation and try by myself, I try with IP address of the machine and with IMAP specifying the port and I got a different error this time..... This is the code that I edited:

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)

This time the error is this one:

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

Some help please!!?!?!?

1条回答
仙女界的扛把子
2楼-- · 2019-09-16 13:35

This is a DNS error. It can't find the name of the host you are attempting to connect to.

查看更多
登录 后发表回答