Question: 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. So that emails can be restored in outlook software.

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#?](https://stackoverflow.com/questions/577904/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#?](https://stackoverflow.com/questions/577904/can-i-read-an-outlook-2003-2007-pst-file-in-c?noredirect=1&lq=1)

My problem is that we have some custom fields in database so how they will get stored in outlook data files

Email table structure is given below - 

[![table structure](https://i.stack.imgur.com/ncSXC.png)](https://i.stack.imgur.com/ncSXC.png)

Answer: 
You can use Outlook Object Model and its Namespace.AddStore/[AddStoreEx](https://msdn.microsoft.com/en-us/library/office/ff861574.aspx) 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](https://msdn.microsoft.com/en-us/library/office/ff862196.aspx) collection.

Note however that OOM will not work in a service - you'd need [Extended MAPI](https://msdn.microsoft.com/en-us/library/office/cc815424.aspx) (C++ or Delphi) or [Redemption](http://www.dimastr.com/redemption) (I am its author - any language) for that. Creating items in the sent state can also be a challenge. If using [Redemption](http://www.dimastr.com/redemption/) is an option, it exposes [RDOSession](http://www.dimastr.com/redemption/rdosession.htm).`LogonPstStore` method that create (and deletes) a temporary profile configured to work with the specified PST file. It can be used i na service. No existing Outlook profiles are affected.

```
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?

Then it looks like you have all you need.
MailItem in Outlook has more-less properties like you in database.

Then conceptually it should look like this:
Enumarate MAPIFolder and for each e-mail store properties inside database and then delete e-mail.

In case of restoring read database record, create new MailItem and add it to folder.

BUT:
I see some problems with field types - i.e. EmailTo nvarchar(100) is far too small.
Additionally you don't have all fields to restore e-mail 1:1.

So i.e. storing msg file could be good option (maybe additionally to data you are retrieving).

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.

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).

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.

Please precise if you want to use Outlook for this process or not.