How to send email in ASP.NET 2.0 - SMTP authentication

ASP.NET 2.0 has a built in class, System.Net.Mail, to send email. Although the legacy System.Web.Mail class is still available in ASP.NET 2.0, it is recommended that you use System.Net.Mail class to send mail if you are using ASP.NET 2.0 framework.

C# Code Sample

<%@ Import Namespace="System.Net" %>
 <%@ Import Namespace="System.Net.Mail" %>
  
<script language="C#" runat="server"
    
   protected void Page_Load(object sender, EventArgs e) 
        
            MailMessage mail = new MailMessage(); 
               
            mail.From = new MailAddress ( "postmaster@YourHostedDomain.com" ); 
            mail.Subject = "This is a test message" ; 
            mail.Body = "If you can see this mail, your SMTP service is working" ; 
            mail.To.Add( "recipient@somewhere.com" ); 
            SmtpClient smtp = new SmtpClient( "smtp.YourHostedDomain.com" ); 
    
            NetworkCredential credential = new NetworkCredential ("postmaster@YourHostedDomain.com" , "password"); 
            smtp.Credentials = credential; 
            smtp.Send(mail); 
            Response.Write( "Message was sent to " + mail.To + " at " + DateTime .Now); 
        
</script>
If you run into problems when using this code, please post in our community forum. Technical support is unable to assist with specific coding issues.

How to send email in ASP.NET 2.0 - SMTP authentication

Article ID: 1060, Created On: 7/10/2012, Modified: 7/10/2012