Asked by:
Send Email Error with Thread

Question
-
User1604908548 posted
hello people, I Have a problem with send email.
This is the error:
System.Threading.ThreadAbortException: Thread was being aborted. at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout) at System.Threading.Thread.Sleep(TimeSpan timeout) at Emailing.Engine.Engine.Process() in C:\Proyectos\Emailing\Code\Emailing.Engine\Engine.cs:line 63
this is my code:
1 public class Engine 2 { 3 private static Thread thProcess; 4 5 public static void Start() 6 { 7 thProcess = new Thread(new ThreadStart(Process)); 8 thProcess.Start(); 9 } 10 11 public static void Stop() 12 { 13 thProcess.Abort(); 14 } 15 16 private static void Process() 17 { 18 while (true) 19 { 20 try 21 { 22 if (BLL.Util.Config.Habilitado) 23 { 24 List<dcl.envio> Envios = BLL.Envio.GetEnvioList(true); 25 26 foreach (DCL.Envio Envio in Envios) 27 { 28 foreach(DCL.Cliente Cliente in Envio.Clientes) 29 { 30 MailMessage msg = new MailMessage(); 31 msg.To.Add (new MailAddress(Cliente.Email)); 32 msg.From = new MailAddress(BLL.Util.Config.SmtpSection.From); 33 msg.Subject = Envio.Asunto; 34 msg.IsBodyHtml = true; 35 msg.Body = Replace(Envio, Cliente); 36 foreach (DCL.Archivo Archivo in Envio.Archivos) 37 { 38 msg.Attachments.Add(new Attachment(BLL.Util.Config.Adjuntos + Envio.idEnvio + "/" + Archivo.Nombre)); 39 } 40 41 SmtpClient smtp = new SmtpClient(); 42 43 try 44 { 45 smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; 46 smtp.Send(msg); 47 BLL.EnvioCliente.Update(Envio.idEnvio, Cliente.idCliente, BLL.EnvioCliente.Estado.Enviado, ""); 48 } 49 catch(Exception ex) 50 { 51 BLL.EnvioCliente.Update(Envio.idEnvio, Cliente.idCliente, BLL.EnvioCliente.Estado.Error, ex.Message); 52 } 53 Thread.Sleep(new TimeSpan(0, BLL.Util.Config.TiempoPausa, 0)); 54 } 55 BLL.Envio.UpdateEstado(Envio.idEnvio, Emailing.BLL.Envio.Estado.Enviado); 56 } 57 } 58 } 59 catch (Exception ex) 60 { 61 //BLL.Log.Insert("Emailing.Engine.Process", ex.Message); 62 BLL.Log.Insert("Emailing.Engine.Process", ex.ToString()); 63 } 64 Thread.Sleep(new TimeSpan(0, 10, 0)); 65 } 66 } 67 68 private static string Replace(DCL.Envio Envio, DCL.Cliente Cliente) 69 { 70 string url = BLL.Util.Config.UrlSitioWeb + "/Encuesta/Responde.aspx?idEnvio=" + Envio.idEnvio + "&idCliente=" + Cliente.idCliente; 71 string Html = Envio.Contenido.Replace("#0000#", url); 72 73 Html = Html.Replace("00EMAIL00", Cliente.Email); 74 Html = Html.Replace("00NOMBRE00", Cliente.Nombre); 75 Html = Html.Replace("00RUT00", Cliente.Rut); 76 Html = Html.Replace("00TELEFONO100", Cliente.Telefono1); 77 Html = Html.Replace("00TELEFONO200", Cliente.Telefono2); 78 Html = Html.Replace("00NOCONTRATO00", Cliente.NoContrato); 79 Html = Html.Replace("00IDCLIENTE00", Cliente.idCliente.ToString()); 80 81 string img = "<br/><br/><br/><br/>Si no quieres recibir más información de XXXXX, haz click <a href=\"" + BLL.Util.Config.UrlSitioWeb + "/Email/Baja.aspx?idCliente=" + Cliente.idCliente + "\">aquí</a><img src=\"" + BLL.Util.Config.UrlSitioWeb + "/Email/Lectura.aspx?idEnvio=" + Envio.idEnvio + "&idCliente=" + Cliente.idCliente + "\" width='1' height='1' />"; 82 int index = Html.IndexOf("</body>"); 83 if (index > -1) 84 { 85 return Html.Insert(index, img); 86 } 87 index = Html.IndexOf("</BODY>"); 88 if (index > -1) 89 { 90 return Html.Insert(index, img); 91 } 92 return Html + img; 93 } 94 } </dcl.envio>
Monday, December 15, 2008 8:38 AM
All replies
-
User398825048 posted
Try creating only 1 smtpclient object i.e outside the loop and use the same client object whenver u want to send mail i.e smptclient.send();
OR
Alteast put it in Using block.
Monday, October 7, 2013 8:23 AM