如何通过Python使用线程发送和读取Outlook邮件?

huangapple go评论59阅读模式
英文:

How to send and read mails from outlook through python via threads?

问题

我正在尝试使用Python通过线程读取和发送Outlook邮件。我尝试使用win32com.client以及pythoncom

outlook = win32.Dispatch("Outlook.Application")
mapi = outlook.GetNamespace("MAPI")
inbox = mapi.GetDefaultFolder(6)
messages = inbox.Items
messages = messages.Restrict("[ReceivedTime] >= '" + maintenance_date + "'")
# ...
# ...
# ...
for message in messages:
    mail = message.ReplyAll()
    mail.To = mail.To
    mail.CC = mail.CC
    mail.Body = f"This is a reply!\nRegards\n{mail.Body}"
    mail.Save()
    mail.Send()

我似乎不明白如何在多线程环境中执行此操作,因为有很多这样的回复。

我希望能在多线程环境中执行此操作,以便更高效地利用资源。

英文:

I am trying to read and send mails through outlook using python via threads. I am trying to use win32com.client along with pythoncom.

outlook         = win32.Dispatch("Outlook.Application")
mapi            = outlook.GetNamespace("MAPI")
inbox           = mapi.GetDefaultFolder(6)
messages        = inbox.Items
messages        = messages.Restrict("[ReceivedTime] >= '"+maintenance_date+"'")
.....
.....
.....
for message in messages:
    mail = message.ReplyAll()
    mail.To = mail.To
    mail.CC = mail.CC
    mail.Body = f"This is a reply!\nRegards\n{mail.Body}"
    mail.Save()
    mail.Send()

I don't seem to understand how to do this in a threaded environment as there many such replies.

I am expecting to do this in a threaded environment so that I can use the resources more efficiently.

答案1

得分: 0

Outlook使用单线程模型,不支持从多个线程调用属性和方法。最新的Outlook版本在检测到这种情况时可能会引发异常。

如果您需要使用多个线程,您可以选择基于Outlook的低级API - Extended MAPI,它允许运行多个线程。或者考虑使用围绕该API的第三方包装器,如Redemption,以处理多线程。

另外,我注意到以下代码片段,在回复中设置了"To"和"Cc"属性:

for message in messages:
    mail = message.ReplyAll()
    mail.To = mail.To
    mail.CC = mail.CC
    mail.Body = f"This is a reply!\nRegards\n{mail.Body}"
    mail.Save()
    mail.Send()

其中以下代码行没有任何意义:

mail.To = mail.To
mail.CC = mail.CC

当调用ReplyAll方法时,收件人会自动设置。只需尝试在Outlook的功能区上单击相应的按钮,您将获得一个包含所有与收件人相关属性的回复设置。

英文:

Outlook uses a single-threaded apartment model and doesn't support calling properties and methods from multiple threads. Latest Outlook versions can throw an exception when such cases are detected.

If you need to use multiple threads your choice is a low-level API on which Outlook is based on - Extended MAPI which allows running multiple threads. Or just consider using any third-party wrappers around that API such as Redemption where you could deal with multithreading.

Also I've noticed the following piece of code where the To, Cc properties are set on the reply:

for message in messages:
    mail = message.ReplyAll()
    mail.To = mail.To
    mail.CC = mail.CC
    mail.Body = f"This is a reply!\nRegards\n{mail.Body}"
    mail.Save()
    mail.Send()

where the following lines of code don't make any sense:

mail.To = mail.To
mail.CC = mail.CC

When you call the ReplyAll method recipients are set up automatically. Just try to click the corresponding button on the ribbon in Outlook and you will get a reply set with all recipient-related properties out of the box.

huangapple
  • 本文由 发表于 2023年2月24日 15:19:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定