-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleteContactsFromHotmailOutlook.py
152 lines (115 loc) · 5.4 KB
/
deleteContactsFromHotmailOutlook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
Install exchangelib:
pip install exchangelib
You can use following servers to connect to MS Exchange Server of
hotmail.com/outlook.com/live.com as of September 1 2023:
m.outlook.com
outlook.office365.com
eas.outlook.com
s.outlook.com
Set your hotmail.com/outlook.com credentials in
ACCOUNT_USERNAME_AND_EMAIL
and
ACCOUNT_PASSWORD
If you use 2FA, generate application password and set its
value to ACCOUNT_PASSWORD
"""
from exchangelib import (
Credentials,
Configuration,
OAUTH2,
BASIC,
Account,
DELEGATE,
IMPERSONATION,
OAuth2AuthorizationCodeCredentials,
)
from exchangelib.indexed_properties import EmailAddress
SERVER_URL = "m.hotmail.com"
ACCOUNT_USERNAME_AND_EMAIL = "[email protected]"
ACCOUNT_PASSWORD = "my very strong password"
#config = Configuration(server="m.hotmail.com", max_connections=10)
def connectToExchangeServer(server, USERNAME_AND_EMAIL, password):
"""
Get Exchange account cconnection with server
"""
creds = Credentials(username=USERNAME_AND_EMAIL, password=password)
config = Configuration(server=SERVER_URL, credentials=creds, auth_type = BASIC)
return Account(primary_smtp_address=USERNAME_AND_EMAIL, autodiscover=False, config=config, access_type=DELEGATE)
def printNumberOfMessagesInAllFolders(account):
print("*** printNumberOfMessagesInAllFolders started ***")
all_folders = account.root.glob('**/*')
for folder in all_folders:
if (not str(folder).startswith("Application")) and (not str(folder).startswith("Folder")):
print(str(folder) + " " + str(folder.all().count()))
print("*** printNumberOfMessagesInAllFolders ended ***\n")
def printNumberOfMessagesInSupportedFolders(account):
print("*** printNumberOfMessagesInSupportedFolders started ***\n")
all_folders = account.root.glob('**/*')
for folder in all_folders:
if (str(folder).startswith("AllContacts")) or (str(folder).startswith("RecoverableItemsDeletions") or (str(folder).startswith("Recipient Cache"))):
print(str(folder) + " " + str(folder.all().count()))
print("*** printNumberOfMessagesInSupportedFolders ended ***\n")
def printNumberOfMessagesInContactsFolders(account):
print("*** printNumberOfMessagesInContactsFolders started ***\n")
all_folders = account.contacts.glob('**/*')
for folder in all_folders:
if (not str(folder).startswith("Application")):
print(str(folder) + " " + str(folder.all().count()))
print("*** printNumberOfMessagesInContactsFolders ended ***\n")
def deleteItemsFromFolder(all_folders, visibleName, logName):
for folder in all_folders:
if (str(folder).startswith(visibleName)):
contacts = folder.all()
print("contact folder: " + str(folder) + str (contacts.count()))
print("Deletion " + logName + " started")
count = 0
for contact in contacts:
print(contact)
print ('\n')
contact.delete()
print (logName + ' item deleted\n')
count = count + 1
print("Deletion " + logName + " ended, deleted " + str(count) + " contacts")
def deleteMainContactsFolder(account):
contacts = account.contacts
allContacts = contacts.all()
print("number of contact: " + str(allContacts.count()))
count = 0
print("Deletion AllContacts (main folder) started")
for contact in allContacts:
print(contact)
print ('\n')
contact.delete()
print ('AllContacts (main folder) item deleted\n')
count = count +1
print("Deletion AllContacts (main folder) ended, deleted " + str(count) + " contacts")
def main():
account = connectToExchangeServer(SERVER_URL, ACCOUNT_USERNAME_AND_EMAIL, ACCOUNT_PASSWORD)
print (account)
printNumberOfMessagesInAllFolders(account)
folder = account.root / "AllContacts"
print("number of people on account: " + str(folder.people().count()))
print("*******************************\n")
"""
This section purges your contact folder. You may or may not recover you address book, backup it before you run this program.
"""
all_folders = account.root.glob('**/*')
deleteItemsFromFolder(all_folders, "AllContacts (AllContacts)", "AllContactsAsFolder")
print("*******************************\n")
"""
# Uncomment this section if you want to delete all purged contacts completely. Remember: It may delete also your Deleted folder and you will
# not be able to recover deleted messages
all_folders = account.root.glob('**/*') # Do it again because folder structure after previous deletions could change
deleteItemsFromFolder(all_folders, "RecoverableItemsDeletions", "RecoverableItemsDeletions")
"""
"""
The following section deletes collected email addresses that collected from incoming mail.
You can comment it if you do not want to purge these addresses
"""
all_folders = account.contacts.glob('**/*') # Do it again because folder structure after previous deletions could change
deleteItemsFromFolder(all_folders, "Recipient Cache", "Recipient Cache")
printNumberOfMessagesInContactsFolders(account)
printNumberOfMessagesInSupportedFolders(account)
if __name__ == "__main__":
main()