from exchangelib import DELEGATE, Account, Credentials
credentials = Credentials(
username='MYDOMAIN\\myusername', # Or [email protected] for O365
password='topsecret'
)
a = Account(
primary_smtp_address='[email protected]',
credentials=credentials,
autodiscover=True,
access_type=DELEGATE
)
# Print first 100 inbox messages in reverse order
for item in a.inbox.all().only('subject').order_by('-datetime_received')[:100]:
print(item.subject)
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.
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='MYDOMAIN\\myusername', # Or [email protected] for O365
password='topsecret'
)
a = Account(
primary_smtp_address='[email protected]',
credentials=credentials,
autodiscover=True,
access_type=DELEGATE
)
# Print first 100 inbox messages in reverse order
for item in a.inbox.all().only('subject').order_by('-datetime_received')[:100]:
print(item.subject)
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))
from email.parser import Parser
p = Parser()
msg = p.parsestr(raw_email)
现在您可以执行
msg.get('From')
msg.get('Subject')
或获取内容
msg.get_payload()
,但如果它是一条多部分消息,您将需要要进行更多处理,幸运的是,递归解决方案非常适合这种情况,
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
现在
msg_contant = process_multipart_message(msg)
每次都会给您完整的消息。
Ive got it, to connect to outbound exchange you need to connect like this:
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
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
我非常确定,如果不使用 Outlook 和 MAPI 配置文件,这是不可能的。 如果您能甜言蜜语地说服您的邮件管理员在 Exchange 服务器上启用 IMAP,那么您的生活会轻松很多。
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.
发布评论
评论(4)
我知道这是一个旧线程,但是...
如果您使用的是 Exchange 2007 或更高版本,或者 Office365,请查看 Exchange Web 服务。 它是一个非常全面的基于 SOAP 的 Exchange 界面,您几乎可以执行 Outlook 能够执行的任何操作,包括委托或模拟对其他用户帐户的访问。
https:// learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/ews-reference-for-exchange
更新:我已经发布了 PyPI 上的 Python EWS 客户端,支持自动发现、日历、收件箱、任务、联系人等:
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.
https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/ews-reference-for-exchange
UPDATE: I have released a Python EWS client on PyPI that supports autodiscover, calendars, inbox, tasks, contacts, and more:
我明白了,要连接到出站交换,您需要像这样连接:
现在您可以像正常连接一样发送
以从收件箱获取邮件,它有点不同,
这会给您一个消息 ID 列表
现在您可以使用它来获取电子邮件
raw_email 是您的电子邮件消息,但它不是很漂亮,如果您想解析它,请执行类似的操作,
现在您可以执行
或获取内容
,但如果它是一条多部分消息,您将需要要进行更多处理,幸运的是,递归解决方案非常适合这种情况,
现在
每次都会给您完整的消息。
Ive got it, to connect to outbound exchange you need to connect like this:
now you can send like a normal connection
to get the mail from your inbox its a little different
this gives you a list of message id'
s that you can use to get your emails
now raw_email is your email messsage, but its not very pretty, if you want to parse it do somthing like this
now you can do
or for the content
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
now
will give you the whole message every time.
我非常确定,如果不使用 Outlook 和 MAPI 配置文件,这是不可能的。 如果您能甜言蜜语地说服您的邮件管理员在 Exchange 服务器上启用 IMAP,那么您的生活会轻松很多。
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.
您必须找到一种方法以该特定用户的身份运行该进程。
看到这个。
我认为 pywin32 .CreateProcessAsUser 是您需要走的路径的起点。 最后一次编辑。 登录的用户句柄是通过使用 win32security.LogonUser 方法获取的
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