public object PostSendEmail([FromBody] object param) { int report_id = 0; short?interval_minute = 0; JObject paramdata = JObject.Parse(param.ToString()); if (!string.IsNullOrEmpty(paramdata["report_id"].ToString())) { report_id = Convert.ToInt32(((JValue)paramdata["report_id"]).Value); } if (!string.IsNullOrEmpty(paramdata["interval"].ToString())) { interval_minute = Convert.ToInt16(((JValue)paramdata["interval"]).Value); } report_ref item = db.Reports.Find(report_id); if (item == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "Report does not exist")); } DataSet ds; try { ds = GetReportData(item, interval_minute); } catch (Exception ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, "Error occured while exeuting report SQL")); } string[] strCSVOutput; try { CommonFunctions commfun = new CommonFunctions(); strCSVOutput = commfun.GetCSVOutput(ds); } catch (Exception ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, "Error occured while generating CSV file")); } try { MemoryStream[] ms = new MemoryStream[ds.Tables.Count]; CommonEmail email = new CommonEmail(); email.ConfigureDefaultSMTPclient(); Attachment[] attachments = new Attachment[ds.Tables.Count]; for (int i = 0; i < strCSVOutput.Length; i++) { ms[i] = new MemoryStream(Encoding.UTF8.GetBytes(strCSVOutput[i])); attachments[i] = new Attachment(ms[i], "Report" + i.ToString() + ".csv", "text/csv"); } string emailSubject = "eTracker Report : " + item.report_nm + " executed on : " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString(); StringBuilder sbEmailbody = new StringBuilder(); sbEmailbody.AppendLine("This report is generated from eTracker Reporting Tool"); sbEmailbody.AppendLine("Report Name : " + item.report_nm); sbEmailbody.AppendLine("Description : " + item.report_descr); if (attachments != null && attachments.Count() > 0) { sbEmailbody.AppendLine("Report output is attached (CSV format)"); } email.SendEmail(item.scheduler_email_ids, emailSubject, sbEmailbody.ToString(), attachments); ms = null; } catch (Exception ex) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, "Error occured while sending email")); } return("Successfully email sent"); }