How to Send Email in Asp.Net using C#

  1. <%this is the client side code for the design and display%>  
  2. <asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">  
  3.     <p>Please Fill the Following to Send Mail.</p>  
  4.     <p>Your name:  
  5.   
  6.         <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"  
  7. ControlToValidate="YourName" ValidationGroup="save" />  
  8.         <br />  
  9.         <asp:TextBox ID="YourName" runat="server" Width="250px" />  
  10.         <br />  
  11. Your email address:  
  12.   
  13.         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"  
  14. ControlToValidate="YourEmail" ValidationGroup="save" />  
  15.         <br />  
  16.         <asp:TextBox ID="YourEmail" runat="server" Width="250px" />  
  17.         <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"  
  18. SetFocusOnError="true" Text="Example: [email protected]" ControlToValidate="YourEmail"  
  19. ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"  
  20. ValidationGroup="save" />  
  21.         <br />  
  22. Subject:  
  23.   
  24.         <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"  
  25. ControlToValidate="YourSubject" ValidationGroup="save" />  
  26.         <br />  
  27.         <asp:TextBox ID="YourSubject" runat="server" Width="400px" />  
  28.         <br />  
  29. Your Question:  
  30.   
  31.         <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"  
  32. ControlToValidate="Comments" ValidationGroup="save" />  
  33.         <br />  
  34.         <asp:TextBox ID="Comments" runat="server"  
  35. TextMode="MultiLine" Rows="10" Width="400px" />  
  36.     </p>  
  37.     <p>  
  38.         <asp:Button ID="btnSubmit" runat="server" Text="Send"  
  39. OnClick="Button1_Click" ValidationGroup="save" />  
  40.     </p>  
  41. </asp:Panel>  
  42. <p>  
  43.     <asp:Label ID="DisplayMessage" runat="server" Visible="false" />  
  44. </p>  
C# Code
  1. protected void SendMail()   
  2. {  
  3.     // Gmail Address from where you send the mail  
  4.     var fromAddress = "[email protected]";  
  5.     // any address where the email will be sending  
  6.     var toAddress = YourEmail.Text.ToString();  
  7.     //Password of your gmail address  
  8.     const string fromPassword = "Password";  
  9.     // Passing the values and make a email formate to display  
  10.     string subject = YourSubject.Text.ToString();  
  11.     string body = "From: " + YourName.Text + "\n";  
  12.     body += "Email: " + YourEmail.Text + "\n";  
  13.     body += "Subject: " + YourSubject.Text + "\n";  
  14.     body += "Question: \n" + Comments.Text + "\n";  
  15.     // smtp settings  
  16.     var smtp = new System.Net.Mail.SmtpClient();   
  17.     {  
  18.         smtp.Host = "smtp.gmail.com";  
  19.         smtp.Port = 587;  
  20.         smtp.EnableSsl = true;  
  21.         smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;  
  22.         smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);  
  23.         smtp.Timeout = 20000;  
  24.     }  
  25.     // Passing values to smtp object  
  26.     smtp.Send(fromAddress, toAddress, subject, body);  
  27. }  
  28. protected void Button1_Click(object sender, EventArgs e)   
  29. {  
  30.     try   
  31.     {  
  32.         //here on button click what will done  
  33.         SendMail();  
  34.         DisplayMessage.Text = "Your Comments after sending the mail";  
  35.         DisplayMessage.Visible = true;  
  36.         YourSubject.Text = "";  
  37.         YourEmail.Text = "";  
  38.         YourName.Text = "";  
  39.         Comments.Text = "";  
  40.     }   
  41.     catch (Exception)  
  42.     {  
  43.     }  
  44. }