Created
March 31, 2015 13:09
-
-
Save nicklasb/a7986731ab15a6809f0b to your computer and use it in GitHub Desktop.
Ultra-ugly test of message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Nicklas Börjesson' | |
from lxml.builder import ElementMaker | |
import lxml.etree | |
from pyexchange import Exchange2010Service, ExchangeNTLMAuthConnection | |
URL = u'https://your_server/EWS/Exchange.asmx' | |
USERNAME = u'your_domain\\your_account' | |
PASSWORD = u"your_password" | |
# Set up the connection to Exchange | |
connection = ExchangeNTLMAuthConnection(url=URL, | |
username=USERNAME, | |
password=PASSWORD) | |
service = Exchange2010Service(connection) | |
MSG_NS = u'http://schemas.microsoft.com/exchange/services/2006/messages' | |
TYPE_NS = u'http://schemas.microsoft.com/exchange/services/2006/types' | |
SOAP_NS = u'http://schemas.xmlsoap.org/soap/envelope/' | |
NAMESPACES = {u'm': MSG_NS, u't': TYPE_NS, u's': SOAP_NS} | |
M = ElementMaker(namespace=MSG_NS, nsmap=NAMESPACES) | |
T = ElementMaker(namespace=TYPE_NS, nsmap=NAMESPACES) | |
EXCHANGE_DATETIME_FORMAT = u"%Y-%m-%dT%H:%M:%SZ" | |
EXCHANGE_DATE_FORMAT = u"%Y-%m-%d" | |
DISTINGUISHED_IDS = ( | |
'calendar', 'contacts', 'deleteditems', 'drafts', 'inbox', 'journal', 'notes', 'outbox', 'sentitems', | |
'tasks', 'msgfolderroot', 'root', 'junkemail', 'searchfolders', 'voicemail', 'recoverableitemsroot', | |
'recoverableitemsdeletions', 'recoverableitemsversions', 'recoverableitemspurges', 'archiveroot', | |
'archivemsgfolderroot', 'archivedeleteditems', 'archiverecoverableitemsroot', | |
'Archiverecoverableitemsdeletions', 'Archiverecoverableitemsversions', 'Archiverecoverableitemspurges', | |
) | |
def get_message_item(exchange_id, format=u"Default", include_mime_content=True): | |
""" | |
Requests a e-mail item from the store. | |
exchange_id is the id for this event in the Exchange store. | |
format controls how much data you get back from Exchange. Full docs are here, but acceptible values | |
are IdOnly, Default, and AllProperties. | |
http://msdn.microsoft.com/en-us/library/aa564509(v=exchg.140).aspx | |
<GetItem | |
xmlns="http://schemas.microsoft.com/exchange/services/2006/messages" | |
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> | |
<ItemShape> | |
<t:BaseShape>Default</t:BaseShape> | |
<t:IncludeMimeContent>true</t:IncludeMimeContent> | |
</ItemShape> | |
<ItemIds> | |
<t:ItemId Id="{exchange_id}"/> | |
</ItemIds> | |
</GetItem> | |
""" | |
elements = list() | |
if type(exchange_id) == list: | |
for item in exchange_id: | |
elements.append(T.ItemId(Id=item)) | |
else: | |
elements = [T.ItemId(Id=exchange_id)] | |
root = M.GetItem( | |
M.ItemShape( | |
T.BaseShape(format), | |
T.IncludeMimeContent(str(include_mime_content).lower()) | |
), | |
M.ItemIds( | |
*elements | |
) | |
) | |
return root | |
def find_item(): | |
""" | |
<?xml version="1.0" encoding="utf-8"?> | |
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"> | |
<soap:Body> | |
<FindItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages" | |
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" | |
Traversal="Shallow"> | |
<ItemShape> | |
<t:BaseShape>IdOnly</t:BaseShape> | |
</ItemShape> | |
<ParentFolderIds> | |
<t:DistinguishedFolderId Id="deleteditems"/> | |
</ParentFolderIds> | |
</FindItem> | |
</soap:Body> | |
</soap:Envelope> | |
""" | |
root = M.FindItem( | |
M.ItemShape( | |
T.BaseShape("IdOnly"), | |
), | |
M.ParentFolderIds( | |
T.DistinguishedFolderId("", Id="inbox") | |
) | |
, Traversal = "Shallow" | |
) | |
return root | |
body = find_item() | |
print("Call find item:") | |
print(lxml.etree.tostring(body, pretty_print=True, encoding="unicode")) | |
response_xml = service.send(body) | |
print("Get the following response:") | |
print(lxml.etree.tostring(response_xml, pretty_print=True, encoding="unicode")) | |
body = get_message_item("AQAVAG1haWx0ZXN0ZXJAemlnaG9tZS5zZQBGAAADuhp42pOhvkytfnXB72q3LQcAr5TtAa/wIkiHTynS3S1jjAAAAw4AAACvlO0Br/AiSIdPKdLdLWOMAAADGgAAAA==") | |
print("Call get item") | |
print(lxml.etree.tostring(body, pretty_print=True, encoding="unicode")) | |
response_xml = service.send(body) | |
print("Get the following response:") | |
print(lxml.etree.tostring(response_xml, pretty_print=True, encoding="unicode")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment