示例#1
0
        public static T ToEnum <T>(this string strEnum)
        {
            T t = default(T);

            try
            {
                t = (T)Enum.Parse(typeof(T), strEnum);
            }
            catch (Exception ex)
            {
                Debuging.Warning(ex, "Class: Utility, Method: ToEnum cannot cast the strEnum = " + strEnum + " To Enum ");
            }
            return(t);
        }
示例#2
0
        public static ActionResult SendEmail(string smtpHost, string from, string to, string body, string subject, string fromPassword)
        {
            ActionResult result = new ActionResult();

            try
            {
                if (!IsValidEmail(to) || !IsValidEmail(@from))
                {
                    throw new LocalException("EmailAddress is wrong", "آدرس ایمیل صحیح نیست", "toEmailAddress", to, "fromEmailAddress", @from);
                }

                MailMessage message = new MailMessage(@from, to, subject, body)
                {
                    BodyEncoding = Encoding.UTF8,
                    IsBodyHtml   = true,
                    DeliveryNotificationOptions = DeliveryNotificationOptions.None,
                    Priority        = MailPriority.High,
                    SubjectEncoding = Encoding.UTF8
                };

                SmtpClient client = new SmtpClient(smtpHost); //example:"mto-foods.ir"
                client.Credentials = new NetworkCredential(@from, fromPassword);
                client.Send(message);

                result.IsSuccess     = true;
                result.ResultMessage = MessageText.SUCCESS;
            }
            catch (LocalException ex)
            {
                Debuging.Warning(ex, "SendEmail Function");
                result.IsSuccess      = false;
                result.EnglishMessage = ex.Message;
                result.ResultMessage  = ex.ResultMessage;
            }
            catch (Exception ex)
            {
                ex.Data.Add("From", @from);
                ex.Data.Add("To", to);
                ex.Data.Add("Body", body);
                ex.Data.Add("Subject", subject);
                Debuging.Error(ex, "SendEmail Function");
                result.IsSuccess      = false;
                result.EnglishMessage = ex.Message;
                result.ResultMessage  = MessageText.UNKNOWN_ERROR;
            }
            return(result);
        }
示例#3
0
        public static DateTime ToEnDate(this string value)
        {
            try
            {
                int temp2     = 0;
                int year      = int.Parse(value.Substring(0, temp2 = value.IndexOf("/", 0)));
                int tempindex = temp2 + 1;
                int month     = int.Parse(value.Substring(tempindex, (temp2 = value.IndexOf("/", tempindex)) - tempindex));
                tempindex = temp2 + 1;
                int day = int.Parse(value.Substring(tempindex, value.Length - tempindex));

                return(_persianAssistante.ToDateTime(year, month, day, 0, 0, 0, 0));
            }
            catch (Exception ex)
            {
                Debuging.Error(ex, "To En Date Function");
                return(DateTime.MinValue);
            }
        }
示例#4
0
 public static void DownloadFile(string strURL)
 {
     try
     {
         // string strURL = Server.MapPath(filePath);
         WebClient    req      = new WebClient();
         HttpResponse response = HttpContext.Current.Response;
         response.Clear();
         response.ClearContent();
         response.ClearHeaders();
         response.Buffer = true;
         response.AddHeader("Content-Disposition", "attachment;filename=\"" + strURL + "\"");
         byte[] data = req.DownloadData(strURL);
         response.BinaryWrite(data);
         response.End();
     }
     catch (Exception ex)
     {
         Debuging.Error(ex, "Utility.DownloadFile");
     }
 }
示例#5
0
        public static string EncryptTripleDES(string strToEncrypt, string strKey)
        {
            string encryptData = "";

            try
            {
                TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider       objHashMD5   = new MD5CryptoServiceProvider();
                byte[] byteHash, byteBuff;
                string strTempKey = strKey;
                byteHash          = objHashMD5.ComputeHash(Encoding.UTF8.GetBytes(strTempKey));
                objDESCrypto.Key  = byteHash;
                objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
                byteBuff          = Encoding.UTF8.GetBytes(strToEncrypt);
                encryptData       =
                    Convert.ToBase64String(objDESCrypto.CreateEncryptor()
                                           .TransformFinalBlock(byteBuff, 0, byteBuff.Length));
            }
            catch (Exception ex)
            {
                Debuging.Error(ex, "EncryptTripleDES");
            }
            return(encryptData);
        }