In this article I’ve implemented the code to Send Email in Asp.Net C# Using SMTP.
Many times when you implement a contact form page in Asp.Net website you need to send that data to email whenever client query through contact page.
Unlike PHP in ASP.Net C# you can’t send emails just by calling mail method, in C# you have to provide proper SMTP and credentials to send email.
You can use below code to Send Email in Asp.Net C# Using SMTP.
Send Email in Asp.Net C# Using SMTP
![Send Email in Asp.Net C# Using SMTP](https://i0.wp.com/adzsol.com/wp-content/uploads/2018/12/maxresdefault-1.jpg?resize=525%2C314)
try { //Create Object of MailMessage Class MailMessage m = new MailMessage(); //Create Object of SmtpClient Class SmtpClient sc = new SmtpClient(); //Add Email Address to Which You want to send email m.To.Add("abc@xyz.com"); //Add Email Address of Sender With Display Name m.From = new MailAddress("demo@demo.com", "Demo Sender"); //Subject m.Subject = "Demo Mail"; //Include this if you want html in email body m.IsBodyHtml = true; //Add Reply To, If you want to change the default reply list m.ReplyToList.Add("demo@other.com"); //Body of Mail m.Body = "Demo Text"; //Credential of your email sc.Credentials = new NetworkCredential("email@email.com", "password"); //SMTP Host of your email, If you don't know this ask to your hosting provider sc.Host = "smtp.domain.com"; //Port of SMTP Server sc.Port = 587; //Enable SSL if ypur host support SSL sc.EnableSsl = true; //Call the send method sc.Send(m); //Dispose the object m.Dispose(); sc.Dispose(); } catch(Exception ex) { //log exception }
If you want to use gmail to send email through ASP.Net C# you can use below code to send that, just use your gmail email id and credential.
Send Email in Asp.Net C# Using Gmail SMTP
try { MailMessage m = new MailMessage(); SmtpClient sc = new SmtpClient(); m.To.Add("abc@xyz.com"); m.From = new MailAddress("your-email@gmail.com", "Your Name"); m.Subject = "Demo Mail"; m.IsBodyHtml = true; m.ReplyToList.Add("your-other-email@gmail.com"); m.Body = "Demo Text"; sc.Credentials = new NetworkCredential("your-email@gmail.com", "your gmail password"); sc.Host = "smtp.gmail.com"; sc.Port = 587; sc.EnableSsl = true; sc.Send(m); m.Dispose(); sc.Dispose(); } catch(Exception ex) { //log exception }
If you have any question or query ask it on Ask Question Page.