private void ImportCategories() { OpenFileDialog OpenFile = new OpenFileDialog { Filter = "Serialized categories|*.xml", Title = "Select a file name to save" }; DialogResult result = OpenFile.ShowDialog(); if (result == DialogResult.OK) { int imported = CategoryHelper.Import(EWSService, OpenFile.FileName, chkClearOnImport.Checked, txtMailbox.Text); if (imported > 0) { lblAction.Text = string.Format("{0} Categories successfully imported", imported); } else { lblAction.Text = "No categories were imported. Check log or import file."; } } }
private void ExportCategories() { SaveFileDialog SaveFile = new SaveFileDialog { Filter = "Serialized Categories|*.xml", Title = "Select a file name to save" }; DialogResult result = SaveFile.ShowDialog(); if (result == DialogResult.OK) { if (SaveFile.FileName != "") { try { int tmpExport = CategoryHelper.Export(EWSService, SaveFile.FileName, txtMailbox.Text); // Do we have more then 0 categories? If not the export will be failing if (tmpExport > 0) { lblAction.Text = string.Format("{0} Categories exported successfully to {1}", tmpExport, SaveFile.FileName); } else { MessageBox.Show("No categories exported.", "Nothing to export", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (System.ArgumentNullException) { MessageBox.Show("The category list empty. No file written.", "Nothing to export", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { MessageBox.Show("Error on export. Please consult the log file.", "Error on export", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
private void CopyCategories() { if (txtTargetAddress.Text.Length != 0) { lblAction.Text = "Conneting to target mailbox"; try { TargetEWS = TargetHelper.Service(Settings.UseDefaultCredentials, Settings.User, Password, txtTargetAddress.Text, Settings.AllowRedirection, chkTargetMailboxImpersonate.Checked, Settings.IgnoreCertificateErrors); lblAction.Text = "Connected to target mailbox"; log.WriteErrorLog(string.Format("Connected to mailbox \"{0}\" successfully", txtTargetAddress.Text)); } catch { MessageBox.Show("Error connecting to target mailbox. Check mailbox name and permissions.", "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error); log.WriteErrorLog(string.Format("Connection to mailbox \"{0}\" failed", txtTargetAddress.Text)); } if (TargetEWS != null) { int tmpCopy = CategoryHelper.CopyCategories(EWSService, TargetEWS, chkClearTargetListBeforeImport.Checked, txtMailbox.Text, txtTargetAddress.Text); if (tmpCopy > 0) { lblAction.Text = string.Format("{0} categories copied", tmpCopy); } else { lblAction.Text = "Error on copying."; MessageBox.Show("Error on copying. Please consult the log file.", "Export error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else { MessageBox.Show("Enter a target address.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
static void Main(string[] args) { log.WriteDebugLog("Main() started"); var arguments = new UtilityArguments(args); string Mailbox, User, URL, Key; SecureString Password; bool UseDefaultCredentials, IgnoreCertificateErrors, AllowRedirection; ExchangeHelper EWSHelper = new ExchangeHelper(); ExchangeService EWSService = new ExchangeService(); if (args.Length > 0) { log.WriteDebugLog("Arguments passed to executable"); if (arguments.Help) { DisplayHelp(); Environment.Exit(0); } // If using settings than load all settings if (arguments.UseSettings) { MySettings Settings = new MySettings(); Key = Settings.Key; if (Settings.User.Length > 0) { User = Settings.User; } else { User = ""; } if (Settings.Password.Length > 0) { Password = SecureStringHelper.StringToSecureString(EncryptionHelper.DecryptString(Settings.Password, EncryptionHelper.Base64Decode(Key), 8)); } else { Password = null; } if ((Settings.URL.StartsWith("http://")) || (Settings.URL.StartsWith("https://"))) { URL = Settings.URL; } else { URL = ""; } UseDefaultCredentials = Settings.UseDefaultCredentials; IgnoreCertificateErrors = Settings.IgnoreCertificateErrors; AllowRedirection = Settings.AllowRedirection; Console.WriteLine("Settings loaded from settings file"); log.WriteDebugLog("Settings loaded from settings file"); } else { if (arguments.User.Length == 0 || arguments.Password.Length == 0) { UseDefaultCredentials = true; log.WriteDebugLog("No user or passsword given. Using default credentials."); User = ""; Password = null; } else { UseDefaultCredentials = false; User = arguments.User; Password = Password = SecureStringHelper.StringToSecureString(arguments.Password); log.WriteDebugLog(string.Format("-user: {0}", arguments.User)); log.WriteDebugLog("-password: set"); } IgnoreCertificateErrors = arguments.IgnoreCertificate; } Mailbox = arguments.Mailbox; AllowRedirection = arguments.AllowRedirection; if (arguments.URL.Length > 0) { URL = arguments.URL; } else { URL = ""; } if (string.IsNullOrEmpty(arguments.ImportFile) && string.IsNullOrEmpty(arguments.ExportFile)) { log.WriteErrorLog("No import or export file was given."); DisplayHelp(); Environment.Exit(1); } if (arguments.URL.Length == 0) { // Autodiscover if (UseDefaultCredentials) { EWSService = EWSHelper.Service(UseDefaultCredentials, "", null, Mailbox, AllowRedirection, arguments.Impersonate, IgnoreCertificateErrors); } else { EWSService = EWSHelper.Service(UseDefaultCredentials, User, Password, Mailbox, AllowRedirection, arguments.Impersonate, IgnoreCertificateErrors); } } else { // URL if (UseDefaultCredentials) { EWSService = EWSHelper.Service(UseDefaultCredentials, "", null, Mailbox, URL, arguments.Impersonate, IgnoreCertificateErrors); } else { EWSService = EWSHelper.Service(UseDefaultCredentials, User, Password, Mailbox, URL, arguments.Impersonate, IgnoreCertificateErrors); } } if (EWSService != null) { if (!(string.IsNullOrEmpty(arguments.ImportFile))) { int imported = CategoryHelper.Import(EWSService, arguments.ImportFile, arguments.ClearOnImport, arguments.Mailbox); Console.WriteLine(string.Format("{0} categories imported", imported)); Environment.Exit(0); } else // arguments.ExportFile should be non-null and not empty { int exported = CategoryHelper.Export(EWSService, arguments.ExportFile, arguments.Mailbox); Console.WriteLine(string.Format("{0} categories exported", exported)); Environment.Exit(0); } } else { string errorMessage = "Error on creating the service. Check permissions and if the server is available."; Console.WriteLine(errorMessage); log.WriteErrorLog(errorMessage); Environment.Exit(2); } } else { log.WriteDebugLog("Main() ended"); var handle = GetConsoleWindow(); ShowWindow(handle, SW_HIDE); StartForm(); } }