What is the code to send an email from an ASP.NET application?

  1. mail message = new mail();
  2. message.From = “abc@gmail.com”;
  3. message.To = “xyz@gmail.com”;
  4. message.Subject = “Test”;
  5. message.Body = “hello”;
  6. SmtpMail.SmtpServer = “localhost”;
  7. SmtpMail.Send(message);

To send an email from an ASP.NET application, you can use the SmtpClient class available in the System.Net.Mail namespace. Here’s a simple example of how you can send an email using ASP.NET:

csharp
using System.Net;
using System.Net.Mail;

public void SendEmail()
{
// Specify the email details
string fromAddress = "your.email@example.com";
string toAddress = "recipient@example.com";
string subject = "Subject of the email";
string body = "Body of the email";

// Create a MailMessage object
MailMessage mailMessage = new MailMessage(fromAddress, toAddress, subject, body);

// Optionally, you can set additional properties such as CC, BCC, etc.
// mailMessage.CC.Add("cc@example.com");
// mailMessage.Bcc.Add("bcc@example.com");

// Create a SmtpClient object and specify the SMTP server details
SmtpClient smtpClient = new SmtpClient("smtp.example.com");
smtpClient.Port = 587; // Specify the SMTP port (e.g., 587 for SMTP with TLS)
smtpClient.Credentials = new NetworkCredential("your.email@example.com", "yourpassword");
smtpClient.EnableSsl = true; // Set to true if you are using SSL/TLS

// Send the email
smtpClient.Send(mailMessage);
}

Replace "your.email@example.com", "recipient@example.com", "smtp.example.com", and "yourpassword" with appropriate values for your email account and SMTP server.

Ensure that you have the necessary permissions and configurations in place to send emails from your ASP.NET application, such as allowing access to SMTP servers in your network or configuring SMTP relay settings if required. Additionally, handle exceptions appropriately for error scenarios like network issues or invalid credentials.