Esempio n. 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);
            }
        }
Esempio n. 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();
 }