There are a few ways to send email from an ASP.NET application. One way is to use the built-in MailMessage class. To do this, you will need to create a MailMessage object and set the following properties:
From: The sender's email address.
To: The recipient's email address(es).
Subject: The subject line of the email.
Body: The body of the email.
Once you have set the properties of the MailMessage object, you can use the SmtpClient class to send the email. To do this, you will need to create a SmtpClient object and set the following properties:
Host: The host name of the SMTP server.
Port: The port number of the SMTP server.
Credentials: The credentials for the SMTP server.
Once you have created the SmtpClient object, you can use the Send() method to send the email.
Here is an example of how to send email using the MailMessage and SmtpClient classes:
C#
using System;
using System.Net;
using System.Net.Mail;
public class Program
{
public static void Main(string[] args)
{
// Create a MailMessage object.
MailMessage message = new MailMessage();
// Set the sender's email address.
message.From = "sender@example.com";
// Set the recipient's email address.
message.To = "recipient@example.com";
// Set the subject line of the email.
message.Subject = "This is a test email";
// Set the body of the email.
message.Body = "This is the body of the email.";
// Create a SmtpClient object.
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
// Set the credentials for the SMTP server.
client.Credentials = new NetworkCredential("username", "password");
// Send the email.
client.Send(message);
}
}
Another way to send email from an ASP.NET application is to use a third-party email library. There are many third-party email libraries available, such as MailKit and SendGrid. These libraries provide a more robust set of features than the built-in MailMessage class.
To use a third-party email library, you will need to add the library to your ASP.NET project. Once you have added the library, you can use the library's API to send email.
Here is an example of how to send email using the MailKit library:
C#
using MailKit;
using MailKit.Net.Smtp;
public class Program
{
public static void Main(string[] args)
{
// Create a MailKit client.
MailKitClient client = new MailKitClient();
// Set the sender's email address.
client.Credentials = new NetworkCredential("username", "password");
// Set the recipient's email address.
client.To = "recipient@example.com";
// Set the subject line of the email.
client.Subject = "This is a test email";
// Set the body of the email.
client.Body = "This is the body of the email.";
// Send the email.
client.Send();
}
}
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
There are a few ways to send email from an ASP.NET application. One way is to use the built-in MailMessage class. To do this, you will need to create a MailMessage object and set the following properties:
Once you have set the properties of the MailMessage object, you can use the SmtpClient class to send the email. To do this, you will need to create a SmtpClient object and set the following properties:
Once you have created the SmtpClient object, you can use the Send() method to send the email.
Here is an example of how to send email using the MailMessage and SmtpClient classes:
C#
Another way to send email from an ASP.NET application is to use a third-party email library. There are many third-party email libraries available, such as MailKit and SendGrid. These libraries provide a more robust set of features than the built-in MailMessage class.
To use a third-party email library, you will need to add the library to your ASP.NET project. Once you have added the library, you can use the library's API to send email.
Here is an example of how to send email using the MailKit library:
C#