Guest User

Untitled

a guest
Oct 23rd, 2017
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Net;
  11. using System.Net.Mail;
  12.  
  13.  
  14. namespace CO6009DissertationV5
  15. {
  16.  
  17. public partial class frmSendMsg : Form
  18. {
  19.  
  20. public frmSendMsg()
  21. {
  22. InitializeComponent();
  23. }
  24.  
  25. private void btnSend_Click(object sender, EventArgs e)
  26. {
  27. SmtpClient client = new SmtpClient();
  28. client.Host = "smtp.gmail.com";
  29. client.Port = 587;
  30. client.EnableSsl = true;
  31.  
  32. System.Net.NetworkCredential userpassword = new System.Net.NetworkCredential();
  33. userpassword.UserName = "user@gmail.com";
  34. userpassword.Password = "password";
  35.  
  36. client.Credentials = userpassword;
  37.  
  38. MailMessage msg = new MailMessage("user@gmail.com", "user@gmail.com");
  39. **msg.To.Add(new MailAddress(txtBoxToEmail.Text));**
  40. msg.Body = "<b> Sender's Name: </b>" + txtBoxName.Text + "<p><b> Sender's E-mail: </b>" + txtBoxEmail.Text + "<p><b>Sender's Subject: </b>" + txtBoxSubject.Text + "<p><b>Sender's Message: </b>" + "<p>" + txtBoxMsg.Text;
  41. msg.Subject = txtBoxSubject.Text;
  42. msg.IsBodyHtml = true;
  43.  
  44. try
  45. {
  46. client.Send(msg);
  47. lblMsgStatus.Text = "<p> Message Successfully Sent </p>";
  48. }
  49.  
  50. catch (Exception ex)
  51. {
  52. lblMsgStatus.Text = "Send failed";
  53. }
  54. }
  55. }
  56. }
  57.  
  58. msg.To.Add(new MailAddress(txtBoxToEmail.Text));
  59.  
  60. msg.To.Add(new MailAddress(txtBoxToEmail.Text));
  61.  
  62. private void btnSend_Click(object sender, EventArgs e)
  63. {
  64. if (String.IsNullOrWhitespace(txtBoxToEmail.Text))
  65. {
  66. MessageBox.Show("To can't be empty!","Invalid Address",
  67. MessageBoxButtons.OK,MessageBoxIcon.Error);
  68. return;
  69. }
  70. ...
  71.  
  72. private void textBox1_Validating(object sender,
  73. System.ComponentModel.CancelEventArgs e)
  74. {
  75. string errorMsg;
  76. if(!ValidEmailAddress(textBox1.Text, out errorMsg))
  77. {
  78. // Cancel the event and select the text to be corrected by the user.
  79. e.Cancel = true;
  80. textBox1.Select(0, textBox1.Text.Length);
  81.  
  82. // Set the ErrorProvider error with the text to display.
  83. this.errorProvider1.SetError(textBox1, errorMsg);
  84. }
  85. }
  86.  
  87. private void textBox1_Validated(object sender, System.EventArgs e)
  88. {
  89. // If all conditions have been met, clear the ErrorProvider of errors.
  90. errorProvider1.SetError(textBox1, "");
  91. }
  92. public bool ValidEmailAddress(string emailAddress, out string errorMessage)
  93. {
  94. // Confirm that the e-mail address string is not empty.
  95. if(String.IsNullOrWhitespace(emailAddress.Length))
  96. {
  97. errorMessage = "e-mail address is required.";
  98. return false;
  99. }
  100.  
  101. // Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
  102. if(emailAddress.IndexOf("@") > -1)
  103. {
  104. if(emailAddress.IndexOf(".", emailAddress.IndexOf("@") ) > emailAddress.IndexOf("@") )
  105. {
  106. errorMessage = "";
  107. return true;
  108. }
  109. }
  110.  
  111. errorMessage = "e-mail address must be valid e-mail address format.n" +
  112. "For example 'someone@example.com' ";
  113. return false;
  114. }
  115.  
  116. if (IsValidEmail(txtBoxToEmail.Text))
  117. {
  118. msg.To.Add(new MailAddress(txtBoxToEmail.Text));
  119. }
  120.  
  121. static bool IsValidEmail(string email)
  122. {
  123. try
  124. {
  125. var addr = new System.Net.Mail.MailAddress(email);
  126. return addr.Address == email;
  127. }
  128. catch
  129. {
  130. return false;
  131. }
  132. }
Add Comment
Please, Sign In to add comment