Advertisement
Guest User

Untitled

a guest
Oct 7th, 2017
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Net.Mail;
  8. using System.Data;
  9. using System.Data.OleDb; // for DB
  10.  
  11.  
  12. public partial class _Default : System.Web.UI.Page
  13. {
  14. //static string strCon = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionString["CustomerConnString"].ConnectionString;
  15. //OleDbConnection conn
  16.  
  17. //go web.config
  18. //<connectionString>
  19. //<add name="CustomerConnString"
  20. //connectionString="provider=Microsoft.jet.oledb.4.0 data source="-location-""
  21. //</connectionString>
  22. protected void Page_Load(object sender, EventArgs e)
  23. {
  24.  
  25. }
  26.  
  27. public SmtpClient client = new SmtpClient();
  28. public MailMessage msg = new MailMessage();
  29.  
  30. public System.Net.NetworkCredential smtpCreds =
  31. new System.Net.NetworkCredential("iwannagofrance@gmail.com", "onglinsheng");
  32. public void SendEmail(string sendTo, string sendFrom, string subject, string body)
  33. {
  34. try
  35. {
  36. //setup smtp host here
  37. client.Host = "smtp.gmail.com";
  38. client.Port = 587;
  39. client.UseDefaultCredentials = false;
  40. client.Credentials = smtpCreds;
  41. client.EnableSsl = true;
  42.  
  43. //convert string to mailaddress
  44. MailAddress to = new MailAddress(sendTo);
  45. MailAddress from = new MailAddress(sendFrom);
  46.  
  47. //set up message settings
  48. msg.Subject = subject;
  49. msg.Body = body;
  50. msg.From = from;
  51. msg.To.Add(to);
  52.  
  53. //send email
  54. client.Send(msg);
  55. }
  56. catch
  57. {
  58. lblMessage.Text = "Error!";
  59. }
  60. }
  61. protected void cmdSubmit_Click(object sender, EventArgs e)
  62. {
  63. if (txtEmail.Text.Length < 5)
  64. {
  65. lblMessage.Text = "Please enter your email";
  66. }
  67. else
  68. {
  69. if (txtName.Text.Length < 5)
  70. {
  71. lblMessage.Text = "Please enter your name";
  72. }
  73. else
  74. {
  75. if (txtComment.Text.Length < 5)
  76. {
  77. lblMessage.Text = "Please enter your comment";
  78. }
  79. else
  80. {
  81. //nothing
  82. }
  83. SendEmail("iwannagofrance@gmail.com", "iwannagofrance@gmail.com", "From: " + txtName.Text + ", Email: " + txtEmail.Text, txtComment.Text);
  84. lblMessage.Text = "Thank you " + txtName.Text + ", your comment has been submitted";
  85.  
  86. }
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement