- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net.Mail;
- namespace wfaDemo
- {
- public partial class frmMail : Form
- {
- public frmMail()
- {
- InitializeComponent();
- }
- private void txtPort_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!Char.IsDigit(e.KeyChar))
- e.Handled = true;
- }
- private void txtUsername_Leave(object sender, EventArgs e)
- {
- Regex mRegxExpression;
- if (txtUsername.Text.Trim() != string.Empty)
- {
- mRegxExpression = new Regex(@"^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}|((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\])$");
- if (!mRegxExpression.IsMatch(txtUsername.Text.Trim()))
- {
- MessageBox.Show("E-mail address format is not correct.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- txtUsername.Focus();
- }
- }
- }
- public void Clear()
- {
- txtUsername.Clear();
- txtPassword.Clear();
- txtPort.Clear();
- txtPort.Clear();
- txtTo.Clear();
- txtCc.Clear();
- txtBcc.Clear();
- txtSubject.Clear();
- txtSMTP.Clear();
- rtxtBody.Clear();
- }
- private void btnSend_Click(object sender, EventArgs e)
- {
- try
- {
- MailMessage mail = new MailMessage();
- SmtpClient SmtpServer = new SmtpClient(txtSMTP.Text);
- mail.From = new MailAddress(txtUsername.Text.ToLower());
- if ((txtTo.Text != null) && (txtTo.Text != string.Empty))
- {
- string[] multiRec = txtTo.Text.Split(';');
- foreach (string to in multiRec)
- {
- if (to.Trim().Length > 0)
- {
- mail.To.Add(new MailAddress(to));
- }
- }
- //mail.To.Add("To Email Address");
- }
- if ((txtCc.Text != null) && (txtCc.Text != string.Empty))
- mail.CC.Add(txtCc.Text);
- if ((txtBcc.Text != null) && (txtBcc.Text != string.Empty))
- mail.Bcc.Add(txtBcc.Text);
- mail.Subject = txtSubject.Text;
- mail.Body = rtxtBody.Text;
- SmtpServer.Port = int.Parse(txtPort.Text);
- SmtpServer.Credentials = new System.Net.NetworkCredential(txtUsername.Text.ToLower(), txtPassword.Text);
- if (chkSSL.Checked == true)
- SmtpServer.EnableSsl = true;
- else
- SmtpServer.EnableSsl = true;
- SmtpServer.Send(mail);
- MessageBox.Show("Mail Send Successfully");
- Clear();
- }
- catch (Exception x)
- {
- MessageBox.Show("Error : " + x.ToString());
- }
- }
- }
- }
This Code for Mail Sending From C#.