public ActionResult GeneratePdf(int id) { string pdfName = Guid.NewGuid().ToString(); Customer_Device_ServicesDTO dto = new Customer_Device_ServicesDTO(); using (MOBILESHOPEntities dbcontext = new MOBILESHOPEntities()) { var cstmr = dbcontext.mbshop_device_detail.Find(id); if (cstmr != null) { dto.RefNo = cstmr.device_key.ToString(); dto.customerName = cstmr.mbshop_customer_details.customer_name; dto.Address = cstmr.mbshop_customer_details.customer_address; dto.Email = cstmr.mbshop_customer_details.customer_email; dto.PhonNumber = cstmr.mbshop_customer_details.customer_mobile_number; dto.Imei_1 = cstmr.device_imei_number_1; dto.Imei_2 = cstmr.device_imei_number_2; dto.Brand = cstmr.mb_model_detail.mb_brand_detail.brand_name; dto.Model = cstmr.mb_model_detail.model_name; dto.Fault = cstmr.mb_fault_detail.fault_name; dto.SubmittDate = cstmr.device_date_submitt.ToString("MM/dd/yyyy h:mm tt"); dto.RepairingCost = cstmr.device_repairing_cost; dto.DeliverDate = Convert.ToDateTime(cstmr.device_deliver_date).ToString("MM/dd/yyyy h:mm tt"); dto.CustomerSignature = cstmr.device_customer_signature != null ? cstmr.device_customer_signature : "~/images/300px-No_image_available.svg (1).png"; } int i = 1; dto.services = dbcontext.Costumer_Device_Services.Where(x => x.cds_device_key == id).AsEnumerable().Select(x => new ServiceDTO { id = (i++), serviceName = x.mbshop_service_detail.service_name, serviceCharges = x.mbshop_service_detail.service_charges }).ToList(); }; return(new ViewAsPdf("CustomerBillPdf", dto) { FileName = pdfName + ".pdf" }); }
public ActionResult EmailBill(int id) { try { string pdfName = Guid.NewGuid().ToString() + ".pdf"; Customer_Device_ServicesDTO dto = new Customer_Device_ServicesDTO(); using (MOBILESHOPEntities dbcontext = new MOBILESHOPEntities()) { var cstmr = dbcontext.mbshop_device_detail.Find(id); if (cstmr != null) { dto.RefNo = cstmr.device_key.ToString(); dto.customerName = cstmr.mbshop_customer_details.customer_name; dto.Address = cstmr.mbshop_customer_details.customer_address; dto.Email = cstmr.mbshop_customer_details.customer_email; dto.PhonNumber = cstmr.mbshop_customer_details.customer_mobile_number; dto.Imei_1 = cstmr.device_imei_number_1; dto.Imei_2 = cstmr.device_imei_number_2; dto.Brand = cstmr.mb_model_detail.mb_brand_detail.brand_name; dto.Model = cstmr.mb_model_detail.model_name; dto.Fault = cstmr.mb_fault_detail.fault_name; dto.SubmittDate = cstmr.device_date_submitt.ToString("MM/dd/yyyy h:mm tt"); dto.RepairingCost = cstmr.device_repairing_cost; dto.DeliverDate = Convert.ToDateTime(cstmr.device_deliver_date).ToString("MM/dd/yyyy h:mm tt"); dto.CustomerSignature = cstmr.device_customer_signature != null ? cstmr.device_customer_signature : "~/images/300px-No_image_available.svg (1).png"; } int i = 1; dto.services = dbcontext.Costumer_Device_Services.Where(x => x.cds_device_key == id).AsEnumerable().Select(x => new ServiceDTO { id = (i++), serviceName = x.mbshop_service_detail.service_name, serviceCharges = x.mbshop_service_detail.service_charges }).ToList(); }; string virtualpath = "~/PdfBills/" + pdfName; var root = Server.MapPath("~/PdfBills/"); var path = Path.Combine(root, pdfName); path = Path.GetFullPath(path); var pdfPath = new ViewAsPdf("CustomerBillPdf", dto) { FileName = pdfName, }; var byteArray = pdfPath.BuildPdf(ControllerContext); Stream stream = new MemoryStream(byteArray); System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Html); System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(stream, ct); attach.ContentDisposition.FileName = pdfName; //send Email SmtpClient client = new SmtpClient(); MailMessage email = new MailMessage(); email.From = new MailAddress("*****@*****.**"); email.To.Add(dto.Email); email.Subject = "Bill from Bariq Mobile"; email.Body = ""; email.Attachments.Add(attach); client.Send(email); return(Json(new { key = true, value = "Email sent Successfully" }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { key = true, value = "Email sending Failed - Please try again" }, JsonRequestBehavior.AllowGet)); } }