Using MVC C#
- I want that when the user submits the form, a message should be sent to the email he entered in the email textbox.
- I want send some data using html
- I want send html data in pdf format
please give some hints
Thank You.....
Using MVC C#
please give some hints
Thank You.....
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Abhishek ChadhaRank #402 · 6.2K reputationPosted May 22, 2024, 1:22 AM EDT
Here's a basic example illustrating these steps:
// Controller action method to handle form submission [HttpPost] public ActionResult SubmitForm(FormViewModel model) { // Send email to the entered email address string emailAddress = model.Email; // Assuming 'Email' is a property in your view model string emailSubject = "Form Submission Confirmation"; string emailBody = "Thank you for submitting the form. Here is the data you provided: " + "Name: " + model.Name + " " + "Message: " + model.Message; // Assuming 'Name' and 'Message' are properties in your view model // Send email SendEmail(emailAddress, emailSubject, emailBody); // Generate PDF document string pdfFilePath = GeneratePdfFromHtml(emailBody); // Other logic... return View("Confirmation"); } // Method to send email using System.Net.Mail private void SendEmail(string to, string subject, string body) { // Configure your email settings (SMTP server, credentials, etc.) using (MailMessage message = new MailMessage("[email protected]", to)) { message.Subject = subject; message.Body = body; message.IsBodyHtml = true; using (SmtpClient client = new SmtpClient("smtp.yourprovider.com")) { client.Send(message); } } } // Method to generate PDF from HTML using iTextSharp private string GeneratePdfFromHtml(string html) { string pdfFilePath = "path/to/save/pdf.pdf"; // Create a PDF document using (FileStream fs = new FileStream(pdfFilePath, FileMode.Create)) { using (Document document = new Document()) { PdfWriter writer = PdfWriter.GetInstance(document, fs); document.Open(); // Read HTML content using (TextReader reader = new StringReader(html)) { // Parse HTML to PDF XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader); } } } return pdfFilePath; }Remember to replace placeholders like
"[email protected]","smtp.yourprovider.com", and"path/to/save/pdf.pdf"with your actual email settings and desired file path.