简体   繁体   English

监控通过 Python win32.Dispatch('Outlook.Application') 发送的电子邮件?

[英]Monitor E-mail sent via Python win32.Dispatch('Outlook.Application')?

I'm having a bit of trouble getting to a solution with the following, I created a function that sends emails via Outlook, using the win32 package, cause the smtblib in blocked where I work.我在使用以下解决方案时遇到了一些麻烦,我创建了一个 function,它通过 Outlook 发送电子邮件,使用 win32 ZEFE90A8E604A7C840E88D03A67F6B7 在其中阻止了 I8D03A67F6B7 工作。 The function itself works like a charm: function 本身就像一个魅力:

def sendEmail(emailTo, emailCC, emailSubject, emailAttach, emailBody):
    for item in psutil.pids():
        if psutil.pid_exists(item):
            p = psutil.Process(item)
            if p.name() == 'OUTLOOK.EXE':
                os.system('taskkill /f /im OUTLOOK.EXE')
                break
            
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.Subject = emailSubject
    mail.To = emailTo
    mail.CC = emailCC
    #mail.Body = emailBody
    mail.HTMLBody = emailBody
    if not emailAttach:
        print('No Attachements...\n')
    else:
        print('Attaching files...\n')
        for i in range(0, len(emailAttach)):
            mail.Attachments.Add(emailAttach[i])
    mail.Send()
    time.sleep(30)

The thing here is, sometimes we need to send multiple different e-mails to different people inside a for loop, however since the code goes faster than the e-mail creation, sometimes it fails, I was wondering if there was a way for me to monitor if the e-mail finished, cause from what I could tell, on the task bar it open a outlook with a gear while it's creating and sending the e-mail.这里的问题是,有时我们需要在 for 循环中向不同的人发送多封不同的电子邮件,但是由于代码比电子邮件创建速度更快,有时它会失败,我想知道是否有适合我的方法要监视电子邮件是否完成,据我所知,在任务栏上它会在创建和发送电子邮件时打开一个带有齿轮的 outlook。 The main point here is if there is a way for me to monitor when the e-mail has been created and indeed sent for me to move on to the next item.这里的要点是我是否有一种方法可以监控电子邮件何时创建并确实发送给我以继续下一个项目。

I forced a wait with the 30 seconds sleep but I'm sure there is a better and more pythonic way to do this.我强迫等待 30 秒的睡眠,但我确信有更好、更 Python 的方式来做到这一点。

Email submission is inherently asynchronous. Email 提交本质上是异步的。 Even if a message is successfully sent by Outlook, it is possible the target mailbox no longer exists and you will get back an NDR, sometimes hours later.即使 Outlook 成功发送了一条消息,目标邮箱也可能不再存在,您有时会在数小时后收到 NDR。

If MailItem.Send did not error out, this is the best you can do.如果MailItem.Send没有出错,这是您能做的最好的事情。

To be sure the email is processed and sent out you can handle the ItemAdd event of the Items class, it is fired when one or more items are added to the specified collection.为确保 email 被处理并发送出去,您可以处理Items class 的ItemAdd事件,当一个或多个项目添加到指定集合时会触发它。 By default, Outlook puts copies of sent items to the Sent Items folder.默认情况下,Outlook 将已发送项目的副本放入已Sent Items文件夹。 The MailItem.SaveSentMessageFolder property specifies a Folder object that represents the folder in which a copy of the email message will be saved after being sent. MailItem.SaveSentMessageFolder属性指定了一个Folder object,它表示 email 消息的副本在发送后将保存在其中的文件夹。 So, you can check out when the message was sent out.因此,您可以查看消息的发送时间。

Another aspect of using the Send method from external application is security.从外部应用程序使用 Send 方法的另一个方面是安全性。 You may get errors on the Send line if the Outlook object model raise a security issue.如果 Outlook object model 引发安全问题,您可能会在Send行上遇到错误。 For example, in Microsoft Outlook 2019, Outlook for Office 365, Outlook 2016, and Outlook 2013, when you send an email message from another program such as Microsoft Excel, you receive the following warning message: For example, in Microsoft Outlook 2019, Outlook for Office 365, Outlook 2016, and Outlook 2013, when you send an email message from another program such as Microsoft Excel, you receive the following warning message:

A program is trying to send an e-mail message on your behalf.一个程序正试图代表您发送一封电子邮件。 If this is unexpected, click Deny and verify your antivirus software is up-to-date.如果这是意外情况,请单击拒绝并确认您的防病毒软件是最新的。

But also there is a chance to get an exception at runtime on the Send line instead.但也有机会在运行时在Send行上获得异常。 Most probably that is the case!很可能就是这样!

Read more about that in the A program is trying to send an e-mail message on your behalf warning in Outlook article. 在 Outlook 文章中的 A 程序试图代表您发送电子邮件警告中阅读有关此内容的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 win32com 通过 Python 访问 Outlook.Application 导致错误 - Accessing Outlook.Application via Python using win32com results in error Python Win32 Outlook。应用程序失败 - Python Win32 Outlook.Application failure Python 中的 win32.Dispatch 与 win32.gencache。 优缺点都有什么? - win32.Dispatch vs win32.gencache in Python. What are the pros and cons? win32com.client.Dispatch(“Outlook.Application”)错误pywintypes.com_error:( - 2147221005,'无效的类字符串',无,无) - win32com.client.Dispatch(“Outlook.Application”) error pywintypes.com_error: (-2147221005, 'Invalid class string', None, None) 检测发送(或不发送)Python发送的Outlook邮件(使用win32com) - Detect sending (or not) of Outlook mail sent by Python (with win32com) Word = win32.Dispatch("Word.Application") 尽管有 Word.Visible = False 命令,但每隔一次显示打开的文件 - Word = win32.Dispatch("Word.Application") Displays the opened file every other time despite Word.Visible = False command win32com.client.gencache.EnsureDispatch(“ Outlook.Application”)不起作用 - win32com.client.gencache.EnsureDispatch(“Outlook.Application”) not working Python client.Dispatch("outlook.application") 在特定情况下失败(@company 电子邮件) - Python client.Dispatch("outlook.application") fails in specific case (@company emails) 是什么阻止 win32.Dispatch() 打开 Microsoft Office 程序? - What is stopping win32.Dispatch() from opening Microsoft Office programs? 使用 python 读取选定 Outlook 电子邮件的内容 - Read contents of a selected Outlook e-mail using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM