简体   繁体   中英

using imaplib to access corporate gmail

I have the following code, it works for my own personal email.

import getpass, imaplib
import email
import libgmail
import re

#M=imaplib.IMAP4()
M=imaplib.IMAP4_SSL('imap.gmail.com',993)
M.login('loginname@company.com','password')
M.select('bur-dev')

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

With my company mail though (I do have imap turned on) i get a connection time out-

    C:\Python24\libgmail-0.1.11>python gmail_app.py
Traceback (most recent call last):
  File "gmail_app.py", line 7, in <module>
    M=imaplib.IMAP4_SSL('imap.gmail.com',993)
  File "/usr/lib/python2.6/imaplib.py", line 1138, in __init__
    IMAP4.__init__(self, host, port)
  File "/usr/lib/python2.6/imaplib.py", line 163, in __init__
    self.open(host, port)
  File "/usr/lib/python2.6/imaplib.py", line 1149, in open
    self.sock = socket.create_connection((host, port))
  File "/usr/lib/python2.6/socket.py", line 514, in create_connection
    raise error, msg
socket.error: [Errno 116] Connection timed out

Any ideas on how to debug this? The port is not firewalled because it is working for my personal email.

You seem to have a firewall issue.
Your company doesn't allow a connection to imap.gmail.com on port 993.
As suggested in one of the comment try from the commandline:
telnet imap.gmail.com 993

if it times out or goes into error then you are not allow to access that port from your machine.

Connection is getting blocked. Following could be the probable reason:

  1. proxy is used for internet access. Python tries to connect directly.

  2. Firewall or network security appliance blocking connection

  3. Local fireall of system or AV blocking connection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM