public async Task <IHttpActionResult> EmailMediaMonitoringReport(EmailMonitoringReportModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var taskEmail = await repository.EmailMediaMonitoringReport(model, HttpContext.Current); return(Ok(taskEmail)); }
public async Task <string> EmailMediaMonitoringReport(EmailMonitoringReportModel model, HttpContext current) { string result = ""; try { await Task.Run(() => { using (SocialCRMEntities db = new SocialCRMEntities()) { string tvTemplate = System.IO.File.ReadAllText(current.Server.MapPath(@"~/Content/EmailTemplates/KE_1.html")); string printTemplate = System.IO.File.ReadAllText(current.Server.MapPath(@"~/Content/EmailTemplates/KE_2.html")); List <string> emailIds = new List <string>(); emailIds = db.Emails.Where(x => x.EmailRights.Where(y => y.EmailRightName == "Consolidated").Count() > 0).Select(e => e.EmailId).Distinct().ToList <string>(); if (model.isPrint) { string body = printTemplate; body = body.Replace("##TotalMediaRecords##", model.totalMediaRecords.ToString()); body = body.Replace("##TopThreeChannels##", model.top3Areas); body = body.Replace("##BottomThreeChannels##", model.bot3Areas); body = body.Replace("##TotalPRValue##", model.totalPRValue.ToString()); for (int i = 1; i < 4; i++) { string sentiment = "", positive = "", neutral = "", negative = "", total = ""; if (i <= model.totalSentimentData.Count) { sentiment = model.totalSentimentData[i - 1].RelName; positive = model.totalSentimentData[i - 1].Positive.ToString(); neutral = model.totalSentimentData[i - 1].Neutral.ToString(); negative = model.totalSentimentData[i - 1].Negative.ToString(); total = model.totalSentimentData[i - 1].TotalSent.ToString(); } body = body.Replace("##Sentiment" + i + "##", sentiment); body = body.Replace("##Positive" + i + "##", positive); body = body.Replace("##Neutral" + i + "##", neutral); body = body.Replace("##Negative" + i + "##", negative); body = body.Replace("##Total" + i + "##", total); } AlternateView av1 = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html); var bytes = Convert.FromBase64String(model.totalSentimentImg.Split(',')[1]); LinkedResource logoPhoto = new LinkedResource(new MemoryStream(bytes), "image/jpeg"); logoPhoto.ContentId = "shiftReport1"; av1.LinkedResources.Add(logoPhoto); bytes = Convert.FromBase64String(model.keTotalChannelImg.Split(',')[1]); logoPhoto = new LinkedResource(new MemoryStream(bytes), "image/jpeg"); logoPhoto.ContentId = "shiftReport2"; av1.LinkedResources.Add(logoPhoto); bytes = Convert.FromBase64String(model.keSentimentImg.Split(',')[1]); logoPhoto = new LinkedResource(new MemoryStream(bytes), "image/jpeg"); logoPhoto.ContentId = "shiftReport3"; av1.LinkedResources.Add(logoPhoto); bytes = Convert.FromBase64String(model.activitiesImg.Split(',')[1]); logoPhoto = new LinkedResource(new MemoryStream(bytes), "image/jpeg"); logoPhoto.ContentId = "shiftReport4"; av1.LinkedResources.Add(logoPhoto); string subject = "Print Monitoring Report (" + model.date.ToString("dd MMMM yyyy") + ")"; List <Attachment> attachmentList = new List <Attachment>(); EmailSender.SendEmailInThreadWithAlternateView(emailIds, null, subject, body, attachmentList, av1); } else { string body = tvTemplate; body = body.Replace("##TotalMediaRecords##", model.totalMediaRecords.ToString()); body = body.Replace("##TopThreeChannels##", model.top3Areas); body = body.Replace("##BottomThreeChannels##", model.bot3Areas); body = body.Replace("##OnDutyAgents##", model.onDutyAgents); for (int i = 1; i < 4; i++) { string sentiment = "", positive = "", neutral = "", negative = "", total = ""; if (i <= model.totalSentimentData.Count) { sentiment = model.totalSentimentData[i - 1].RelName; positive = model.totalSentimentData[i - 1].Positive.ToString(); neutral = model.totalSentimentData[i - 1].Neutral.ToString(); negative = model.totalSentimentData[i - 1].Negative.ToString(); total = model.totalSentimentData[i - 1].TotalSent.ToString(); } body = body.Replace("##Sentiment" + i + "##", sentiment); body = body.Replace("##Positive" + i + "##", positive); body = body.Replace("##Neutral" + i + "##", neutral); body = body.Replace("##Negative" + i + "##", negative); body = body.Replace("##Total" + i + "##", total); } AlternateView av1 = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html); var bytes = Convert.FromBase64String(model.keEvolutionImg.Split(',')[1]); LinkedResource logoPhoto = new LinkedResource(new MemoryStream(bytes), "image/jpeg"); logoPhoto.ContentId = "shiftReport1"; av1.LinkedResources.Add(logoPhoto); bytes = Convert.FromBase64String(model.totalSentimentImg.Split(',')[1]); logoPhoto = new LinkedResource(new MemoryStream(bytes), "image/jpeg"); logoPhoto.ContentId = "shiftReport2"; av1.LinkedResources.Add(logoPhoto); bytes = Convert.FromBase64String(model.keSentimentImg.Split(',')[1]); logoPhoto = new LinkedResource(new MemoryStream(bytes), "image/jpeg"); logoPhoto.ContentId = "shiftReport3"; av1.LinkedResources.Add(logoPhoto); bytes = Convert.FromBase64String(model.keCategoryImg.Split(',')[1]); logoPhoto = new LinkedResource(new MemoryStream(bytes), "image/jpeg"); logoPhoto.ContentId = "shiftReport4"; av1.LinkedResources.Add(logoPhoto); bytes = Convert.FromBase64String(model.keNIChannelImg.Split(',')[1]); logoPhoto = new LinkedResource(new MemoryStream(bytes), "image/jpeg"); logoPhoto.ContentId = "shiftReport5"; av1.LinkedResources.Add(logoPhoto); bytes = Convert.FromBase64String(model.activitiesImg.Split(',')[1]); logoPhoto = new LinkedResource(new MemoryStream(bytes), "image/jpeg"); logoPhoto.ContentId = "shiftReport6"; av1.LinkedResources.Add(logoPhoto); string subject = "Electronic & Radio Monitoring - " + model.shift + " (" + model.date.ToString("dd MMMM yyyy") + ")"; List <Attachment> attachmentList = new List <Attachment>(); EmailSender.SendEmailInThreadWithAlternateView(emailIds, null, subject, body, attachmentList, av1); } result = "success"; } }); } catch (Exception ex) { throw ex; } return(result); }