How to send email in ASP.NET

Programming, error messages and sample code > sample code
NOTICE:  Trial account does not have SMTP service. Therefore, you cannot test this in your trial account. We do this to prevent spammer from taking advantage of our Free Trial Service.

You should use the smtp server assigned to your account "mail.yourdomain.com" as the SMTP outgoing server for your web applications(Replace yourdomain.com to your own domain name). 

To get your SMTP server setup, please follow the below:
1) Login to your control panel
2) Make sure a domain name is added to your account by going to Hosting Control Panel -> Website Domain Manager
3) Once the domain name is added to your account, go to Hosting Control Panel -> Email Manager and activate your email service for your domain name.
4) Create the necessary email account that you need.  Your script MUST send from an valid Email account. So create the ones you need.
5) Done! Please follow below's code sample to send emails.

YOU MUST USE SMTP AUTHENTICATION


ASP.NET 2.0 (and later) has a built in class, System.Net.Mail, to send email.

VB.NET Code Sample
 
<%@ Page Language="VB" %> 
<%@ Import Namespace="System.Net.Mail" %> 
<script runat="server"
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)
 
        Dim strFrom = "postmaster@yourdomain.com"  ''IMPORTANT: This must be same as your smtp authentication address.
        Dim strTo = "postmaster@yourdomain.com" 
        Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo)) 
        MailMsg.BodyEncoding = Encoding.Default 
        MailMsg.Subject = "This is a test" 
        MailMsg.Body = "This is a sample message using SMTP authentication" 
        MailMsg.Priority = MailPriority.High 
        MailMsg.IsBodyHtml = True 
        'Smtpclient to send the mail message 
 
        Dim SmtpMail As New SmtpClient 
        Dim basicAuthenticationInfo As New System.Net.NetworkCredential("postmaster@mydoamin.com", "password") 

''IMPORANT:  Your smtp login email MUST be same as your FROM address.
 
        SmtpMail.Host = "mail.yourdomain.com" 
        SmtpMail.UseDefaultCredentials = False 
        SmtpMail.Credentials = basicAuthenticationInfo
        SmtpMail.Port = 25;    //alternative port number is 8889
        SmtpMail.EnableSsl = false;
 
        SmtpMail.Send(MailMsg) 
        lblMessage.Text = "Mail Sent"     
    End Sub 
</script> 
<html> 
<body> 
    <form runat="server"
        <asp:Label id="lblMessage" runat="server"></asp:Label> 
    </form> 
</body> 
</html> 

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) 
    { 
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; 
       //create the mail message 
        MailMessage mail = new MailMessage(); 
 
        //set the addresses 
        mail.From = new MailAddress("postmaster@yourdomain.com"); //IMPORTANT: This must be same as your smtp authentication address.
        mail.To.Add("postmaster@yourdomain.com"); 
        
        //set the content 
        mail.Subject = "This is an email"
        mail.Body = "This is from system.net.mail using C sharp with smtp authentication."
        //send the message 
         SmtpClient smtp = new SmtpClient("mail.yourdomain.com"); 
         
//IMPORANT:  Your smtp login email MUST be same as your FROM address. 
         NetworkCredential Credentials = new NetworkCredential("postmaster@yourdomain.com", "password"); 
         smtp.UseDefaultCredentials = false;
         smtp.Credentials = Credentials;
         smtp.Port = 25;    //alternative port number is 8889
         smtp.EnableSsl = false;
         smtp.Send(mail); 
         lblMessage.Text = "Mail Sent"
    } 
</script> 
<html> 
<body> 
    <form runat="server"
        <asp:Label id="lblMessage" runat="server"></asp:Label> 
    </form> 
</body> 
</html> 


Download a Testing Script

Click Here to download a ASP.NET script to test your SMTP