swimlane / pyews

A Python package to interact with the both on-premises and Office 365 Exchange Web Services

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pyews do not work!!

imad777550 opened this issue · comments

WARNING:pyews.core:Unable to parse response from GetUserSettings
INFO:pyews.core:Access is denied. Check credentials and try again., The process failed to get the correct properties.

@imad777550 it looks like you do not have the correct permissions or credentials.

Reference Code:

from pyews import UserConfiguration
from pyews import GetSearchableMailboxes
from pyews import SearchMailboxes
from pyews import DeleteItem
from pyews import GetInboxRules
import urllib3
from requests.auth import HTTPBasicAuth
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

userconfig = UserConfiguration('mail','pass')

# get searchable mailboxes based on your accounts permissions
referenceid_list = []
for mailbox in GetSearchableMailboxes(userconfig).run():
      referenceid_list.append(mailbox['reference_id'])


# let's search all the referenceid_list items
messages_found = []
for search in SearchMailboxes(userconfig).run('subject:Problem: Unavailable by ICMP ping', referenceid_list):
      messages_found.append(search['id'])
      # we can print the results first if we want
      print(search['subject'])
      print(search['id'])
      print(search['sender'])
      print(search['to_recipients'])
      print(search['created_time'])
      print(search['received_time'])

I was able to get_searchable_mailboxes with exchangelib. I opened Role Based access control permission. I don't know what permission should I add in order to do it with pyews. In fact, I am using pyews because it allows me to perform actions on specific mailboxes, and getting forwarding rules. This is not present in exchangelib.

Thank You.

