private string GetHtmlMessage(CotizacionMessage cotDet) { var table = ""; cotDet.Cotizacion.cotiza.ForEach(c => { table += string.Format("<tr><td>{0}</td> <td>{1}</td></tr>", c.prodid, c.prodqty); }); var Cotiza = ""; Cotiza = @" <html> <body> <h1> <span style='color:#ff0000;'><span style='font-family: 'trebuchet ms', helvetica, sans-serif;'><strong><u>Se ha recibido una nueva cotización</u></strong></span></span></h1> <p> <span style='font-family:trebuchet ms,helvetica,sans-serif;'>El codigo de cotización es: {0}</span></p> <p> <span style='color:#0000ff;'><strong><span style='font-family: 'trebuchet ms', helvetica, sans-serif;'>Información de la persona interesada</span></strong></span></p> <p style='margin-left: 40px;'> <span style='font-family:trebuchet ms,helvetica,sans-serif;'>Nombre:{1}</span></p> <p style='margin-left: 40px;'> <span style='font-family:trebuchet ms,helvetica,sans-serif;'>Email:{2}</span></p> <p style='margin-left: 40px;'> <span style='font-family:trebuchet ms,helvetica,sans-serif;'>Télefono:{3}</span></p> <p style='margin-left: 40px;'> <span style='font-family:trebuchet ms,helvetica,sans-serif;'>Celular:{4}</span></p> <p style='margin-left: 40px;'> <span style='font-family:trebuchet ms,helvetica,sans-serif;'>Mensaje:{5}</span></p> <p> <span style='font-family:trebuchet ms,helvetica,sans-serif;'>La misma cuenta con el siguiente detalle de productos a cotizar:</span></p> <table border='1' cellpadding='1' cellspacing='1' style='width: 500px;'> <thead> <tr> <th scope='col'> <span style='font-family:trebuchet ms,helvetica,sans-serif;'>Producto</span></th> <th scope='col'> <span style='font-family:trebuchet ms,helvetica,sans-serif;'>Cantidad</span></th> </tr> </thead> <tbody> {6} </tbody> </table></body> </html>"; return string.Format(Cotiza, cotDet.Id, cotDet.Cotizacion.nombre, cotDet.Cotizacion.correo, cotDet.Cotizacion.telefono, cotDet.Cotizacion.celular, cotDet.Cotizacion.mensaje, table); }
public ActionResult Create(FormCollection form) { try { CotizacionMessage cotMessage = new CotizacionMessage(); ; if (ModelState.IsValid) { var detalle = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelCotiza>(form["datainfo"]); var cotDet = new List<COD_CotizacionDetalle>(); COT_Cotizaciones cot = new COT_Cotizaciones() { COT_Celular = detalle.celular, COT_Email = detalle.correo, COT_Descripcion = detalle.descripcion, COT_Mensaje = detalle.mensaje, COT_Telefono = detalle.telefono, COT_Nombre = detalle.nombre, COT_Fecha = System.DateTime.Now, STS_Id = 1, }; using (TransactionScope transaction = new TransactionScope()) { db.COT_Cotizaciones.Add(cot); var savecot = db.SaveChanges<COT_Cotizaciones>(cot); detalle.cotiza.ForEach(c => cotDet.Add(new COD_CotizacionDetalle { COD_Cantidad = int.Parse(c.prodqty), PRO_Id = int.Parse(c.prodid), COD_Id = savecot.COT_Id })); cotDet.ForEach(c => db.SaveChanges<COD_CotizacionDetalle>(c)); transaction.Complete(); cotMessage = new CotizacionMessage { Cotizacion = detalle, Id = savecot.COT_Id }; } Task.Factory.StartNew(() => { SendMessage(cotMessage); }); return Json(new { success = true }); } return null; } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { return Json(new { success = "validation" }); } catch (Exception ex) { return Json(new { success = false }); } }
public async void SendMessage(CotizacionMessage cotDet) { System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); SmtpClient smtpClient = new SmtpClient(); if (cotDet != null) { var correos = System.Configuration.ConfigurationManager.AppSettings["Correos"].Split(';').ToList(); var from = System.Configuration.ConfigurationManager.AppSettings["From"]; correos.ForEach(c => mailMessage.To.Add(new MailAddress(c))); mailMessage.From = new MailAddress(from); mailMessage.Subject = "Nueva cotización creada en Sitio Web"; mailMessage.Body = GetHtmlMessage(cotDet); mailMessage.IsBodyHtml = true; smtpClient.EnableSsl = true; } await smtpClient.SendMailAsync(mailMessage); }