6
I am developing a system that sends mail in batches to customers, but if the user who is operating the system click 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 keeps sending the emails and reporting the completed percentage.
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);
}
Excerpt from the code posted.
– user18748
Ideally this class
EnviarEmail
is that it would need to be rewritten to use asynchronicity. Inside it you are usingSmtpClient
? Use the methodSend
? Tried to change toSendAsync
?– Maniero
I edited the post and put the code of the Send email class. I use the Smtpclient method. How Sendasync works?
– user18748
I would do one of the following ways: using Application.Doevents or using the component Backgroundworker, I will not answer officially because I believe there are more ideal solutions.
– mateusalxd