0

I have already been through these set of solutions for connecting IMAP under proxy

Information to add: I am trying to write a python code that can fetch mails from gmails IMAP server using imapclient under http,https and socks proxy server of my academic insitute When tried without any proxy handling, it used to give error

socket.error [101] network is unreachable

import imapclient
import pyzmail

imapObj = imapclient.IMAPClient('imap.gmail.com',ssl=True)
imapObj.login('***********@gmail.com','*********')
imapObj.select_folder('INBOX', readonly=True)

UIDs = imapObj.search(['SINCE 07-Jul-2016'])

for item in UIDs:
    rawMessages = imapObj.fetch(item, ['BODY[]', 'FLAGS'])
    message = pyzmail.PyzMessage.factory(rawMessages[item]['BODY[]'])
    message.get_subject()
    message.get_addresses('from')
    message.get_addresses('to')
    message.get_addresses('cc')
    message.get_addresses('bcc')
    message.text_part != None
    message.text_part.get_payload().decode(message.text_part.charset)
    message.html_part != None
    message.html_part.get_payload().decode(message.html_part.charset)
imapObj.logout()

However, the process gives error as

File "mailtotext.py", line 16, in <module>
imapObj = imapclient.IMAPClient('imap.gmail.com',ssl=True)
  File "/usr/local/lib/python2.7/dist-packages/imapclient/imapclient.py", line 152, in __init__
    self._imap = self._create_IMAP4()
  File "/usr/local/lib/python2.7/dist-packages/imapclient/imapclient.py",line 164, in _create_IMAP4
self._timeout)
  File "/usr/local/lib/python2.7/dist-packages/imapclient/tls.py", line 153, in __init__
    imaplib.IMAP4.__init__(self, host, port)
  File "/usr/lib/python2.7/imaplib.py", line 172, in __init__
self.open(host, port)
  File "/usr/local/lib/python2.7/dist-packages/imapclient/tls.py", line 158, in open
    sock = socket.create_connection((host, port), self._timeout)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 101] Network is unreachable

I then followed the above mentioned link procedures owing to my institute proxy as http,https,socks I have already set my system proxy settings as

http_proxy="http://10.3.100.207:8080/"
https_proxy="https://10.3.100.207:8080/"
ftp_proxy="ftp://10.3.100.207:8080/"
socks_proxy="socks://10.3.100.207:8080/"

and edited the code as import imapclient import pyzmail import socks import socket

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4,"10.3.100.207",8080,True)
socket.socket = socks.socksocket

imapObj = imapclient.IMAPClient('imap.gmail.com',ssl=True)
imapObj.login('***********@gmail.com','*********')
imapObj.select_folder('INBOX', readonly=True)

UIDs = imapObj.search(['SINCE 07-Jul-2016'])

for item in UIDs:
    rawMessages = imapObj.fetch(item, ['BODY[]', 'FLAGS'])
    message = pyzmail.PyzMessage.factory(rawMessages[item]['BODY[]'])
    message.get_subject()
    message.get_addresses('from')
    message.get_addresses('to')
    message.get_addresses('cc')
    message.get_addresses('bcc')
    message.text_part != None
    message.text_part.get_payload().decode(message.text_part.charset)
    message.html_part != None
    message.html_part.get_payload().decode(message.html_part.charset)
imapObj.logout()

But this process seems to freeze for very long time and finally I always need to make a keyboard interrupt. Its sure that it was freezed somewhere in socket files as per keyboard interrupt output.

Please help me through this, there are hardly solution to such problems over web. I have even tried tunneling but it isn't solving my problem or say making it worse (I might have not implemented it well :P)I would provide any other information and output if needed here

Community
  • 1
  • 1
Shubh Agrawal
  • 11
  • 1
  • 4
  • Are you sure that the account allows for IMAP? In a standard Gmail you have to enable IMAP before it works. The preferred method is to use Google's REST API to retrieve emails from software apps. I am almost sure that is not the problem but sometimes it is good to state the obvious. – Falk Schuetzenmeister Jul 07 '16 at 22:40
  • yeah, I have already enabled IMAP on my account and completely sure about it. I know using Google REST API but I particularly want to work with IMAP. – Shubh Agrawal Jul 09 '16 at 21:08
  • The proxy cannot possibly support four different proxy architectures on the same IP address and port number,. If the proxy isn't a SOCKS proxy, that would explain the error. Similarly, if you have the wrong port number, you are never actually connecting to the proxy successfully, and eventually the connection attempt times out. – tripleee May 24 '19 at 10:24

0 Answers0