简体   繁体   English

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

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

I want to take backups of all emails from SQL Server database and need to create it's backup file using C# code in outlook compatible format.我想从 SQL 服务器数据库备份所有电子邮件,需要使用 outlook 兼容格式的 C# 代码创建备份文件。 So that emails can be restored in outlook software.这样就可以在outlook软件中恢复邮件了。

Please help请帮忙

Till now we have created one desktop application and we have table containing emails which has some custom fields also as per our need.到目前为止,我们已经创建了一个桌面应用程序,并且我们有包含电子邮件的表格,其中还根据我们的需要具有一些自定义字段。

We have done some exploration on it and found given below links -我们对其进行了一些探索,发现以下链接 -

Can I read an Outlook (2003/2007) PST file in C#? 我可以在 C# 中读取 Outlook (2003/2007) PST 文件吗?

http://www.c-sharpcorner.com/article/outlook-integration-in-C-Sharp/ 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/ 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#? 我可以在 C# 中读取 Outlook (2003/2007) PST 文件吗?

My problem is that we have some custom fields in database so how they will get stored in outlook data files我的问题是我们在数据库中有一些自定义字段,所以它们将如何存储在 outlook 数据文件中

Email table structure is given below - Email表结构如下-

表结构

You can use Outlook Object Model and its 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.您可以使用 Outlook Object Model 及其 Namespace.AddStore/ AddStoreEx方法将新的或现有的 PST 文件添加到配置文件,然后(给定返回的 Store 对象)用文件夹和电子邮件填充它。 To store custom properties, use MailItem.UserProperties collection.要存储自定义属性,请使用MailItem.UserProperties集合。

Note however that OOM will not work in a service - you'd need Extended MAPI (C++ or Delphi) or Redemption (I am its author - any language) for that.但是请注意,OOM 不会在服务中工作 - 为此,您需要 扩展 MAPI (C++ 或 Delphi)或Redemption (我是它的作者 - 任何语言)。 Creating items in the sent state can also be a challenge.在发送的 state 中创建项目也可能是一个挑战。 If using Redemption is an option, it exposes RDOSession .如果使用Redemption是一个选项,它会公开RDOSession LogonPstStore method that create (and deletes) a temporary profile configured to work with the specified PST file. LogonPstStore方法创建(和删除)配置为使用指定 PST 文件的临时配置文件。 It can be used i na service.它可以在服务中使用。 No existing Outlook profiles are affected.现有的 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();

Your description is still not precise enough.你的描述还是不够准确。

Do you want to store individual e-mails from outlook into database and eventually get them back as e-mails in Outlook?您是否要将来自 outlook 的个人电子邮件存储到数据库中,并最终将它们作为 Outlook 中的电子邮件取回?

Then it looks like you have all you need.然后看起来你拥有你需要的一切。 MailItem in Outlook has more-less properties like you in database. Outlook 中的 MailItem 在数据库中具有像您这样的 more-less 属性。

Then conceptually it should look like this: Enumarate MAPIFolder and for each e-mail store properties inside database and then delete e-mail.然后在概念上它应该看起来像这样: Enumarate MAPIFolder 并为数据库中的每个电子邮件存储属性,然后删除电子邮件。

In case of restoring read database record, create new MailItem and add it to folder.在恢复读取数据库记录的情况下,创建新的 MailItem 并将其添加到文件夹中。

BUT: I see some problems with field types - ie EmailTo nvarchar(100) is far too small.但是:我发现字段类型存在一些问题 - 即 EmailTo nvarchar(100) 太小了。 Additionally you don't have all fields to restore e-mail 1:1.此外,您没有所有字段来 1:1 恢复电子邮件。

So ie storing msg file could be good option (maybe additionally to data you are retrieving).因此,即存储 msg 文件可能是一个不错的选择(也许除了您正在检索的数据之外)。

Please specify more details then I could also answer in better way.请指定更多详细信息,然后我也可以以更好的方式回答。

EDIT:编辑:

According to your clarification (still not sure if I understand correctly): Simpliest way would be to use outlook for this process.根据您的澄清(仍然不确定我是否理解正确):最简单的方法是在此过程中使用 outlook。

Create in outlook pst folder, create e-mails inside and then backup complete pst file (then you have all in one file) or export individually e-mails to.msg files (then 1 file per e-mail).在 outlook pst 文件夹中创建,在其中创建电子邮件,然后备份完整的 pst 文件(然后您将所有内容都放在一个文件中)或将单独的电子邮件导出到 .msg 文件(然后每个电子邮件 1 个文件)。

Trying to write directly from your application to pst file or to msg file could be very hard since format of those files are not described.尝试直接从您的应用程序写入 pst 文件或 msg 文件可能非常困难,因为没有描述这些文件的格式。

Please precise if you want to use Outlook for this process or not.如果您想在此过程中使用 Outlook,请准确说明。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM