-1

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 -

table structure

Community
  • 1
  • 1
keerti
  • 455
  • 2
  • 13
  • How does your desktop application store the data in tables? Reverse that process. Does it grab each individual email and store as a blob? Does it extract each piece of data into custom fields? – Nick.Mc Nov 20 '16 at 08:40
  • Please check table structure given above. – keerti Nov 20 '16 at 08:49
  • Your existing code must use the office API to extract emails. You need to use the Office API to create them. There is no way to just 'restore' them - this is a completely custom format that you are storing them in. If you had also stored the original `.msg` file it might be more straightforward – Nick.Mc Nov 20 '16 at 09:04
  • We are using custom format because from our application we are sending and manipulating emails similar to outlook software. – keerti Nov 20 '16 at 09:12
  • I'm not going to be able to help much more but basically it looks like Outlook supports custom fields, and you can work with outlook through the API. The problem I see is that you can't create a 'sent' email unless you actually send it again. Maybe you could explain further - what's the use of generating a bunch of mails inside outlook that are based on data in a table? – Nick.Mc Nov 20 '16 at 09:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128561/discussion-between-keerti-systematix-infotech-and-nick-mcdermaid). – keerti Nov 20 '16 at 09:40

2 Answers2

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", "user@domain.demo", "SMTP");
item.UserProperties.Add("My custom prop", olText).Value = "custom prop value";
item.Save();
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

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.

smartobelix
  • 748
  • 5
  • 16
  • We have c# windows application similar to outlook software, that synchronize email, with compose new, reply and reply all provisions. we have to create backup of all the emails from database that we getting using IMAP and POP3 server, in outlook compatible format. so that we can restore them in outlook. – keerti Nov 20 '16 at 09:45