How to send email from Asp.net Mvc-5?

How to send email from Asp.net Mvc-5?

To send an email from an ASP.NET MVC 5 application, you can use the SmtpClient class available in the .NET Framework. Here's an example of sending an email using ASP.NET MVC 5:

  • First, make sure you have the necessary namespaces imported at the top of your code file:
using System.Net; using System.Net.Mail; 
  • Inside your controller action or wherever you want to send the email, create a MailMessage object and set its properties:
public ActionResult SendEmail() { // Sender's email address and display name string senderEmail = "your-email@example.com"; string senderName = "Your Name"; // Recipient's email address string recipientEmail = "recipient-email@example.com"; // Create the MailMessage object MailMessage mailMessage = new MailMessage(senderEmail, recipientEmail); mailMessage.Subject = "Test Email"; mailMessage.Body = "This is a test email."; mailMessage.IsBodyHtml = false; // Set to true if you want to send HTML email mailMessage.From = new MailAddress(senderEmail, senderName); // Create the SmtpClient object and set its properties SmtpClient smtpClient = new SmtpClient(); smtpClient.Host = "smtp.example.com"; // Set the SMTP server address smtpClient.Port = 587; // Set the SMTP port (e.g., 587 for Gmail) smtpClient.EnableSsl = true; // Set to true for SSL/TLS encryption smtpClient.Credentials = new NetworkCredential("your-username", "your-password"); // Send the email smtpClient.Send(mailMessage); return View(); } 
  • Update the email settings to match your SMTP server details. Replace the values in smtpClient.Host, smtpClient.Port, smtpClient.EnableSsl, smtpClient.Credentials, senderEmail, senderName, recipientEmail, "your-username", and "your-password" with the appropriate values for your email provider.

Note: It's recommended to store sensitive information like SMTP credentials in a secure configuration file or using other secure methods.

  • This example assumes you have a corresponding view for the SendEmail action. You can create a simple view to display a message indicating that the email has been sent.

With the above code, when you invoke the SendEmail action, it will create a MailMessage object, configure it with the appropriate details (sender, recipient, subject, body, etc.), create an SmtpClient object, and send the email using the specified SMTP server.

Make sure you have a working internet connection and that your SMTP server settings are correct for the email to be sent successfully.

Examples

  1. "ASP.NET MVC 5 send email with SMTP"

    • Code:
      using (var message = new MailMessage("from@example.com", "to@example.com")) { message.Subject = "Subject"; message.Body = "Hello, this is the email body."; using (var smtpClient = new SmtpClient("smtp.example.com")) { smtpClient.Send(message); } } 
    • Description: Sends a basic email using SMTP in ASP.NET MVC 5.
  2. "ASP.NET MVC 5 send HTML email"

    • Code:
      using (var message = new MailMessage("from@example.com", "to@example.com")) { message.Subject = "Subject"; message.Body = "<p>Hello, this is the HTML email body.</p>"; message.IsBodyHtml = true; using (var smtpClient = new SmtpClient("smtp.example.com")) { smtpClient.Send(message); } } 
    • Description: Sends an HTML email in ASP.NET MVC 5.
  3. "ASP.NET MVC 5 send email with attachment"

    • Code:
      using (var message = new MailMessage("from@example.com", "to@example.com")) { message.Subject = "Subject"; message.Body = "Hello, this is the email body."; // Add attachment message.Attachments.Add(new Attachment("path/to/file.txt")); using (var smtpClient = new SmtpClient("smtp.example.com")) { smtpClient.Send(message); } } 
    • Description: Sends an email with an attachment in ASP.NET MVC 5.
  4. "ASP.NET MVC 5 send email asynchronously"

    • Code:
      using (var message = new MailMessage("from@example.com", "to@example.com")) { message.Subject = "Subject"; message.Body = "Hello, this is the email body."; using (var smtpClient = new SmtpClient("smtp.example.com")) { await smtpClient.SendMailAsync(message); } } 
    • Description: Sends an email asynchronously in ASP.NET MVC 5.
  5. "ASP.NET MVC 5 send email with SMTP credentials"

    • Code:
      using (var message = new MailMessage("from@example.com", "to@example.com")) { message.Subject = "Subject"; message.Body = "Hello, this is the email body."; using (var smtpClient = new SmtpClient("smtp.example.com")) { smtpClient.Credentials = new NetworkCredential("username", "password"); smtpClient.Send(message); } } 
    • Description: Sends an email with SMTP credentials in ASP.NET MVC 5.
  6. "ASP.NET MVC 5 send email with custom sender name"

    • Code:
      using (var message = new MailMessage("from@example.com", "to@example.com")) { message.Subject = "Subject"; message.Body = "Hello, this is the email body."; message.From = new MailAddress("from@example.com", "Custom Sender Name"); using (var smtpClient = new SmtpClient("smtp.example.com")) { smtpClient.Send(message); } } 
    • Description: Sends an email with a custom sender name in ASP.NET MVC 5.
  7. "ASP.NET MVC 5 send email with multiple recipients"

    • Code:
      using (var message = new MailMessage("from@example.com", "recipient1@example.com")) { message.Subject = "Subject"; message.Body = "Hello, this is the email body."; message.To.Add("recipient2@example.com"); using (var smtpClient = new SmtpClient("smtp.example.com")) { smtpClient.Send(message); } } 
    • Description: Sends an email to multiple recipients in ASP.NET MVC 5.
  8. "ASP.NET MVC 5 send email with CC and BCC"

    • Code:
      using (var message = new MailMessage("from@example.com", "to@example.com")) { message.Subject = "Subject"; message.Body = "Hello, this is the email body."; message.CC.Add("cc@example.com"); message.Bcc.Add("bcc@example.com"); using (var smtpClient = new SmtpClient("smtp.example.com")) { smtpClient.Send(message); } } 

More Tags

network-programming git-fetch whitespace dateinterval coalesce phpspreadsheet cllocationmanager generator gnupg ld

More C# Questions

More Mortgage and Real Estate Calculators

More Date and Time Calculators

More Organic chemistry Calculators

More Chemical reactions Calculators