Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


C#
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

C#
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)..
Posted
Updated 12-Feb-16 23:39pm
v3

1 solution

A progress bar only works if you have something which does regular updates on how far it has got - and neither client.Send or client.SendAsync provide that.
So the best way it to use a marquee progress bar[^] which keeps cycling until you stop it, and start it before you begin the send operation. If you use SendAsync then handle the SendCompleted event to stop the marquee.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900