Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Mail;
- namespace CO6009DissertationV5
- {
- public partial class frmSendMsg : Form
- {
- public frmSendMsg()
- {
- InitializeComponent();
- }
- private void btnSend_Click(object sender, EventArgs e)
- {
- SmtpClient client = new SmtpClient();
- client.Host = "smtp.gmail.com";
- client.Port = 587;
- client.EnableSsl = true;
- System.Net.NetworkCredential userpassword = new System.Net.NetworkCredential();
- userpassword.UserName = "user@gmail.com";
- userpassword.Password = "password";
- client.Credentials = userpassword;
- MailMessage msg = new MailMessage("user@gmail.com", "user@gmail.com");
- **msg.To.Add(new MailAddress(txtBoxToEmail.Text));**
- 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;
- msg.Subject = txtBoxSubject.Text;
- msg.IsBodyHtml = true;
- try
- {
- client.Send(msg);
- lblMsgStatus.Text = "<p> Message Successfully Sent </p>";
- }
- catch (Exception ex)
- {
- lblMsgStatus.Text = "Send failed";
- }
- }
- }
- }
- msg.To.Add(new MailAddress(txtBoxToEmail.Text));
- msg.To.Add(new MailAddress(txtBoxToEmail.Text));
- private void btnSend_Click(object sender, EventArgs e)
- {
- if (String.IsNullOrWhitespace(txtBoxToEmail.Text))
- {
- MessageBox.Show("To can't be empty!","Invalid Address",
- MessageBoxButtons.OK,MessageBoxIcon.Error);
- return;
- }
- ...
- private void textBox1_Validating(object sender,
- System.ComponentModel.CancelEventArgs e)
- {
- string errorMsg;
- if(!ValidEmailAddress(textBox1.Text, out errorMsg))
- {
- // Cancel the event and select the text to be corrected by the user.
- e.Cancel = true;
- textBox1.Select(0, textBox1.Text.Length);
- // Set the ErrorProvider error with the text to display.
- this.errorProvider1.SetError(textBox1, errorMsg);
- }
- }
- private void textBox1_Validated(object sender, System.EventArgs e)
- {
- // If all conditions have been met, clear the ErrorProvider of errors.
- errorProvider1.SetError(textBox1, "");
- }
- public bool ValidEmailAddress(string emailAddress, out string errorMessage)
- {
- // Confirm that the e-mail address string is not empty.
- if(String.IsNullOrWhitespace(emailAddress.Length))
- {
- errorMessage = "e-mail address is required.";
- return false;
- }
- // Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
- if(emailAddress.IndexOf("@") > -1)
- {
- if(emailAddress.IndexOf(".", emailAddress.IndexOf("@") ) > emailAddress.IndexOf("@") )
- {
- errorMessage = "";
- return true;
- }
- }
- errorMessage = "e-mail address must be valid e-mail address format.n" +
- "For example 'someone@example.com' ";
- return false;
- }
- if (IsValidEmail(txtBoxToEmail.Text))
- {
- msg.To.Add(new MailAddress(txtBoxToEmail.Text));
- }
- static bool IsValidEmail(string email)
- {
- try
- {
- var addr = new System.Net.Mail.MailAddress(email);
- return addr.Address == email;
- }
- catch
- {
- return false;
- }
- }
Add Comment
Please, Sign In to add comment