IMAP Backup and Restore Operation

Standard Backup and Restore

Aspose.Email for .NET provides the ability to backup and restore messages. For this, the API provides the following methods.

This article demonstrates how to backup and restore messages using the ImapClient class.

Backup Messages

To backup messages, use the ImapClient.Backup method as demonstrated in the following code snippet.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_IMAP();
// Create an instance of the ImapClient class
ImapClient imapClient = new ImapClient();
// Specify host, username and password, and set port for your client
imapClient.Host = "imap.gmail.com";
imapClient.Username = "your.username@gmail.com";
imapClient.Password = "your.password";
imapClient.Port = 993;
imapClient.SecurityOptions = SecurityOptions.Auto;
ImapMailboxInfo mailboxInfo = imapClient.MailboxInfo;
ImapFolderInfo info = imapClient.GetFolderInfo(mailboxInfo.Inbox.Name);
ImapFolderInfoCollection infos = new ImapFolderInfoCollection();
infos.Add(info);
imapClient.Backup(infos, dataDir + @"\ImapBackup.pst", BackupOptions.Recursive);

Restore Messages

To restore messages, use the ImapClient.Restore method as demonstrated in the following code snippet.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_IMAP();
// Create an instance of the ImapClient class
ImapClient imapClient = new ImapClient();
// Specify host, username and password, and set port for your client
imapClient.Host = "imap.gmail.com";
imapClient.Username = "your.username@gmail.com";
imapClient.Password = "your.password";
imapClient.Port = 993;
imapClient.SecurityOptions = SecurityOptions.Auto;
RestoreSettings settings = new RestoreSettings();
settings.Recursive = true;
PersonalStorage pst = PersonalStorage.FromFile(dataDir + @"\ImapBackup.pst");
imapClient.Restore(pst, settings);

IMAP Backup and Restore with MultiConnection

When working with a large number of messages, the backup/restore operation can take a long time. For this, the API provides support for multiple connections during backup and restore operation. To enable the MultiConnection mode, set ImapClient.UseMultiConnection property to MultiConnectionMode.Enable. The following code snippets demonstrate backup and restore operation with MultiConnection mode enabled.

The following code snippets demonstrate backup operation with MultiConnection mode enabled.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_IMAP();
// Create an instance of the ImapClient class
ImapClient imapClient = new ImapClient();
// Specify host, username and password, and set port for your client
imapClient.Host = "imap.gmail.com";
imapClient.Username = "your.username@gmail.com";
imapClient.Password = "your.password";
imapClient.Port = 993;
imapClient.SecurityOptions = SecurityOptions.Auto;
imapClient.UseMultiConnection = MultiConnectionMode.Enable;
ImapMailboxInfo mailboxInfo = imapClient.MailboxInfo;
ImapFolderInfo info = imapClient.GetFolderInfo(mailboxInfo.Inbox.Name);
ImapFolderInfoCollection infos = new ImapFolderInfoCollection();
infos.Add(info);
imapClient.Backup(infos, dataDir + @"\ImapBackup.pst", BackupOptions.Recursive);

The following code snippets demonstrate restore operation with MultiConnection mode enabled.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_IMAP();
// Create an instance of the ImapClient class
ImapClient imapClient = new ImapClient();
// Specify host, username and password, and set port for your client
imapClient.Host = "imap.gmail.com";
imapClient.Username = "your.username@gmail.com";
imapClient.Password = "your.password";
imapClient.Port = 993;
imapClient.SecurityOptions = SecurityOptions.Auto;
imapClient.UseMultiConnection = MultiConnectionMode.Enable;
RestoreSettings settings = new RestoreSettings();
settings.Recursive = true;
PersonalStorage pst = PersonalStorage.FromFile(dataDir + @"\Outlook.pst");
imapClient.Restore(pst, settings);