@imad777550 which version of py-ews are you using? It looks to be py-ews-dev and version 3.0.0 (#20)

Is this correct?

In the mean time I will take a look as well.

I downloaded it with "pip3 install py-ews" few days ago. It should be the latest version.
https://pypi.org/project/py-ews/

@imad777550 okay, my apologies - I'm looking into it now

@MSAdministrator No worries. Thanks for your support.

@imad777550 Can you replace the line where you have userconfig = UserConfig('mail','pass') with these addtional parameters:

userconfig = UserConfiguration('mail', 'pass', autodiscover=False, ews_url='https://outlook.office365.com/EWS/Exchange.asmx')

Let me know if this helps.

image

from pyews import UserConfiguration
from pyews import GetSearchableMailboxes
from pyews import SearchMailboxes
from pyews import DeleteItem
from pyews import GetInboxRules
import urllib3
from requests.auth import HTTPBasicAuth
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

userconfig = UserConfiguration('xxx',xxxx')',autodiscover=False, ews_url='https://outlook.office365.com/EWS/Exchange.asmx')

# get searchable mailboxes based on your accounts permissions
referenceid_list = []
for mailbox in GetSearchableMailboxes(userconfig).run():
      referenceid_list.append(mailbox['reference_id'])
print(referenceid_list)


# let's search all the referenceid_list items
messages_found = []
for search in SearchMailboxes(userconfig).run('subject:Join our experts for a live demo.', referenceid_list):
      messages_found.append(search['id'])
      # we can print the results first if we want
      print(search['subject'])
      print(search['id'])
      print(search['sender'])
      print(search['to_recipients'])
      print(search['created_time'])
      print(search['received_time'])


deleted_message_response = DeleteItem(userconfig).run(messages_found)
print(deleted_message_response)

I was able to "SearchMailboxes" without any problem. But when it domes to "Delete message" it gived me the error that I sent below. Is there any extra permission that I should enable than RBAC on the server?

@imad777550 sorry this is a bug (but thanks for finding it). I have a new version coming out soon that will resolve this and many other stability issues (the link i provided above).

Anyways, please add this line anywhere after the creation of userconfig variable and before the DeleteItem call:

setattr(userconfig, 'exchangeVersion', 'Exchange2015')

(basically the variable name for exchange_version didn't get changed from exchangeVersion)

I added this line. (setattr(userconfig, 'exchangeVersion', 'Exchange2015'))
image

Also, "GetInboxRules" and "FindHiddenInboxRules" I found the same problem.

Anyway, is this the right method to use GetInboxRules? I didn't found how to use it in pyews docs.

#inbox_rules = GetInboxRules(userconfig).run(messages_found)
#print(inbox_rules)

Thank you for your help.

So @imad777550 regarding the first error - can you check and see if the item you deleted actually deleted from the mailbox ? (I believe it worked but ews didn’t respond with any data).

As far as the inbox rules you can see the documentation for that here: https://py-ews.readthedocs.io/en/latest/services/getinboxrules.html

I'm sorry to disturb you again but I checked my inbox and the mail is not deleted.
Also I used GetInboxRules from the documentation that you sent and it gave me also an error.

image

Is this will be also fixed in the next release? or there is extra permission that I should configure on my server?
Thank you.

Sorry for all the trouble - @imad777550 - do you mind testing something for me?

Can you install this new version by running: pip3 install py-ews-dev==3.0.0

Once installed run this:

from pyews import EWS

ews = EWS(
    'youremail@company.com',
    'yourpassword', multi_threading=False
)

print(ews.search_and_delete_message('some search string'))

I know this is a lot to ask but curious if this will resolve the issue. Thanks,

Don't worry for your questions I'm grateful that you're helping me. Thank you.
Anyway, I tried the script above.

With multi_threading parameter:
image

Without multi_threading paramter:
image

@imad777550 sorry for the delay, had to make some changes and didn't have time until now.

Can you please do the following

pip install py-ews-dev==3.0.1

Then attempt to do the instructions above again? Thanks and sorry for all the troubles.

@MSAdministrator Sorry for the delay.

`from pyews import UserConfiguration
from pyews import GetSearchableMailboxes
from pyews import SearchMailboxes
from pyews import DeleteItem
from pyews import GetInboxRules

import urllib3
from requests.auth import HTTPBasicAuth
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

userconfig = UserConfiguration('xxx','xxx',autodiscover=False, ews_url='https://outlook.office365.com/EWS/Exchange.asmx')
setattr(userconfig, 'exchangeVersion', 'Exchange2015')

get searchable mailboxes based on your accounts permissions

referenceid_list = []
for mailbox in GetSearchableMailboxes(userconfig).run():
referenceid_list.append(mailbox['reference_id'])
print(referenceid_list)

let's search all the referenceid_list items

messages_found = []
for search in SearchMailboxes(userconfig).run('subject:test-imad', referenceid_list):
messages_found.append(search['id'])
# we can print the results first if we want
print(search['subject'])
print(search['id'])
print(search['sender'])
print(search['to_recipients'])
print(search['created_time'])
print(search['received_time'])

deleted_message_response = DeleteItem(userconfig).run(messages_found)
print(deleted_message_response)
`

image

It won't work with "pip3 install py-ews"?
I am infosec consultant. I'm using this to solve some use cases for compliance. That's why I should perform "searchmailboxes" function and than choose specific mailboxes to delete the mail.
I should also perform "FindHiddenInboxRules" at later stages.

Thank you for your support.

@imad777550 sorry for the delay - I’ll look at this closer this evening

@imad777550 I have a new development verson of pyews that I would like you to try.

pip install py-ews-dev==3.1.0

Once it is installed then create a new Python script file.

Inside that file paste this:

from pyews import EWS

ews = EWS(
    'email@company.com,
    'some_password'
)

for item in ews.search_and_delete_message('phish', what_if=True):
    print(item)

You should see logs on your console and that is expected. Please let me know how this works for you.

Hello @MSAdministrator,

I installed pyews with "pip3 install py-ews-dev==3.1.0". I replaced phish" with "test test" and with "[Test-Pyews-Delete ]". The same error is generated.

image

image

Best Regards,
Imad.