In My C# windows application i have built one automated mail system to inform the user about the key events they set in the application using SMTP Setting...this mailing system is working fine. I have used System.Net.Mail namespace to achieve this....
My Problem is that whenever the mail is sent successfully, I need to display a notification window(Popup window) displaying name of Recepient and his email ID on one windows form.
For this purpose i'm using secondary thread, but it is not working properly, it is making my main application non responsive till it delivers mail to all users. For each Mail have given thread sleep of 5 Secs, consider there are 10 mails, then I will not be able to use my main application for 50 seconds....So any help would be highly appreciated...
I have done C# coding like this:
//My main Function
public void SendMail(dataset ds)
{
for(inti=0;i<ds.Tables[0].Rows.Count;i++)
{
string resValue = genFunc.SMTPClienTSetting(ds, mail);
if (resValue == "True")
{
Thread tPopUp = new Thread(new ThreadStart(this.ShowMessagePopUp));
tPopUp.IsBackground = true;
tPopUp.Start();
lblPOpUpInfo = "Message sent successfully";
}
else
{
Thread tPopUp = new Thread(new ThreadStart(this.ShowMessagePopUp));
tPopUp.IsBackground = true;
tPopUp.Start();
lblPOpUpInfo = "Message sending failed";
}
}
}
//Thread Function to open secondary form
public void ShowMessagePopUp()
{
try
{
frmHomePopUp homePopUp = new frmHomePopUp();
homePopUp.Show();
homePopUp.lblInfo.Text = lblPOpUpInfo;
Application.DoEvents();
Thread.Sleep(5000);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public string SMTPClienTSetting(DataSet ds, MailMessage MailstoSend)
{
try
{
SmtpClient objsmtp = new SmtpClient();
NetworkCredential NetAccess = new NetworkCredential();
NetAccess.UserName = ds.Tables[0].Rows[0][0].ToString();
NetAccess.Password = ds.Tables[0].Rows[0][1].ToString();
objsmtp.Credentials = NetAccess;
objsmtp.Host = ds.Tables[0].Rows[0][2].ToString();
objsmtp.Port = Convert.ToInt16(ds.Tables[0].Rows[0][3].ToString());
objsmtp.Send(MailstoSend);
System.Threading.Thread.Sleep(500);
return "True";
}
catch (NullReferenceException nr)
{
return nr.Message;
}
catch (Exception ex)
{
return ex.Message;
}
}
I'm able to show my form using using thread, but main application is not responding till it finishes sending mails....