使用Outlook 2016通过Python发送电子邮件而无需打开它 [英] Send email through Python using Outlook 2016 without opening it

查看:157
本文介绍了使用Outlook 2016通过Python发送电子邮件而无需打开它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'To address'
mail.Subject = 'Message subject'
mail.Body = 'Message body'
mail.HTMLBody = '<h2>HTML Message body</h2>' #this field is optional

# To attach a file to the email (optional):
attachment  = "Path to the attachment"
mail.Attachments.Add(attachment)

mail.Send()

以上代码完全可以正常工作。但是问题是,需要在系统上打开Outlook并登录,然后才发送邮件。

The above code works totally fine. But the problem is that Outlook needs to be opened on the system and logged in, then only the mail is sent.

是否可以使用Outlook在不使用Outlook的情况下发送邮件的任何方式

Is there any way of send a mail using outlook without actually running outlook application on the system?

推荐答案

您需要在不自动化Outlook的情况下使用Outlook REST API。看看以下示例:

You need to use Outlook REST API without automating Outlook. Take a look at the following samples:

  • Create and send messages
  • Send a new message on the fly

请注意,Microsoft当前不建议并且不支持从任何无人参与的非交互客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT Services)自动化Microsoft Office应用程序,因为在这种环境下运行Office时,Office可能会表现出不稳定的行为和/或死锁。

Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

如果要构建在服务器端上下文中运行的解决方案,则应该尝试使用对无人值守执行安全的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方法。如果您使用服务器端解决方案中的Office应用程序,则该应用程序将缺少许多成功运行所需的功能。此外,您将承担整体解决方案稳定性的风险。在中了解有关此内容的更多信息。有关服务器端Office自动化的注意事项

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

这篇关于使用Outlook 2016通过Python发送电子邮件而无需打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