Send Email using ASP.Net using C#

Send Email using ASP.Net using C#

ASP.Net has built in class to send email. You can send email with or without authentication. In our previous post we provided sample script to send email from ASP.Net using VB.Net code.

In this post, we will provide you sample code to send email from ASP.Net using C# code. Following is the sample code to send mail using ASP.Net (C# code):

<%@ Import Namespace="System.Net" %> 
<%@ Import Namespace="System.Net.Mail" %> 
 
<script language="C#" runat="server"> 
    protected void Page_Load(object sender, EventArgs e) 
    { 
       //create the mail message 
        MailMessage mail = new MailMessage(); 
 
        //set the addresses 
        mail.From = new MailAddress("[email protected]"); 
        mail.To.Add("[email protected]"); 
        
        //set the content 
        mail.Subject = "This is a test email from C# script"; 
        mail.Body = "This is a test email from C# script"; 
        //send the message 
         SmtpClient smtp = new SmtpClient("smtp.yourdomain.com"); 
          
         NetworkCredential Credentials = new NetworkCredential("[email protected]", "password"); 
         smtp.Credentials = Credentials;
         smtp.Send(mail); 
         lblMessage.Text = "Mail Sent"; 
    } 
</script> 
<html> 
<body> 
    <form runat="server"> 
        <asp:Label id="lblMessage" runat="server"></asp:Label> 
    </form> 
</body>

Make sure to make necessary changes in your script (i.e. SMTP server, email address, password etc.)

Leave a Reply