123 lines
8.0 KiB
C#
123 lines
8.0 KiB
C#
using bdGrupoSanchoToro;
|
|
using Microsoft.VisualBasic;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Mail;
|
|
using System.Net.Mime;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using tsUtilidades.Extensiones;
|
|
|
|
namespace ServicioGrupoSanchoToro
|
|
{
|
|
internal class ProcesosCorreos
|
|
{
|
|
internal static void EnviaCorreos()
|
|
{
|
|
try
|
|
{
|
|
var bd = tscGrupoSanchoToro.NuevoContexto();
|
|
var correospendientes = bd.correos.Where(x => x.FechaAnulacion == null && x.FechaEnvio == null && x.idcuenta != null).ToList();
|
|
ArrayList ficherosadjuntos = null/* TODO Change to default(_) if this is not a reference type */;
|
|
bool bIntentar;
|
|
var EmailEnPruebas = bd.enumeraciones.First(x => x.Codigo == "CONF.EMAILENPRUEBAS").ValorAlfabeticoLargo;
|
|
var EmailCopiaOculta = bd.enumeraciones.First(x => x.Codigo == "CONF.EMAILCOPIAOCULTA").ValorAlfabeticoLargo;
|
|
try
|
|
{
|
|
foreach (var email in correospendientes)
|
|
{
|
|
email.Asunto = email.Asunto.Replace(Environment.NewLine, " ");
|
|
bIntentar = false;
|
|
if (email.FechaUltimoIntento == null)
|
|
bIntentar = true;
|
|
else if (DateTime.UtcNow.Subtract(email.FechaUltimoIntento.Value).TotalHours > 1)
|
|
bIntentar = true;
|
|
if (bIntentar)
|
|
{
|
|
try
|
|
{
|
|
string sPasswd = email.idcuentaNavigation.Password;
|
|
if (System.IO.Directory.Exists(@"c:\tecnosis.tfs"))
|
|
email.Destinatario = "manmog@gmail.com";
|
|
else if (!string.IsNullOrEmpty(EmailEnPruebas))
|
|
{
|
|
email.Destinatario = EmailEnPruebas;
|
|
email.CopiaOculta = "manmog@gmail.com";
|
|
}
|
|
else if (EmailCopiaOculta != null && EmailCopiaOculta != "" && email.CodigoAplicacion != "ERRORES")
|
|
{
|
|
if (string.IsNullOrEmpty(email.CopiaOculta))
|
|
email.CopiaOculta = EmailCopiaOculta;
|
|
else
|
|
email.CopiaOculta = EmailCopiaOculta + ";" + EmailCopiaOculta;
|
|
}
|
|
// End If
|
|
List<Attachment> attachments = new List<Attachment>();
|
|
string sCuerpoHtml = System.Text.Encoding.UTF8.GetString(bd.ficherosconfiguracion.Where(x => x.Codigo == "PLANTILLA_CORREOS_EMP_" + email.idcuentaNavigation.idEmpresa.ToString()).First().Configuracion);
|
|
// Dim sCuerpoHtml As String = System.Text.Encoding.UTF8.GetString(IO.File.ReadAllBytes("c:\tmp\PlantillaCorreos.html"))
|
|
sCuerpoHtml = sCuerpoHtml.Replace("$cuerpo$", email.Cuerpo).Replace(Constants.vbCrLf, "<br>");
|
|
|
|
AlternateView avHtml = AlternateView.CreateAlternateViewFromString(sCuerpoHtml, null/* TODO Change to default(_) if this is not a reference type */, MediaTypeNames.Text.Html);
|
|
List<Attachment> ats = null;
|
|
if (!string.IsNullOrEmpty(email.RutaFicheroAdjunto))
|
|
{
|
|
ats = new List<Attachment>();
|
|
Attachment at = new Attachment(new MemoryStream(File.ReadAllBytes(email.RutaFicheroAdjunto)), System.IO.Path.GetFileName(email.RutaFicheroAdjunto));
|
|
ats.Add(at);
|
|
}
|
|
if (email.idFicheroAdjunto.HasValue)
|
|
{
|
|
if (ats == null)
|
|
ats = new List<Attachment>();
|
|
Attachment at = new Attachment(new MemoryStream(email.idFicheroAdjuntoNavigation.Fichero), System.IO.Path.GetFileName(email.idFicheroAdjuntoNavigation.NombreFichero));
|
|
ats.Add(at);
|
|
}
|
|
foreach (var fad in email.ficherosadjuntos)
|
|
{
|
|
if (ats == null)
|
|
ats = new List<Attachment>();
|
|
Attachment at = new Attachment(new MemoryStream(fad.idFicheroNavigation.Fichero), System.IO.Path.GetFileName(fad.idFicheroNavigation.NombreFichero));
|
|
ats.Add(at);
|
|
}
|
|
List<AlternateView> avs = new List<AlternateView>();
|
|
avs.Add(avHtml);
|
|
tsCorreos.Funciones.EnviaCorreo(email.idcuentaNavigation.ServidorSMTP, email.Remitente, email.Destinatario, email.Asunto, sCuerpoHtml, ats, avs, email.Copia, email.CopiaOculta, email.idcuentaNavigation.CuentaCorreo, email.idcuentaNavigation.Password, (int)email.idcuentaNavigation.Puerto, email.idcuentaNavigation.SSL, true, email.idcuentaNavigation.ResponderA);
|
|
// tsUtilidades.Correo.Funciones.EnviaCorreo(email.cuentascorreo.ServidorSMTP, email.Remitente, email.Destinatario, email.Asunto, email.Cuerpo, attachments, avs, , , email.cuentascorreo.CuentaCorreo, sPasswd, email.cuentascorreo.Puerto, email.cuentascorreo.SSL, True)
|
|
email.FechaEnvio = DateTime.UtcNow;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Call AñadeLog(Enumeraciones.TipoLog.Advertencia, "El correo para el destinatario " & email.DESTINATARIO & " ha sido rechazado durante 48 horas y será marcado como anulado.", "Detalle del error: " & vbCrLf & ex.Message, ex)
|
|
email.FechaUltimoIntento = DateTime.UtcNow;
|
|
email.MensajeError = ex.Message.Acortar(512);
|
|
if (DateTime.UtcNow.Subtract(email.FechaCreacion.Value).TotalHours > 48)
|
|
{
|
|
email.FechaAnulacion = DateTime.UtcNow;
|
|
// GeneraRegistroCorreoExcepcion(Nothing, ex, "Procesar")
|
|
Logs.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.Fallo, "El correo para el destinatario " + email.Destinatario + " ha sido rechazado durante 48 horas y será marcado como anulado.", "Detalle del error: " + Constants.vbCrLf + ex.Message, ex);
|
|
}
|
|
else
|
|
{
|
|
string sFicheroLog = Procesos.Conf.DirectorioLogs + @"Errores\Errores-" + DateTime.UtcNow.Year.ToString() + DateTime.UtcNow.Month.ToString() + ".txt";
|
|
Logs.Anadelogtxt("Error en EnviaCorreo. " + ex.Message + Constants.vbCrLf + ex.StackTrace, sFicheroLog);
|
|
}
|
|
}
|
|
}
|
|
bd.SaveChanges();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Dim sFicheroLog = DirectorioLogs & "Errores-" & Now.Year.ToString & Now.Month.ToString.PadLeft(2, "0") & ".txt"
|
|
Logs.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.Fallo, ex.Message, "Detalle del error: " + Constants.vbCrLf + ex.Message, ex);
|
|
}
|
|
}
|
|
}
|
|
}
|