Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void btnExit_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void btnReset_Click(object sender, EventArgs e)
- {
- txtAttach.ResetText();
- txtGmail.ResetText();
- txtMailFrom.ResetText();
- txtMailTo.ResetText();
- txtMess.ResetText();
- txtPassWord.ResetText();
- txtPortGmail.ResetText();
- txtPortYahoo.ResetText();
- txtSubject.ResetText();
- txtUser.ResetText();
- txtYahoo.ResetText();
- radioGmail.Checked = false;
- radioYahoo.Checked = false;
- }
- private void btnAttach_Click(object sender, EventArgs e)
- {
- string path = "";
- OpenFileDialog openDiglog1 = new OpenFileDialog();
- openDiglog1.ShowDialog();
- path = openDiglog1.FileName;
- txtAttach.Text = path;
- }
- MailMessage mail = new MailMessage(); //Khởi tạo 1 MailMessage
- SmtpClient smtpClient; //Khai báo đối tượng của lớp SmtpClient gửi mail
- private void btnSend_Click(object sender, EventArgs e)
- {
- try
- {
- if (radioGmail.Checked == true)
- {
- 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
- smtpClient.Port = int.Parse(txtPortGmail.Text); //Sử dụng port của Gmail
- }
- else if(radioYahoo.Checked == true) //Ngược lại nếu dùng Yahoo
- {
- smtpClient = new SmtpClient(txtYahoo.Text);
- smtpClient.Port = int.Parse(txtPortYahoo.Text);
- }
- mail.From = new MailAddress(txtMailFrom.Text); //Thiết lập địa chỉ người gửi từ textBox MailFrom
- mail.To.Add(txtMailTo.Text); //Thiết lập địa chỉ người nhận
- mail.Subject = txtSubject.Text; //Chủ đề của email
- mail.Body = txtMess.Text; //Nội dung của email
- Attachment attachment = new Attachment(txtAttach.Text); //Đính kèm tập tin từ đường dẫn trong textBox txtAttach
- mail.Attachments.Add(attachment); //Thêm file này vào đính kèm của mail
- 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
- smtpClient.EnableSsl = true; //Gửi mail có bảo mật
- 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
- object userState = mail;
- 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
- }
- catch
- {
- MessageBox.Show("Có lỗi xảy ra, vui lòng kiểm tra lại!", "Thông báo");
- }
- }
- private void smtp_SendComplete(object sender, AsyncCompletedEventArgs e)
- {
- 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
- MessageBox.Show("Có lỗi xảy ra!", "Thông báo");
- else
- MessageBox.Show("Gửi thành công!", "Thông báo");
- }
- private void radioGmail_CheckedChanged(object sender, EventArgs e)
- {
- if (radioGmail.Checked == true)
- {
- txtGmail.Text = "smtp.gmail.com";
- txtPortGmail.Text = "587";
- }
- else
- {
- txtGmail.ResetText();
- txtPortGmail.ResetText();
- }
- }
- private void radioYahoo_CheckedChanged(object sender, EventArgs e)
- {
- if (radioYahoo.Checked == true)
- {
- txtYahoo.Text = "smtp.mail.yahoo.com";
- txtPortYahoo.Text = "587";
- }
- else
- {
- txtYahoo.ResetText();
- txtPortYahoo.ResetText();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement