【问题标题】:Create outlook draft email in python with out launching outlook application在 python 中创建 Outlook 草稿电子邮件而不启动 Outlook 应用程序
【发布时间】:2018-11-26 12:19:39
【问题描述】:
我需要在不启动 Outlook 应用程序的情况下创建电子邮件草稿并以 msg 格式保存。
(或)
我有一个现有的草稿 msg 文件,我需要修改该文件的发件人、正文和附件并保存为 msg 文件。
我尝试了 win32,它工作正常,但它正在我的系统中启动 Outlook 应用程序。在我的服务器中,没有 Outlook 应用程序。
你能告诉我有没有其他方法可以生成 msg 文件。
【问题讨论】:
标签:
python
python-3.x
outlook
msgpack
【解决方案1】:
如果您不想使用 Outlook 对象模型,则几乎只能使用 Aspose 之类的库(它无需安装 Outlook 即可处理 MSG 文件,但您的使用范围可能会有所不同)或 @ 987654322@(披露:我是它的作者) - 它需要安装 MAPI 系统(这意味着必须安装 Outlook),但如果您使用的是 RDOSession.CreateMsgFile,它不会启动 Outlook(通过设置各种RDOMail 属性和/或使用RDOMail.Import 后跟RDOMail.Save 导入现有的MSG 文件。
根据 OP 请求更新。
我不使用 Python,但在 VB 脚本中它会类似于以下内容:
Set Session = CreateObject("Redemption.RDOSession")
set newMsg = Session.CreateMessageFromMsgFile("c:\temp\new.msg")
newMsg.Import("c:\temp\template.msg", 3)
newMsg.Body = "updated body"
newMsg.Save
【解决方案2】:
您可以通过 .NET 使用 Aspose.Email for Python 创建电子邮件草稿并将其保存为 MSG,使用下面给出的代码示例:
eml = MailMessage()
# Set from, to, subject and body properties
eml.from_address = "sender@domain.com";
eml.to.append("receiver@domain.com");
eml.subject = "This is test message";
eml.body = "This is test body";
# Create an instance of the MapiMessage class and pass MailMessage as argument
outlookMsg = MapiMessage.from_mail_message(eml);
# Save the message (MSG) file
strMsgFile = "CreatingAndSavingOutlookMessages_out.msg"
outlookMsg.save(dataDir + strMsgFile);
注意:我在 Aspose 担任支持开发人员/宣传员。