Advertisement
Guest User

Untitled

a guest
May 24th, 2018
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. private void btnExit_Click(object sender, EventArgs e)
  2.         {
  3.             this.Close();
  4.         }
  5.  
  6.         private void btnReset_Click(object sender, EventArgs e)
  7.         {
  8.             txtAttach.ResetText();
  9.             txtGmail.ResetText();
  10.             txtMailFrom.ResetText();
  11.             txtMailTo.ResetText();
  12.             txtMess.ResetText();
  13.             txtPassWord.ResetText();
  14.             txtPortGmail.ResetText();
  15.             txtPortYahoo.ResetText();
  16.             txtSubject.ResetText();
  17.             txtUser.ResetText();
  18.             txtYahoo.ResetText();
  19.             radioGmail.Checked = false;
  20.             radioYahoo.Checked = false;
  21.         }
  22.  
  23.         private void btnAttach_Click(object sender, EventArgs e)
  24.         {
  25.             string path = "";
  26.             OpenFileDialog openDiglog1 = new OpenFileDialog();
  27.             openDiglog1.ShowDialog();
  28.             path = openDiglog1.FileName;
  29.             txtAttach.Text = path;
  30.         }
  31.  
  32.         MailMessage mail = new MailMessage(); //Khởi tạo 1 MailMessage
  33.         SmtpClient smtpClient; //Khai báo đối tượng của lớp SmtpClient gửi mail
  34.         private void btnSend_Click(object sender, EventArgs e)
  35.         {
  36.             try
  37.             {
  38.                 if (radioGmail.Checked == true)
  39.                 {
  40.                     smtpClient = new SmtpClient(txtGmail.Text); //Khởi tạo đối tượng bằng SmtpClient được lấy từ textBox Gmail nếu muốn dùng Gmail
  41.                     smtpClient.Port = int.Parse(txtPortGmail.Text); //Sử dụng port của Gmail
  42.                 }
  43.                 else if(radioYahoo.Checked == true) //Ngược lại nếu dùng Yahoo
  44.                 {
  45.                     smtpClient = new SmtpClient(txtYahoo.Text);
  46.                     smtpClient.Port = int.Parse(txtPortYahoo.Text);
  47.                 }
  48.                 mail.From = new MailAddress(txtMailFrom.Text); //Thiết lập địa chỉ người gửi từ textBox MailFrom
  49.                 mail.To.Add(txtMailTo.Text); //Thiết lập địa chỉ người nhận
  50.                 mail.Subject = txtSubject.Text; //Chủ đề của email
  51.                 mail.Body = txtMess.Text; //Nội dung của email
  52.                 Attachment attachment = new Attachment(txtAttach.Text); //Đính kèm tập tin từ đường dẫn trong textBox txtAttach
  53.                 mail.Attachments.Add(attachment); //Thêm file này vào đính kèm của mail
  54.                 smtpClient.Credentials = new System.Net.NetworkCredential(txtUser.Text, txtPassWord.Text); //Để truy xuất được email, client phải chứng thực chính nó với username và password
  55.                 smtpClient.EnableSsl = true; //Gửi mail có bảo mật
  56.                 smtpClient.SendCompleted += new SendCompletedEventHandler(smtp_SendComplete); //Tạo hàm smtp_SendComplete vì khi sử dụng SendAsync, email được send với các thread khác nhau nên quá trình hoàn tất việc send email cũng không được tiến hành ngay lập tức, lớp smtpClient cung cấp sự kiện SendComplete email, trong hàm này ta có thể bắt được lỗi xảy ra
  57.                 object userState = mail;
  58.                 smtpClient.SendAsync(mail, userState); //Phương thức gửi email bất đồng bộ này cần thêm tham số User Token (kiểu object) được truyền khi send email
  59.             }
  60.             catch
  61.             {
  62.                 MessageBox.Show("Có lỗi xảy ra, vui lòng kiểm tra lại!", "Thông báo");
  63.             }
  64.  
  65.         }
  66.  
  67.         private void smtp_SendComplete(object sender, AsyncCompletedEventArgs e)
  68.         {
  69.             if (e.Error != null) //Nếu có lỗi xảy ra thì email không send được và thông báo lỗi
  70.                 MessageBox.Show("Có lỗi xảy ra!", "Thông báo");
  71.             else
  72.                 MessageBox.Show("Gửi thành công!", "Thông báo");
  73.         }
  74.  
  75.         private void radioGmail_CheckedChanged(object sender, EventArgs e)
  76.         {
  77.             if (radioGmail.Checked == true)
  78.             {
  79.                 txtGmail.Text = "smtp.gmail.com";
  80.                 txtPortGmail.Text = "587";
  81.             }
  82.             else
  83.             {
  84.                 txtGmail.ResetText();
  85.                 txtPortGmail.ResetText();
  86.             }
  87.         }
  88.  
  89.         private void radioYahoo_CheckedChanged(object sender, EventArgs e)
  90.         {
  91.             if (radioYahoo.Checked == true)
  92.             {
  93.                 txtYahoo.Text = "smtp.mail.yahoo.com";
  94.                 txtPortYahoo.Text = "587";
  95.             }
  96.             else
  97.             {
  98.                 txtYahoo.ResetText();
  99.                 txtPortYahoo.ResetText();
  100.             }
  101.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement