Beispiel #1
0
        /// <summary>
        /// Connect to Exchange using AutoDiscover for the given email address
        /// </summary>
        /// <param name="UseDefaultCredentials">if set to true, UserName and Password will be ignored and the session credentials will be used</param>
        /// <param name="UserName">UserName for the connection</param>
        /// <param name="Password">Password for the connection</param>
        /// <param name="Mailbox">If Impersonate is true, the Mailbox is neeed which should be impersonated</param>
        /// <param name="AllowRedirection">Should normaly set to true </param>
        /// <param name="Impersonate">If a mailbox should be impersonated, this need to be set to true</param>
        /// <param name="IgnoreCertificateErrors">At now not implemented. Will be Ignored.</param>
        /// <returns>Exchange Web Service binding</returns>
        public ExchangeService Service(bool UseDefaultCredentials, string UserName, SecureString Password, string Mailbox, bool AllowRedirection, bool Impersonate, bool IgnoreCertificateErrors)
        {
            log.WriteDebugLog("Connecting to exchange with following settings:");
            log.WriteDebugLog(string.Format("UseDefaultCredentials: {0}", UseDefaultCredentials.ToString()));
            if (UserName.Length > 0)
            {
                log.WriteDebugLog(string.Format("UserName: {0}", UserName));
            }
            if (Password != null)
            {
                log.WriteDebugLog("Passwort: set");
            }

            log.WriteDebugLog(string.Format("Mailbox: {0}", Mailbox));
            log.WriteDebugLog(string.Format("AllowRedirection: {0}", AllowRedirection.ToString()));
            log.WriteDebugLog(string.Format("Impersonate: {0}", Impersonate.ToString()));
            log.WriteDebugLog(string.Format("IgnoreCertificateErrors: {0}", IgnoreCertificateErrors.ToString()));
            if (IgnoreCertificateErrors)
            {
                ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
            }

            var service = new ExchangeService();

            try
            {
                if (UseDefaultCredentials)
                {
                    service.UseDefaultCredentials = true;
                }
                else
                {
                    service.Credentials = new WebCredentials(UserName, SecureStringHelper.SecureStringToString(Password));
                }

                if (AllowRedirection)
                {
                    service.AutodiscoverUrl(Mailbox, url => true);
                }
                else
                {
                    service.AutodiscoverUrl(Mailbox);
                }

                if (Impersonate)
                {
                    service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, Mailbox);
                }
                log.WriteDebugLog("Service successfully created.");

                return(service);
            }
            catch (Exception ex)
            {
                log.WriteErrorLog("Error on creating service.");
                log.WriteErrorLog(string.Format("Exception: {0}", ex.Message));
                return(null);
            }
        }
Beispiel #2
0
 private void SaveSettings()
 {
     Settings.User     = User;
     Settings.Password = EncryptionHelper.EncryptString(SecureStringHelper.SecureStringToString(Password), EncryptionHelper.Base64Decode(Key), 8);
     if ((URL.StartsWith("http://")) || (URL.StartsWith("https://")))
     {
         Settings.URL = URL;
     }
     else
     {
         Settings.URL = "";
     }
     Settings.UseDefaultCredentials   = UseDefaultCredentials;
     Settings.IgnoreCertificateErrors = IgnoreCertificateErrors;
     Settings.AllowRedirection        = AllowRedirection;
     Settings.UseAutodiscover         = UseAutodiscover;
     Settings.Key = Key;
     Settings.SaveSettings();
 }
Beispiel #3
0
 private void LoadSettings()
 {
     Key  = Settings.Key;
     User = Settings.User;
     if (Settings.Password.Length > 0)
     {
         Password = SecureStringHelper.StringToSecureString(EncryptionHelper.DecryptString(Settings.Password, EncryptionHelper.Base64Decode(Key), 8));
     }
     if ((Settings.URL.StartsWith("http://")) || (Settings.URL.StartsWith("https://")))
     {
         URL = Settings.URL;
     }
     else
     {
         URL = "";
     }
     UseDefaultCredentials   = Settings.UseDefaultCredentials;
     IgnoreCertificateErrors = Settings.IgnoreCertificateErrors;
     AllowRedirection        = Settings.AllowRedirection;
     UseAutodiscover         = Settings.UseAutodiscover;
 }
Beispiel #4
0
        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();
            }
        }