Email Sender (Windows Form Application Sending Email Using using System.Net.Mail)

Friday, December 28, 2012

Email Sender (Windows Form Application Sending Email Using using System.Net.Mail)



This application can send email using System.Net.Mail library...



#region  - Sending Method -
void SendReport(string host, 
string port,
string from, 
string password, 
string to, 
string bcc, 
string cc, 
string subject, 
string body)
{
 retry:
 try
 {
    MailMessage mMailMessage = new MailMessage();
    mMailMessage.From = new MailAddress(from);
    mMailMessage.To.Add(new MailAddress(to));
    if ((bcc != null) && (bcc != string.Empty))
    {
        mMailMessage.Bcc.Add(new MailAddress(bcc));
    }
    if ((cc != null) && (cc != string.Empty))
    {
        mMailMessage.CC.Add(new MailAddress(cc));
    }
    mMailMessage.Subject = subject;
    mMailMessage.Body = body;
    mMailMessage.IsBodyHtml = true;
    mMailMessage.Priority = MailPriority.Normal;
    SmtpClient smtp = new SmtpClient();
    if ((host != null) && (host != string.Empty))
    {
        smtp.Host = host;
    }
    else
    {
        smtp.Host = "smtp.gmail.com";
    }
    if(port!=null) && (port != string.Empty))
    {
        smtp.Port = int.Parse(port);
    }
    else
    {
        smtp.Port = int.Parse("587");
    }
    smtp.EnableSsl = true;
    smtp.Credentials = new System.Net.NetworkCredential(from, password);
    smtp.SendCompleted +=new SendCompletedEventHandler(smtp_SendCompleted);
    smtp.SendAsync(mMailMessage, new object());
    sendThread = new Thread(new ThreadStart(WhileSending));
    sendThread.Start();
}
catch (Exception ex)
{
    //??????????????????? Youre code here....
    if (You want to retry)
    {                   
        goto retry;
    }
}
}
#endregion  - Sending Method -
 #region - Method While Sending -
void WhileSending()
{
    int number = 4;
    while (IsSending)
    {
        Thread.Sleep(500);
        lblStatus.Text = "Sending email report ....".Substring(0, 20 + number);
        lblStatus.Image = global::ReportEmailer.Properties.Resources.send;
        number--;
        number = number <= 0 ? 4 : number;
    }
}
#endregion - Method While Sending -

0 comments :

Post a Comment