예제 #1
0
    def _connect(self):
        conn = None

        try:
            if self.ssl:
                if self.port == '':
                    conn = imaplib.IMAP4_SSL(self.server)
                else:
                    conn = imaplib.IMAP4_SSL(self.server, int(self.port))
            else:
                if self.port == '':
                    conn = imaplib.IMAP4(self.server)
                else:
                    conn = imaplib.IMAP4(self.server, int(self.port))

                if 'STARTTLS' in conn.capabilities:
                    conn.starttls()
                else:
                    logging.warning(
                        "Using unencrypted connection for account '%s'" %
                        self.name)

            if self.oauth2string != '':
                conn.authenticate('XOAUTH2', lambda x: self.oauth2string)
            elif 'AUTH=CRAM-MD5' in conn.capabilities:
                # use CRAM-MD5 auth if available
                conn.login_cram_md5(self.user, self.password)
            else:
                conn.login(self.user, self.password)
        except:
            try:
                if conn != None:
                    # conn.close() # allowed in SELECTED state only
                    conn.logout()
            except:
                pass
            raise  # re-throw exception

        # notify_next_change() (IMAP IDLE) requires a selected folder
        if conn.state == AUTH:
            self._select_single_folder(conn)

        return conn
예제 #2
0
파일: accounts.py 프로젝트: wodole/mailnag
	def _get_IMAP_connection(self, use_existing):
		# try to reuse existing connection
		if use_existing and self._has_IMAP_connection():
			return self._conn
		
		self._conn = conn = None
		
		try:
			if self.ssl:
				if self.port == '':
					conn = imaplib.IMAP4_SSL(self.server)
				else:
					conn = imaplib.IMAP4_SSL(self.server, int(self.port))
			else:
				if self.port == '':
					conn = imaplib.IMAP4(self.server)
				else:
					conn = imaplib.IMAP4(self.server, int(self.port))
				
				if 'STARTTLS' in conn.capabilities:
					conn.starttls()
				else:
					logging.warning("Using unencrypted connection for account '%s'" % self.name)
				
			if self.oauth2string != '':
				conn.authenticate('XOAUTH2', lambda x: self.oauth2string)
			else:
				conn.login(self.user, self.password)
			
			self._conn = conn
		except:
			try:
				if conn != None:
					# conn.close() # allowed in SELECTED state only
					conn.logout()
			except:	pass			
			raise # re-throw exception
		
		return self._conn