I am trying to send the mail in c# windows application, and i used the progress bar
but it does not show while sending mail , only shown after sent the mail.
StrTable1 = "<table align='center' width='100%'><tr><td></td></tr><tr><td>" + content + "</td></tr></table></td></tr></table>";
bool SendMail()
{
if (string.IsNullOrEmpty(admail))
admail = "abc@abc.com";
mail.To.Add(admail);
if (mail.To.Count > 0)
{
mail.To.Clear();
mail.Bcc.Clear();
mail.CC.Clear();
mail.To.Add(admail);
if (!string.IsNullOrEmpty(bcc))
{
mail.Bcc.Add(bcc);
}
if (!string.IsNullOrEmpty(cc))
{
mail.CC.Add(cc);
}
}
mail.From = new MailAddress(TI_Mail1_address.Trim(), TI_Mail1_address.Trim());
mail.Subject = subject;
mail.Priority = MailPriority.High;
mail.Body = StrTable1;
System.Net.Mail.Attachment attachment;
ds = dbfun.GetQuotationdetail(Q_ID);
DirectoryInfo dinfo = new DirectoryInfo(@"C:\attachemnt");
FileInfo[] Files = dinfo.GetFiles();
mail.Attachments.Clear();
Cursor.Current = Cursors.WaitCursor;
if (mail.Attachments.Count < Files.Length + 1)
{
attachment = new System.Net.Mail.Attachment(location + "\\Quotations\\" + ds.Tables[0].Rows[0]["filename"].ToString());
mail.Attachments.Add(attachment);
for (int a = 0; a < Files.Length; a++)
{
mail.Attachments.Add(new System.Net.Mail.Attachment(@"C:\attachemnt\" + Files[a].ToString()));
}
}
SmtpClient client = new SmtpClient("mail.abc.com");
client.Port = 587;
client.EnableSsl = false;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(TI_Mail1_address.Trim(), TI_Mail1_pwd.Trim());
mail.IsBodyHtml = true;
client.EnableSsl = false;
object obj = true;
try
{
client.Send(mail);
mailSendStatus = true;
}
catch (Exception ee)
{
mailSendStatus = false;
}
return mailSendStatus;
}
////////
Button Click Event
private void btnsend_Click(object sender, EventArgs e)
{
progressBar1.Visible = true;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = 1;
progressBar1.Step = 1;
progressBar1.Value = 25;
if (SenddMaill() == true)
{
progressBar1.Value = 75;
progressBar1.Value = 100;
lblsuccess.Text = "Mail Successfully sent";
if (MessageBox.Show("Mail successfully Sent!") == DialogResult.OK)
this.Close();
}
}
By this code, progress bar shown only after the button click has done, but when done the button click event the mail also sent and directly show the Msg
"Mail Sent Successfully"..
kindly help on this....
What I have tried:
I have tried with the progress bar, it doesn't work properly.
as well as i have tried the client.SendAsync(mail, obj); instead of client.Send(mail)..