I'm developing a system that sends batch email to clients, but if the user who is operating the system clicks on any part of the window during the process, it appears that the system is not responding, interrupting the sending process .
I would like a solution so that even the user clicking on the screen or performing operations on other programs the system will continue sending the emails and informing the percentage completed.
private void bnt_EnviarEmail_Click(object sender, EventArgs e) {
for (int i = 0; i < grid1.RowCount; i++) {
if (grid1.Rows[i].Cells[5].Value.ToString() == "S") //VERIFICA SE E O ENVIO DE EMAIL ESTA ATIVO PARA ESTE CLIENTE
{
int COD_CLI_atual = int.Parse(grid1.Rows[i].Cells[0].Value.ToString());
using(DatabaseEntities db = new DatabaseEntities()) {
Clientes c = (from cli in db.Clientes where cli.CODIGO == COD_CLI_atual select cli).FirstOrDefault();
string email = c.EMAIL;
string nome_fantasia = c.NOME_FANTASIA;
string cnpj = c.CNPJ;
EnviarEmail em = new EnviarEmail();
em.Enviar(email, "MENSAGEM DO EMAIL" + nome_fantasia + " " + cnpj);
}
}
}
}
Method used to send email:
public void Enviar(string Destinatario, string Assunto, string Texto) {
string Usuario = usuario;
string Senha = senha;
int porta = 0;
bool ssl;
string servidor = true;
Email = new MailMessage();
Email.To.Add(new MailAddress(Destinatario));
Email.From = new MailAddress(Usuario);
Email.Subject = Assunto;
Email.IsBodyHtml = false;
Email.Body = Texto;
SmtpClient cliente = new SmtpClient(servidor, porta);
cliente.Credentials = new System.Net.NetworkCredential(Usuario, Senha);
cliente.EnableSsl = true;
cliente.Send(Email);
}