How to send out email through an ASP.Net page with SMTP authentication

Use the ASP.Net built in class, System.Net.Mail, to send email through your ASP.Net page with SMTP authentication.


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 ( "[email protected]" );
            mail.Subject = "This is a test message" ;
            mail.Body = "If you can see this mail, your SMTP service is working" ;
            mail.To.Add( "[email protected]" );
            SmtpClient smtp = new SmtpClient( "mail.YourSiteDomain.com" );
     
            NetworkCredential credential = new NetworkCredential ("[email protected]" , "password");
            smtp.Credentials = credential;
            smtp.Send(mail);
            Response.Write( "Message was sent to " + mail.To + " at " + DateTime .Now);
        }
</script>