Connect to Exchange mailbox with Python [closed]

冷暖自知 提交于 2019-11-28 03:36:11

I'm pretty sure this is going to be impossible without using Outlook and a MAPI profile. If you can sweet talk your mail admin into enabling IMAP on the Exchange server it would make your life a lot easier.

I know this is an old thread, but...

If you're using Exchange 2007 or newer, or Office365, take a look at Exchange Web Services. It's a pretty comprehensive SOAP-based interface for Exchange, and you can do pretty much anything Outlook is able to do, including delegate or impersonation access to other user accounts.

http://msdn.microsoft.com/en-us/library/bb204119.aspx

UPDATE: I have released a Python EWS client on PyPI that supports autodiscover, calendars, inbox, tasks, contacts, and more:

from exchangelib import DELEGATE, Account, Credentials

credentials = Credentials(
    username='MYWINDOMAIN\\myusername',  # Or myusername@example.com for O365
    password='topsecret'
)
account = Account(
    primary_smtp_address='john@example.com', 
    credentials=credentials, 
    autodiscover=True, 
    access_type=DELEGATE
)
# Print first 100 inbox messages in reverse order
for item in account.inbox.all().order_by('-datetime_received')[:100]:
    print(item.subject, item.body, item.attachments)
Kyle Roux

Ive got it, to connect to outbound exchange you need to connect like this:

import smtplib

url = YOUR_EXCHANGE_SERVER
conn = smtplib.SMTP(url,587)
conn.starttls()
user,password = (EXCHANGE_USER,EXCHANGE_PASSWORD)
conn.login(user,password)

now you can send like a normal connection

message = 'From: FROMADDR\nTo: TOADDRLIST\nSubject: Your subject\n\n{}'
from, to = fromaddr,toaddrs
txt = 'This is my message'
conn.sendmail(fromaddr,toaddrs,msg.format(txt))

to get the mail from your inbox its a little different

import imaplib

url = YOUR_EXCHANGE_URL
conn = imaplib.IMAP4_SSL(url,993)
user,password = (EXCHANGE_USER,EXCHANGE_PASSWORD)
conn.login(user,password)
conn.select('INBOX')
results,data = conn.search(None,'ALL')
msg_ids = data[0]
msg_id_list = msg_ids.split()

this gives you a list of message id' s that you can use to get your emails

latest_email_id = msg_id_list[-1]
result,data = conn.fetch(latest_email_id,"(RFC822)")
raw_email = data[0][1]

now raw_email is your email messsage, but its not very pretty, if you want to parse it do somthing like this

from email.parser import Parser

p = Parser()
msg = p.parsestr(raw_email)

now you can do

msg.get('From')
msg.get('Subject')

or for the content

msg.get_payload()

but if its a multipart message your going to need to do a little more processing, luckly a recursive solution is perfect for this situation

def process_multipart_message(message):
    rtn = ''
    if message.is_multipart():
        for m in message.get_payload():
            rtn += process_multipart_message(m)
    else:
        rtn += message.get_payload()
    return rtn

now

msg_contant = process_multipart_message(msg)

will give you the whole message every time.

You'll have to find a way to run the process as that particular user.

See this.

I think pywin32.CreateProcessAsUser is the start of the path you need to go down. One last edit. The logged on user handle is obtained from using the win32security.LogonUser method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!