Send Email from ASP.NET using System.Net.Mail
The System.Net.Mail namespace provides a modern way to send email from ASP.NET applications.
Unlike CDOSYS, this library is actively supported and recommended for secure, reliable email delivery.
Sample Code
using System;
using System.Net;
using System.Net.Mail;
public partial class SendMail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("me@mydomain.com");
mail.To.Add("recipient@test.com");
mail.Subject = "Example System.Net.Mail Message";
mail.Body = "This is a sample message sent using System.Net.Mail.";
SmtpClient smtp = new SmtpClient("mail.mydomain.com", 587);
smtp.Credentials = new NetworkCredential("me@mydomain.com", "yourpassword");
smtp.EnableSsl = true; // Recommended for secure connections
smtp.Send(mail);
}
}
Configuration Notes
- SMTP Server: Replace
mail.mydomain.comwith your actual mail server hostname. - Port: Use
587for STARTTLS or465for SSL. Avoid port 25 if blocked by your ISP. - Credentials: Use your full email address and password.
- EnableSsl: Always set to
trueif your server supports SSL/TLS for secure transmission.
Advantages over CDOSYS
- Actively supported in .NET Framework and .NET Core.
- Provides built-in support for SSL/TLS encryption.
- More secure and reliable than legacy CDOSYS.
- Better error handling and integration with modern ASP.NET applications.
✔ Tip: Always test your configuration by sending a sample email.
If you encounter issues, verify your SMTP hostname, port, and SSL settings.