private void SendNotificationToManager(YourReportFormModel model, string managerEmail) { var subject = string.Format("New {0} form by {1} {2}", model.ReportType, model.RefereeType, model.RefereeName); var message = string.Format("Please find {0} form by {1} {2} in the attachment", model.ReportType, model.RefereeType, model.RefereeName); //MailMessage is Disposable ==> USE "using"! using (var email = new MailMessage { Subject = subject, From = new MailAddress(model.RefereeEmail), Body = message, IsBodyHtml = true }) { var developerEmail = ConfigurationManager.AppSettings["DeveloperEmail"]; email.To.Add(string.IsNullOrWhiteSpace(managerEmail) ? developerEmail : managerEmail); var documentFileName = CreateWordDoc(model); //Attachment is Disposable ==> USE "using"! using (Attachment refsReportDocx = new Attachment(documentFileName)) { email.Attachments.Add(refsReportDocx); var isSendCopyToDev = bool.Parse(ConfigurationManager.AppSettings["SendEmailCopyToDev"]); if (isSendCopyToDev) { email.Bcc.Add(developerEmail); } SmtpClient smtp = new SmtpClient(); smtp.Send(email); } // at this point Attachment has been disposed - so we can delete the cached file safely System.IO.File.Delete(documentFileName); } }
private string CreateWordDoc(YourReportFormModel model) { //Try to find the path "~YOURPATH" if (!Directory.Exists(Server.MapPath("~/YOURPATH"))) { //If it's not found --> create one Directory.CreateDirectory(Server.MapPath("~/YOURPATH")); } string docName = string.Format("{0} form by {1} {2}", model.ReportType, model.RefereeType, model.RefereeName); string fileName = Server.MapPath("~/YOURPATH/" + docName + ".docx"); //Create docx file var doc = DocX.Create(fileName); //Add header image var image = doc.AddImage(Server.MapPath("~/YOURPATH/YourHeaderImage.jpg")); var logo = image.CreatePicture(60, 120); doc.AddHeaders(); doc.Headers.Odd.InsertParagraph().AppendPicture(logo).SpacingBefore(0).Alignment = Alignment.right; //Start building word doc doc.InsertParagraph("YOUR REPORT TITLE").FontSize(12).Font("Trebuchet MS").Alignment = Alignment.center; var p = doc.InsertParagraph(" "); //Create table 1 var tb1 = doc.AddTable(7, 2); tb1.Alignment = Alignment.center; tb1.Rows[0].Cells[0].Paragraphs.First().Append("Home Team: " + model.HomeTeam).FontSize(10).Font("Trebuchet MS").Alignment = Alignment.left; tb1.Rows[0].Cells[1].Paragraphs.First().Append("Visitor: " + model.Visitors).FontSize(10).Font("Trebuchet MS").Alignment = Alignment.left; tb1.Rows[2].Cells[0].Paragraphs.First().Append("Venue: " + model.Venue).FontSize(10).Font("Trebuchet MS").Alignment = Alignment.left; tb1.Rows[2].Cells[1].Paragraphs.First().Append("Date: " + model.FixtureDate).FontSize(10).Font("Trebuchet MS").Alignment = Alignment.left; tb1.Rows[4].MergeCells(0, 1); tb1.Rows[4].Paragraphs.First().Append("Period of the match when called off: " + model.Period).FontSize(10).Font("Trebuchet MS").Alignment = Alignment.left; tb1.Rows[6].MergeCells(0, 1); tb1.Rows[6].Paragraphs.First().Append("Elapsed time in half: " + model.TimeElapsed).FontSize(10).Font("Trebuchet MS").Alignment = Alignment.left; //Configure the table 1 tb1.SetWidths(new float[] { 200, 300, 100 }); var blankBorder = new Border(Xceed.Words.NET.BorderStyle.Tcbs_none, 0, 0, Color.White); tb1.SetBorder(TableBorderType.Bottom, blankBorder); tb1.SetBorder(TableBorderType.Top, blankBorder); tb1.SetBorder(TableBorderType.Left, blankBorder); tb1.SetBorder(TableBorderType.Right, blankBorder); tb1.SetBorder(TableBorderType.InsideH, blankBorder); tb1.SetBorder(TableBorderType.InsideV, blankBorder); doc.InsertTable(tb1); doc.InsertParagraph(" "); //Create table 2 var tb2 = doc.AddTable(3, 2); tb2.Alignment = Alignment.center; tb2.Rows[0].Cells[0].Paragraphs.First().Append("REFEREE’S NAME: " + model.RefereeName).FontSize(10).Font("Trebuchet MS").Alignment = Alignment.left; tb2.Rows[0].Cells[1].Paragraphs.First().Append("UNION: " + model.RefereeUnion).FontSize(10).Font("Trebuchet MS").Alignment = Alignment.left; tb2.Rows[2].MergeCells(0, 1); tb2.Rows[2].Paragraphs.First().Append("CONTACT PHONE: " + model.RefereePhone).FontSize(10).Font("Trebuchet MS").Alignment = Alignment.left; //Configure table 2 tb2.SetWidths(new float[] { 200, 300, 100 }); tb2.SetBorder(TableBorderType.Bottom, blankBorder); tb2.SetBorder(TableBorderType.Top, blankBorder); tb2.SetBorder(TableBorderType.Left, blankBorder); tb2.SetBorder(TableBorderType.Right, blankBorder); tb2.SetBorder(TableBorderType.InsideH, blankBorder); tb2.SetBorder(TableBorderType.InsideV, blankBorder); doc.InsertTable(tb2); doc.InsertParagraph(" "); doc.InsertParagraph("What were the circumstances in which the match was called off? ").FontSize(10).Font("Trebuchet MS").Alignment = Alignment.left; doc.InsertParagraph(model.Circumstances).FontSize(8).Font("Trebuchet MS").Alignment = Alignment.left; doc.InsertParagraph(" "); doc.InsertParagraph("What were the examples of the persistent or serious Foul Play or Misconduct that led to the match being called off and who committed these offences? ").FontSize(10).Font("Trebuchet MS").Alignment = Alignment.left; doc.InsertParagraph(model.Misconduct).FontSize(8).Font("Trebuchet MS").Alignment = Alignment.left; doc.InsertParagraph(" "); doc.InsertParagraph("Were one or both teams responsible for the match being called off (give details)? ").FontSize(10).Font("Trebuchet MS").Alignment = Alignment.left; doc.InsertParagraph(model.Details).FontSize(8).Font("Trebuchet MS").Alignment = Alignment.left; doc.InsertParagraph(" "); doc.InsertParagraph("REPORT TO BE LODGED WITH THE PROVINCIAL UNION WHERE THE MATCH WAS PLAYED WITHIN 48 HOURS OF THE MATCH").FontSize(12).Font("Trebuchet MS").Alignment = Alignment.center; //Save the document doc.Save(); return(fileName); }