'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. 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#?
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#?
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 -
Solution 1:[1]
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. To store custom properties, use MailItem.UserProperties collection.
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. Creating items in the sent state can also be a challenge. If using Redemption is an option, it exposes RDOSession.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", "[email protected]", "SMTP");
item.UserProperties.Add("My custom prop", olText).Value = "custom prop value";
item.Save();
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |