How to send email through asp.net

Please follow below steps to configure the email:

  • Create a new website in visual studio.
  • Go to the menu, select “Website” and then click on “Add New Item” or you can press Ctrl+Shift+A directly to prompt “Add New Items” dialogue.
  • Select “Web Form”, add file name as per you convenient and click on Add button, ensure that “Place code in separate file” has been checked. If not than check it.
  • Just go to the newly added file “your given file name.aspx”.
  • Add new label form toolbox or you can paste these code <label>Name:</label>
  • Add new textbox form toolbox or you can paste these code <asp:TextBox ID=”txtName” runat=”server” Width=”300px”></asp:TextBox>
  • Add new label form toobox or you can paste these code <label>Email ID:</label>
  • Add new textbox form toolbox or you can paste these code <asp:TextBox ID=”txtEmailID” runat=”server” Width=”300px”></asp:TextBox>
  • Add new button form toolbox or you can paste these code <asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” OnClick=”btnSubmit_Click” />
  • After this go to the newly added file “your given file name.aspx.cs”.
  • Add  these namespace on you page: using System.Web.Mail; using System.Net; using System.Net.NetworkInformation;
  • Create a string variable for body like strBody and assign a value like this strBody=”This is mail body”;
  • Go to .aspx page and double click on button and paste below codes under that section.
  •  System.Net.Mail.MailMessage mymail = new System.Net.Mail.MailMessage(“FromEmailAddress”,txtEmailID.text, “Subject”, strbody);
  • mymail.IsBodyHtml = true;
  •  NetworkCredential mailAuthentic = new NetworkCredential(“FromEmailAddress”, “Your Password”);
  • System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient(“SMPT Client address”, 25);
  • mailclient.UseDefaultCredentials = true;
  •  mailclient.Credentials = mailAuthentic;
  • mailclient.Send(mymail);
  • Here is the demo for send mail through your aspx website. If you have any query please share your inputs with us so that we can serve you better.