How to use the exchangelib.Account function in exchangelib

To help you get started, we’ve selected a few exchangelib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ecederstrand / exchangelib / tests / test_account.py View on Github external
Account(primary_smtp_address='blah')
        self.assertEqual(str(e.exception), "primary_smtp_address 'blah' is not an email address")
        with self.assertRaises(AttributeError) as e:
            # Non-autodiscover requires a config
            Account(primary_smtp_address='blah@example.com', autodiscover=False)
        self.assertEqual(str(e.exception), 'non-autodiscover requires a config')
        with self.assertRaises(ValueError) as e:
            # access type must be one of ACCESS_TYPES
            Account(primary_smtp_address='blah@example.com', access_type=123)
        if PY2:
            self.assertEqual(str(e.exception), "'access_type' 123 must be one of (u'impersonation', u'delegate')")
        else:
            self.assertEqual(str(e.exception), "'access_type' 123 must be one of ('impersonation', 'delegate')")
        with self.assertRaises(ValueError) as e:
            # locale must be a string
            Account(primary_smtp_address='blah@example.com', locale=123)
        self.assertEqual(str(e.exception), "Expected 'locale' to be a string, got 123")
        with self.assertRaises(ValueError) as e:
            # default timezone must be an EWSTimeZone
            Account(primary_smtp_address='blah@example.com', default_timezone=123)
        self.assertEqual(str(e.exception), "Expected 'default_timezone' to be an EWSTimeZone, got 123")
        with self.assertRaises(ValueError) as e:
            # config must be a Configuration
            Account(primary_smtp_address='blah@example.com', config=123)
        self.assertEqual(str(e.exception), "Expected 'config' to be a Configuration, got 123")
github ecederstrand / exchangelib / tests / test_autodiscover.py View on Github external
def test_autodiscover_from_account(self):
        from exchangelib.autodiscover import _autodiscover_cache
        _autodiscover_cache.clear()
        account = Account(primary_smtp_address=self.account.primary_smtp_address,
                          credentials=self.account.protocol.credentials,
                          autodiscover=True, locale='da_DK')
        self.assertEqual(account.primary_smtp_address, self.account.primary_smtp_address)
        self.assertEqual(account.protocol.service_endpoint.lower(), self.account.protocol.service_endpoint.lower())
        self.assertEqual(account.protocol.version.build, self.account.protocol.version.build)
        # Make sure cache is full
        self.assertTrue((account.domain, self.account.protocol.credentials, True) in _autodiscover_cache)
        # Test that autodiscover works with a full cache
        account = Account(primary_smtp_address=self.account.primary_smtp_address,
                          credentials=self.account.protocol.credentials,
                          autodiscover=True, locale='da_DK')
        self.assertEqual(account.primary_smtp_address, self.account.primary_smtp_address)
        # Test cache manipulation
        key = (account.domain, self.account.protocol.credentials, True)
        self.assertTrue(key in _autodiscover_cache)
        del _autodiscover_cache[key]
        self.assertFalse(key in _autodiscover_cache)
        del _autodiscover_cache
github strawp / random-scripts / sendmails.py View on Github external
def send_ews( self, email ):
    from exchangelib import Account, Message, Mailbox, FileAttachment, HTMLBody, Credentials, Configuration, NTLM, DELEGATE
    creds = Credentials(self.username,self.password)
    config = Configuration(
      service_endpoint=self.ews, 
      credentials=creds, 
      auth_type=NTLM
    )
    account = Account(
        primary_smtp_address=self.username,
        config=config, 
        autodiscover=False,
        access_type=DELEGATE,
    )
    m = email.get_ewsmessage( account )
    m.send()
    return False
github demisto / content / Integrations / EWSv2 / EWSv2.py View on Github external
context_dict = demisto.getIntegrationContext()

    if context_dict:
        try:
            config_args = construct_config_args(context_dict, credentials)
            account = Account(
                primary_smtp_address=account_email, autodiscover=False, config=Configuration(**config_args),
                access_type=access_type,
            )
            account.root.effective_rights.read  # pylint: disable=E1101
            return account
        except Exception, original_exc:
            pass

    try:
        account = Account(
            primary_smtp_address=ACCOUNT_EMAIL, autodiscover=True, credentials=credentials, access_type=access_type,
        )
    except AutoDiscoverFailed:
        return_error("Auto discovery failed. Check credentials or configure manually")

    autodiscover_result = create_context_dict(account)
    if autodiscover_result == context_dict:
        raise original_exc  # pylint: disable=E0702

    if account_email == ACCOUNT_EMAIL:
        demisto.setIntegrationContext(create_context_dict(account))
    return account
github StackStorm-Exchange / stackstorm-msexchange / sensors / item_sensor.py View on Github external
def setup(self):
        if self.autodiscover:
            self.account = Account(
                primary_smtp_address=self.primary_smtp_address,
                credentials=self._credentials,
                autodiscover=True,
                access_type=DELEGATE)
        else:
            ms_config = Configuration(
                server=self.server,
                credentials=self._credentials)
            self.account = Account(
                primary_smtp_address=self.primary_smtp_address,
                config=ms_config,
                autodiscover=False,
                access_type=DELEGATE)
github ibmresilient / resilient-community-apps / fn_exchange / fn_exchange / util / exchange_utils.py View on Github external
def connect_to_account(self, primary_smtp_address, impersonation=False):
        """Connect to specified account and return it"""

        # Don't check certificates
        if not self.verify_cert:
            BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter

        # Decide whether or not to use impersonation access
        access_type = IMPERSONATION if impersonation else DELEGATE

        # Use credentials to get account
        try:
            credentials = Credentials(username=self.username, password=self.password)
            config = Configuration(server=self.server, credentials=credentials)
            account = Account(primary_smtp_address=primary_smtp_address, config=config,
                              autodiscover=self.verify_cert, access_type=access_type)
        except ErrorNonExistentMailbox:
            raise NoMailboxError(primary_smtp_address)
        except ConnectionError:
            raise ServerConnectionError(self.server)
        except UnauthorizedError:
            raise CredentialsError()
        except ErrorImpersonateUserDenied:
            raise ImpersonationError(self.username, primary_smtp_address)
        return account
github demisto / content / Integrations / EWSMailSender / EWSMailSender.py View on Github external
def get_account(account_email):
    return Account(
        primary_smtp_address=account_email, autodiscover=False, config=config, access_type=ACCESS_TYPE,
    )
github StackStorm-Exchange / stackstorm-msexchange / actions / base / action.py View on Github external
config=config,
                autodiscover=False,
                access_type=DELEGATE
            )
        else:
            if autodiscover:
                self.account = Account(
                    primary_smtp_address=config['primary_smtp_address'],
                    credentials=self._credentials,
                    autodiscover=autodiscover,
                    access_type=DELEGATE)
            else:
                ms_config = Configuration(
                    server=server,
                    credentials=self._credentials)
                self.account = Account(
                    primary_smtp_address=config['primary_smtp_address'],
                    config=ms_config,
                    autodiscover=False,
                    access_type=DELEGATE)
            self._store_cache_configuration()