Send Email From ASP.NET Using System.Net.Mail

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.com with your actual mail server hostname.
  • Port: Use 587 for STARTTLS or 465 for SSL. Avoid port 25 if blocked by your ISP.
  • Credentials: Use your full email address and password.
  • EnableSsl: Always set to true if 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.
Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 7

Need more information or have a question ?