Example #1
0
 public static string GetSha1(string path)
 {
     try {
         using (FileStream fs = new FileStream(path, FileMode.Open)) {
             return(GetSha1(fs));
         }
     } catch (Exception ex) {
         LogIt.E(ex);
         throw ex;
     }
 }
Example #2
0
        //public static void SendMessage(MailMergeMessage mmm, bool sendAsync = false, bool throwOnError = true) {
        //  try {
        //    #region Send the MailMessage (will use the Web.config settings)
        //    using (MailMergeSender mms = new MailMergeSender()) {
        //      SmtpSection smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
        //      if (smtpSection == null) {
        //        throw new Exception("smtpSection == null");
        //      } else {
        //        //mms.EnableSsl = true; //(mm.From.Address.ToLower().Contains("@gmail.com")
        //        mms.SmtpHost = smtpSection.Network.Host;
        //        mms.SmtpPort = smtpSection.Network.Port;
        //        mms.SetSmtpAuthentification(smtpSection.Network.UserName, smtpSection.Network.Password);
        //        //mms.LocalHostName = "mail." + Environment.MachineName;
        //        mms.MaxFailures = 1;
        //        //mms.DelayBetweenMessages = 1000;
        //        //mms.MailOutputDirectory = _outputFolder;
        //        //mms.MessageOutput = MessageOutput.Directory;  // change to MessageOutput.SmtpServer if you like, but be careful :)
        //        mms.MessageOutput = MessageOutput.SmtpServer;

        //        if (!sendAsync) {
        //          mms.Send(mmm);
        //        } else {
        //          mms.OnAfterSend += new EventHandler<MailSenderAfterSendEventArgs>(mms_OnAfterSend);
        //          // The userState can be any object that allows your callback method to identify this send operation. For this example, the userToken is a string constant.
        //          string userState = System.Guid.NewGuid().ToString();
        //          mms.SendAsync(mmm);

        //          // If the user canceled the send, and mail hasn't been sent yet, then cancel the pending operation.
        //          //if (answer.StartsWith("c") && mailSent == false) {
        //          //client.SendAsyncCancel();
        //          //}
        //        }
        //      }
        //    }
        //    #endregion
        //  } catch (Exception ex) {
        //    LogIt.E(ex);
        //    if (throwOnError) {
        //      throw;
        //    }
        //  }
        //}

        public static void SendMessageAsAsyncTask(MailMessage mm)
        {
            Task.Factory.StartNew(() => {
                try {
                    //tried using async, but bc the calling thread went away, it was cancelled
                    SendMessage(mm);
                } catch (Exception ex) {
                    LogIt.E(ex);
                }
            });
        }
Example #3
0
        public static string GetJson(object o, Formatting f = Formatting.None)
        {
            var json = "";

            try {
                json = Newtonsoft.Json.JsonConvert.SerializeObject(o, f);
            } catch (Exception ex) {
                LogIt.E(ex);
            } finally {
            }
            return(json);
        }
Example #4
0
        Config()
        {
            try {
                //those required
                //CHILKAT_EMAIL_KEY = GetRequiredConfigValue("CHILKAT_EMAIL_KEY");

                //those not required:
                CHILKAT_EMAIL_KEY = ConfigurationManager.AppSettings["CHILKAT_EMAIL_KEY"];
            } catch (Exception ex) {
                LogIt.E(ex);
                throw new Exception("Config error");
            }
        }
Example #5
0
        //TODO: fix this so it works w/ collection
        public static string Get(object o, bool onlyPublic = true, string delimiter = "|")
        {
            var result = "";
            var loo    = new List <object> {
            };

            try {
                if (o == null)
                {
                    loo.Add("null");
                }
                else if (o is string)
                {
                    loo.Add(o);
                }
                else
                {
                    var pia = o.GetType().GetProperties();
                    foreach (var pi in pia)
                    {
                        if (!onlyPublic || (onlyPublic && pi.PropertyType.IsPublic))
                        {
                            loo.Add(
                                pi.Name.ToString().Trim() + ":" + ((pi.GetValue(o) == null) ? "" : pi.GetValue(o).ToString().Trim())
                                );
                        }
                    }

                    var fia = o.GetType().GetFields();
                    foreach (var fi in fia)
                    {
                        if (!onlyPublic || (onlyPublic && fi.IsPublic))
                        {
                            loo.Add(
                                fi.Name.ToString().Trim() + ":" + ((fi.GetValue(o) == null) ? "" : fi.GetValue(o).ToString().Trim())
                                );
                        }
                    }
                }
            } catch (Exception ex) {
                LogIt.E(ex);
            } finally {
                try {
                    result = string.Join(delimiter, loo);
                } catch (Exception ex1) {
                    LogIt.E(ex1);
                }
            }
            return(result);
        }
Example #6
0
        private static string GetRequiredConfigValue(string key)
        {
            string value = "";

            try {
                value = ConfigurationManager.AppSettings[key];
                if (String.IsNullOrEmpty(value))
                {
                    LogIt.Log("Config value was null: " + key, Severity.Error);
                    throw new Exception("Config value was null: " + key);
                }
            } catch (Exception ex) {
                LogIt.E(ex);
                throw;
            }
            return(value);
        }
Example #7
0
 public static string GetSha1(byte[] ba)
 {
     try {
         using (var sha1 = new SHA1Managed()) {
             byte[] hash      = sha1.ComputeHash(ba);
             var    formatted = new StringBuilder(2 * hash.Length);
             foreach (var b in hash)
             {
                 formatted.AppendFormat("{0:X2}", b);
             }
             var sha1Hash = formatted.ToString();
             return(sha1Hash);
         }
     } catch (Exception ex) {
         LogIt.E(ex);
         throw ex;
     }
 }
Example #8
0
        private static void mm_SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the unique identifier for this asynchronous operation.
            String token = (string)e.UserState;

            if (e.Cancelled)
            {
                LogIt.W("[" + token + "] Send canceled.");
            }
            if (e.Error != null)
            {
                LogIt.E("[" + token + "] " + e.Error.ToString());
            }
            else
            {
                //LogIt.Log("Message sent", Severity.Info);
            }
        }
Example #9
0
        public static void SendMessage(MailMessage mm, bool sendAsync = false, bool throwOnError = true)
        {
            try {
                #region Send the MailMessage (will use the Web.config settings)
                SmtpSection smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
                if (smtpSection == null)
                {
                    throw new Exception("smtpSection == null");
                }
                else
                {
                    using (var sc = new System.Net.Mail.SmtpClient()) {
                        sc.EnableSsl = sc.EnableSsl || (mm.From.Address.ToLower().Contains("@gmail.com"));
                        //sc.Host = smtpSection.Network.Host;
                        //sc.Port = smtpSection.Network.Port;
                        //CredentialCache.DefaultNetworkCredentials.Password = smtpSection.Network.Password;
                        //CredentialCache.DefaultNetworkCredentials.UserName = smtpSection.Network.UserName;

                        if (!sendAsync)
                        {
                            sc.Send(mm);
                        }
                        else
                        {
                            // The userState can be any object that allows your callback method to identify this send operation. For this example, the userToken is a string constant.
                            string userState = System.Guid.NewGuid().ToString();

                            sc.SendCompleted += new SendCompletedEventHandler(mm_SendCompletedCallback);
                            // The userState can be any object that allows your callback method to identify this send operation. For this example, the userToken is a string constant.
                            sc.SendAsync(mm, userState);
                            //mm.Dispose();
                        }
                    }
                }
                #endregion
            } catch (Exception ex) {
                LogIt.E(ex);
                if (throwOnError)
                {
                    throw;
                }
            }
        }