Funcionalidad de mardar mails hecha
This commit is contained in:
@@ -20,12 +20,119 @@ namespace Informes
|
||||
get { return instance; }
|
||||
}
|
||||
|
||||
private InformeEmail()
|
||||
public string EnviarEmailFactura(string titulo, List<DetalleFactura> desc)
|
||||
{
|
||||
string body = GenerarTablaDetalle(desc);
|
||||
return SendMail(titulo, body);
|
||||
|
||||
}
|
||||
public string EnviarEmailFacturas(string titulo, List<Factura> facturas)
|
||||
{
|
||||
string body = GenerarTablaFacturas(facturas);
|
||||
return SendMail(titulo, body);
|
||||
}
|
||||
|
||||
public string EnviarEmailFactura(string titulo, List<DetalleFactura> desc)
|
||||
private string GenerarTablaFacturas(List<Factura> facturas)
|
||||
{
|
||||
string body = @"
|
||||
<style>
|
||||
table, td, th {
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
";
|
||||
foreach (var item in facturas)
|
||||
{
|
||||
body += $"""
|
||||
<hr>
|
||||
<h3>Factura del {item.Fecha}, cliente: {item.NombreCliente}</h3>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Producto</td>
|
||||
<th>Cantidad</td>
|
||||
<th>Precio CU</td>
|
||||
<th>Subtotal</td>
|
||||
</tr>
|
||||
""";
|
||||
foreach (var i in item.Detalles)
|
||||
{
|
||||
body += $"""
|
||||
<tr>
|
||||
<td>{i.Producto.Nombre}</td>
|
||||
<td>{i.Cantidad}</td>
|
||||
<td>{i.Producto.Precio}</td>
|
||||
<td>{i.Producto.Precio * i.Cantidad}</td>
|
||||
</tr>
|
||||
""";
|
||||
}
|
||||
body += $"""
|
||||
<tr>
|
||||
<td>Total</td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td>{CalcularTotal(item.Detalles)}</td>
|
||||
</tr>
|
||||
</table>
|
||||
""";
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
private string GenerarTablaDetalle(List<DetalleFactura> desc)
|
||||
{
|
||||
// Esta seccion es el armado del mail html
|
||||
string body =
|
||||
@"
|
||||
<style>
|
||||
table, td, th {
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Producto</td>
|
||||
<th>Cantidad</td>
|
||||
<th>Precio CU</td>
|
||||
<th>Subtotal</td>
|
||||
</tr>";
|
||||
|
||||
foreach (var i in desc)
|
||||
{
|
||||
body +=
|
||||
@$"
|
||||
<tr>
|
||||
<td>{i.Producto.Nombre}</td>
|
||||
<td>{i.Cantidad}</td>
|
||||
<td>{i.Producto.Precio}</td>
|
||||
<td>{i.Producto.Precio * i.Cantidad}</td>
|
||||
</tr>";
|
||||
}
|
||||
|
||||
body +=
|
||||
@$"
|
||||
<tr>
|
||||
<td>Total</td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td>{CalcularTotal(desc)}</td>
|
||||
</tr>
|
||||
</table>";
|
||||
|
||||
return body;
|
||||
}
|
||||
private double CalcularTotal(List<DetalleFactura> desc)
|
||||
{
|
||||
double total = 0;
|
||||
foreach (var i in desc)
|
||||
{
|
||||
total += i.Producto.Precio * i.Cantidad;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private string SendMail(string titulo, string body)
|
||||
{
|
||||
string? ret = null;
|
||||
string json;
|
||||
@@ -58,16 +165,13 @@ namespace Informes
|
||||
MailMessage mail = new MailMessage();
|
||||
mail.Subject = titulo;
|
||||
mail.IsBodyHtml = true;
|
||||
mail.Body = GenerarTabla(desc);
|
||||
mail.Body = body;
|
||||
mail.Sender = new MailAddress(config.EmailAddr);
|
||||
foreach (var i in config.EmailTarget) mail.To.Add(i);
|
||||
mail.From = new MailAddress(config.EmailAddr);
|
||||
|
||||
try
|
||||
{
|
||||
#if DEBUG
|
||||
Console.WriteLine($"From: {config.EmailAddr}, Title: {titulo}");
|
||||
#endif
|
||||
smtp.Send(mail);
|
||||
mail.Dispose();
|
||||
}
|
||||
@@ -76,65 +180,11 @@ namespace Informes
|
||||
ret = "No se pudo comunicar con el server SMTP";
|
||||
throw;
|
||||
}
|
||||
|
||||
//Decimos que se envio el email correctamente si el valor del retorno sigue siendo nulo en otro caso tendra la descripcion del error.
|
||||
return (ret == null) ?
|
||||
"Se envio el Email Correctamente":
|
||||
"Se envio el Email Correctamente" :
|
||||
ret;
|
||||
//
|
||||
}
|
||||
|
||||
private string GenerarTabla(List<DetalleFactura> desc)
|
||||
{
|
||||
// Esta seccion es el armado del mail html
|
||||
string body =
|
||||
@"
|
||||
<style>
|
||||
table, td, th {
|
||||
border: 1px solid black;
|
||||
}
|
||||
</style>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Producto</td>
|
||||
<th>Cantidad</td>
|
||||
<th>Precio CU</td>
|
||||
<th>Subtotal</td>
|
||||
</tr>";
|
||||
|
||||
foreach (var i in desc)
|
||||
{
|
||||
body +=
|
||||
@$"
|
||||
<tr>
|
||||
<td>{i.Producto.Nombre}</td>
|
||||
<td>{i.Cantidad}</td>
|
||||
<td>{i.Producto.Precio}</td>
|
||||
<td>{i.Producto.Precio * i.Cantidad}</td>
|
||||
</tr>";
|
||||
}
|
||||
|
||||
body +=
|
||||
@$"
|
||||
<tr>
|
||||
<td>Total</td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td>{CalcularTotal(desc)}</td>
|
||||
</tr>
|
||||
</table>
|
||||
";
|
||||
return body;
|
||||
}
|
||||
private double CalcularTotal(List<DetalleFactura> desc)
|
||||
{
|
||||
double total = 0;
|
||||
foreach (var i in desc)
|
||||
{
|
||||
total += i.Producto.Precio * i.Cantidad;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user