private async Task <byte[]> CreateInvoicePdfStream(int id) { var invoiceDetails = await _service.GetInvoice(User, id); if (invoiceDetails != null) { invoiceDetails.CabinReservation = await _service.GetCabinReservation(User, invoiceDetails.CabinReservationId); } var globalSettings = new GlobalSettings { Orientation = Orientation.Portrait, PaperSize = PaperKind.A4, Margins = new MarginSettings { Top = 30, Bottom = 30, Left = 20, Right = 20 }, DocumentTitle = "Lasku " + id }; var objectSettings = new ObjectSettings { PagesCount = true, HtmlContent = InvoicePdfGenerator.GetHTMLString(invoiceDetails), WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "css", "pdf.css") } }; var pdf = new HtmlToPdfDocument { GlobalSettings = globalSettings, Objects = { objectSettings } }; return(_converter.Convert(pdf)); }
//---------------------------------------------------------------------------------------------------- Send email public async Task <IActionResult> SendMail(int id) { var invoiceDetails = await _service.GetInvoice(User, id); if (invoiceDetails != null) { invoiceDetails.CabinReservation = await _service.GetCabinReservation(User, invoiceDetails.CabinReservationId); } var globalSettings = new GlobalSettings { Orientation = Orientation.Portrait, PaperSize = PaperKind.A4, Margins = new MarginSettings { Top = 30, Bottom = 30, Left = 20, Right = 20 }, DocumentTitle = "Lasku " + id }; var objectSettings = new ObjectSettings { PagesCount = true, HtmlContent = InvoicePdfGenerator.GetHTMLString(invoiceDetails), WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "css", "pdf.css") } }; var pdf = new HtmlToPdfDocument { GlobalSettings = globalSettings, Objects = { objectSettings } }; byte[] file = _converter.Convert(pdf); string filename = "lasku.pdf"; Attachment attachment = new Attachment(new MemoryStream(file), filename); try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(_configuration.GetValue <string>("Smtp:Server")); mail.From = new MailAddress(_configuration.GetValue <string>("Smtp:FromAddress")); mail.To.Add(invoiceDetails.CabinReservation.Person.Email); mail.Subject = "Lasku"; mail.Body = "Hei, tässä on laskusi."; mail.Attachments.Add(attachment); SmtpServer.Port = _configuration.GetValue <int>("Smtp:Port"); SmtpServer.Credentials = new NetworkCredential(_configuration.GetValue <string>("Smtp:FromAddress"), _configuration.GetValue <string>("Smtp:Password")); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); return(View()); } catch { return(BadRequest("Sähköpostin lähettäminen epäonnistui")); } }