public static string ParseData(string data, object obj, bool compileRazor = true) { //return data; if (obj == null) { return(""); } data = data.Trim(); if (data.StartsWith("@") && data.EndsWith("}")) { data = RunOrCompileRazorCode(data, data, obj, compileRazor); } var matches = Regex.Matches(data, openToken + "[a-zA-Z0-9-.&=<>/\\;(\n|\r|\r\n)\"#?']+" + closeToken); foreach (var item in matches) { var tag = item.ToString(); var tagValue = ""; var propertyName = tag.Replace("{", "").Replace("}", ""); var queryStringParams = ""; if (propertyName.Contains("?")) { var segments = propertyName.Split('?').ToList(); propertyName = segments[0]; segments.RemoveAt(0); queryStringParams = string.Join("", segments); } var nestedProperties = StringHelper.SplitByString(propertyName, "."); if (nestedProperties.Length > 0) { if (obj == null) { continue; } object tempNestedProperty = obj; var propertyloopIndex = 0; foreach (var nestedProperty in nestedProperties) { PropertyInfo tempPropertyInfo = null; MethodInfo tempMethodInfo = null; var matchParams = Regex.Matches(nestedProperty, "([a-zA-Z0-9-_]+)"); var methodParamsMatches = new List <string>(); for (int i = 0; i < matchParams.Count; i++) { var val = matchParams[i].ToString(); if (val != "quot") { methodParamsMatches.Add(val); } } if (nestedProperty.Contains("(") && !nestedProperty.Contains(".")) { try { tempMethodInfo = tempNestedProperty.GetType().GetMethod(methodParamsMatches[0]); } catch (Exception ex) { ErrorHelper.LogException(ex); } } else { var prop = nestedProperty; var queryParamsSplit = nestedProperty.Split('?'); if (queryParamsSplit.Count() > 1) { prop = queryParamsSplit.ElementAt(0); } if (tempNestedProperty is JObject) { var val = (tempNestedProperty as JObject).GetValue(prop); if (val is JArray) { tempNestedProperty = String.Join(",", val as JArray); } else { if (val != null) { tempNestedProperty = val.ToString(); } } } else { tempPropertyInfo = tempNestedProperty.GetType().GetProperty(prop); } } if (tempPropertyInfo != null || tempMethodInfo != null) { if (tempPropertyInfo != null) { tempNestedProperty = tempPropertyInfo.GetValue(tempNestedProperty, null); } else if (tempMethodInfo != null) { var objParams = new object[methodParamsMatches.Count - 1]; var parametersInfo = tempMethodInfo.GetParameters(); for (var i = 0; i < methodParamsMatches.Count - 1; i++) { if (parametersInfo.Count() > i) { objParams[i] = Convert.ChangeType(methodParamsMatches[i + 1], parametersInfo[i].ParameterType); } } tempNestedProperty = tempMethodInfo.Invoke(tempNestedProperty, objParams.Where(i => i != null)?.ToArray()); } if (tempNestedProperty != null) { var hasEnumerable = tempNestedProperty.GetType().ToString().Contains("Enumerable") || tempNestedProperty.GetType().ToString().Contains("Collection"); long tmpIndex = 0; if (nestedProperties.Count() > propertyloopIndex + 1) { if (hasEnumerable) { if (long.TryParse(nestedProperties[propertyloopIndex + 1], out tmpIndex)) { var count = 0; foreach (var nestedItem in tempNestedProperty as IEnumerable <object> ) { if (count == tmpIndex) { tempNestedProperty = nestedItem; break; } count++; } continue; } else { var count = 0; var returnValue = ""; var tempPropertiesString = ""; var tmp = nestedProperties.ToList(); tmp.RemoveAt(propertyloopIndex); var newPropertyString = string.Join(".", tmp); foreach (var nestedItem in (dynamic)tempNestedProperty) { var tmpReturn = ParseData("{" + newPropertyString + "}", nestedItem); returnValue += ParseData(tmpReturn, nestedItem); /*var tmpReturn = ParseData("{" + nestedProperties[propertyloopIndex + 1] + "}", nestedItem); * returnValue += ParseData(tmpReturn, nestedItem); * count++;*/ } tagValue = returnValue; } } } else { if (nestedProperties.Length < propertyloopIndex + 1) { return(ParseData("{" + nestedProperties[propertyloopIndex + 1] + "}", tempNestedProperty)); } else if (tempNestedProperty is string) { if (tempMethodInfo != null) { tagValue = tempNestedProperty.ToString(); } //return ParseData("{" + nestedProperties[propertyloopIndex + 1] + "}", tempNestedProperty); } } } } else { var splitEq = nestedProperty.ToString().Split('='); if (splitEq.Count() > 1) { tempPropertyInfo = tempNestedProperty.GetType().GetProperty(splitEq[0]); if (tempPropertyInfo != null) { var returnVal = tempPropertyInfo.GetValue(tempNestedProperty, null); if (splitEq[1].Replace("\"", "") == returnVal.ToString()) { var tmp = nestedProperties.ToList(); tmp.RemoveAt(propertyloopIndex); var newPropertyString = string.Join(".", tmp); tempNestedProperty = ParseData("{" + newPropertyString + "}", tempNestedProperty); } else { tempNestedProperty = ""; } } } } propertyloopIndex++; } if (tempNestedProperty is DateTime) { tagValue = data.Replace(item.ToString(), StringHelper.FormatOnlyDate((DateTime)tempNestedProperty)); } if (tempNestedProperty is string || tempNestedProperty is bool || tempNestedProperty is long) { var val = tempNestedProperty.ToString(); var queryStringSplit = item.ToString().Replace(OpenToken, "").Replace(CloseToken, "").Split('?'); if (queryStringSplit.Count() > 1) { var nv = HttpUtility.ParseQueryString(queryStringSplit.ElementAt(1)); foreach (string key in nv) { var value = nv[key]; val = val.Replace(OpenToken + key + CloseToken, value); } } tagValue = data.Replace(item.ToString(), val); } } if (tagValue.StartsWith("~/")) { tagValue = URIHelper.ConvertToAbsUrl(tagValue); } if (!string.IsNullOrEmpty(tagValue)) { data = RunOrCompileRazorCode(tag, tagValue, obj, compileRazor); } } data = RunOrCompileRazorCode(data, data, obj, compileRazor); return(data); }
private static Return SendDirectMessage(string fromEmailAddress, IEnumerable <MailAddress> emailAddresses, string subject, string body, bool bcc = true) { var returnObj = new Return(); var emailLog = new EmailLog(); Message message = new Message(); message.Subject = subject; message.From = new Address(fromEmailAddress); foreach (var mailAddress in emailAddresses) { if (bcc) { message.Bcc.Add(mailAddress.Address); } else { message.To.Add(mailAddress.Address); } } message.IsHtml = true; message.BodyHtml.Text = body; message.BodyText.Text = body; emailLog.FromEmailAddress = message.From.Email; emailLog.SenderName = message.Sender.Name; emailLog.SenderEmailAddress = message.Sender.Email; emailLog.Subject = message.Subject; emailLog.ToEmailAddresses = ""; emailLog.VisitorIP = ""; emailLog.RequestUrl = ""; emailLog.ServerMessage = ""; emailLog.Message = body; try { var mailMessage = new MailMessage(); mailMessage.CopyFrom(message); var returnStr = ActiveUp.Net.Mail.SmtpClient.DirectSend(message); if (!string.IsNullOrEmpty(returnStr)) { emailLog.ServerMessage = returnObj?.Error?.Message; if (emailLog.ServerMessage == null) { emailLog.ServerMessage = ""; } EmailsMapper.Insert(emailLog); } return(returnObj); } catch (Exception ex) { ErrorHelper.LogException(ex); returnObj.Error = ErrorHelper.CreateError(ex); emailLog.ServerMessage = returnObj?.Error?.Message; if (emailLog.ServerMessage == null) { emailLog.ServerMessage = ""; } EmailsMapper.Insert(emailLog); return(returnObj); } }
public static Return Send(string fromEmailAddress, IEnumerable <MailAddress> emailAddresses, string subject, string body, EmailMode emailMode = EmailMode.Both, bool bcc = true) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; Return returnObj = new Return(); var emailLog = new EmailLog(); if (emailMode == EmailMode.Both || emailMode == EmailMode.Smtp) { try { MailMessage message = new MailMessage(); foreach (MailAddress address in emailAddresses) { if (bcc) { message.Bcc.Add(address); } else { message.To.Add(address); } } message.Sender = new MailAddress(fromEmailAddress); message.IsBodyHtml = true; message.Subject = subject; message.Body = body; emailLog = GetEmailLogFromMailMessage(message); System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); client.Send(message); emailLog.ServerMessage = "Successfully sent email"; EmailsMapper.Insert(emailLog); return(returnObj); } catch (Exception ex) { ErrorHelper.LogException(ex); returnObj.Error = ErrorHelper.CreateError(ex); emailLog.ServerMessage = returnObj.Error.Message; EmailsMapper.Insert(emailLog); if (emailMode == EmailMode.Both) { var directSentReturn = SendDirectMessage(fromEmailAddress, emailAddresses, subject, body, bcc); if (directSentReturn.IsError) { return(directSentReturn); } else { returnObj = BaseMapper.GenerateReturn(); } } return(returnObj); } } else { return(SendDirectMessage(fromEmailAddress, emailAddresses, subject, body)); } }
public string MakeWebRequest(string urlString, RequestMethod method = RequestMethod.GET, WebHeaderCollection headerCollection = null, string queryString = "") { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; var storageItem = WebserviceRequestsMapper.GetByUrl(urlString); var data = ""; if ((storageItem != null) && (EnableCaching) && !storageItem.Response.Contains("unavailable") && storageItem.DateLastModified.AddSeconds(CacheDurationInSeconds) > DateTime.Now) { return(storageItem.Response); } WebserviceRequest request = null; if (storageItem == null) { request = new WebserviceRequest(); } else { request = storageItem; } request.Url = urlString; request.QueryString = queryString; request.Response = ""; request.Method = method.ToString(); request.UrlReferrer = ""; if (System.Web.HttpContext.Current.Request.UrlReferrer != null) { request.UrlReferrer = System.Web.HttpContext.Current.Request.UrlReferrer.AbsoluteUri; } WebRequest webRequest = WebRequest.Create(urlString); if (headerCollection != null) { webRequest.Headers = headerCollection; } webRequest.Method = method.ToString(); try { if (method == RequestMethod.POST && !string.IsNullOrEmpty(queryString)) { var encoding = new ASCIIEncoding(); byte[] bytes = encoding.GetBytes(queryString); webRequest.ContentLength = bytes.Length; webRequest.ContentType = "application/x-www-form-urlencoded"; using (var webRequestStream = webRequest.GetRequestStream()) { webRequestStream.Write(bytes, 0, bytes.Length); webRequestStream.Flush(); webRequestStream.Close(); } } using (WebResponse webResponse = webRequest.GetResponse()) { Stream dataStream = webResponse.GetResponseStream(); using (StreamReader streamReader = new StreamReader(dataStream)) { data = streamReader.ReadToEnd(); } } } catch (WebException ex) { ErrorHelper.LogException(ex); data = ex.Message; if (ex.Response != null) { // can use ex.Response.Status, .StatusDescription if (ex.Response.ContentLength != 0) { using (var stream = ex.Response.GetResponseStream()) { using (var reader = new StreamReader(stream)) { data = reader.ReadToEnd(); } } } } } if (request != null) { request.Response = data; var ommit = false; foreach (var item in OmmitList) { if (request.Url.ToLower().Contains(item)) { ommit = true; break; } } if (EnableCaching) { if (request.ID == 0) { WebserviceRequestsMapper.Insert(request); } else { request = BaseMapper.GetObjectFromContext(request); WebserviceRequestsMapper.Update(request); } } } return(data); }