如何通过 C# 代码创建 Outlook 兼容格式的 SQL 服务器数据备份文件

How to create SQL Server data backup file in Outlook Compatible format by C# code

我想备份来自 SQL 服务器数据库的所有电子邮件,并且需要使用 C# 代码以 outlook 兼容格式创建它的备份文件。以便在outlook软件中恢复邮件。

请帮忙

到目前为止,我们已经创建了一个桌面应用程序,并且我们有 table 包含电子邮件,其中还根据我们的需要具有一些自定义字段。

我们对其进行了一些探索,并找到了以下链接 -

Can I read an Outlook (2003/2007) PST file in C#?

http://www.c-sharpcorner.com/article/outlook-integration-in-C-Sharp/

https://www.add-in-express.com/creating-addins-blog/2013/12/20/create-outlook-files/

Can I read an Outlook (2003/2007) PST file in C#?

我的问题是我们在数据库中有一些自定义字段,因此如何将它们存储在 outlook 数据文件中

电子邮件 table 结构如下 -

您的描述还不够准确。

您要将 Outlook 中的个别电子邮件存储到数据库中并最终将它们作为电子邮件取回 Outlook 中吗?

那么看起来你已经拥有了你所需要的一切。 Outlook 中的 MailItem 与您在数据库中一样具有 more-less 属性。

那么从概念上讲它应该是这样的: 枚举 MAPIFolder 并为数据库中的每个电子邮件存储属性,然后删除电子邮件。

如果要恢复读取的数据库记录,请创建新的 MailItem 并将其添加到文件夹。

但是: 我发现字段类型存在一些问题 - 即 EmailTo nvarchar(100) 太小了。 此外,您没有所有字段来恢复电子邮件 1:1。

因此,即存储 msg 文件可能是一个不错的选择(也许除了您正在检索的数据之外)。

请详细说明,我会更好地回答。

编辑:

根据你的说明(还是不确定我理解的对不对): 最简单的方法是在此过程中使用 outlook。

在 outlook pst 文件夹中创建,在其中创建电子邮件,然后备份完整的 pst 文件(然后您将所有内容都放在一个文件中)或将单独的电子邮件导出到 .msg 文件(然后每个电子邮件 1 个文件) .

尝试直接从您的应用程序写入 pst 文件或 msg 文件可能非常困难,因为未描述这些文件的格式。

请准确说明您是否要在此过程中使用 Outlook。

您可以使用 Outlook 对象模型及其 Namespace.AddStore/AddStoreEx methods to add new or existing PST file to a profile and then (given the returned Store object) populate it with folders and emails. To store custom properties, use MailItem.UserProperties 集合。

但请注意,OOM 不会在服务中工作 - 您需要 Extended MAPI (C++ or Delphi) or Redemption (I am its author - any language) for that. Creating items in the sent state can also be a challenge. If using Redemption is an option, it exposes RDOSession.LogonPstStore 方法来创建(和删除)配置为使用指定 PST 文件的临时配置文件。它可以在服务中使用。没有现有的 Outlook 配置文件受到影响。

Redemption.RDOSession session = new Redemption.RDOSession();
Redemption.RDOPstStore store = session.LogonPstStore(PstFileName);
Redemption.RDOFolder folder = store.IPMRootFolder.Folders.Add("Backup folder");
RDOMail item = folder.Items.Add("IPM.Note");
item.Sent = true;
item.Subject = "test";
item.Body = "test body";
item.Recipients.AddEx("The User", "user@domain.demo", "SMTP");
item.UserProperties.Add("My custom prop", olText).Value = "custom prop value";
item.Save();