117 lines
6.4 KiB
C#
117 lines
6.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Mail;
|
|
using System.Net.Mime;
|
|
|
|
namespace tsCorreos
|
|
{
|
|
|
|
public static class Funciones
|
|
{
|
|
|
|
public static async void EnviaCorreo(string ServidorSMTP, string Remitente, string Destinatario, string Asunto, string Cuerpo, List<Attachment> AttachMents = null, List<AlternateView> AlternateViews = null, string CC = "", string BCC = "", string CuentaCorreo = "", string ContraseñaCorreo = "", int Puerto = 25, bool UsarSSL = false, bool CuerpoenHTML = false, string ResponderA = "", bool CredencialesConDominio = false, SecurityProtocolType ProtocoloSeguridad = SecurityProtocolType.Tls, string NombreRemitente = "")
|
|
{
|
|
try
|
|
{
|
|
if (ServidorSMTP == "smtp.gmail.com")
|
|
{
|
|
var tkgm = await TokenGmail.ObtieneTokenAsync(Remitente);
|
|
await GmailOAuthSender.EnviarCorreoAsync(tkgm.Config, tkgm.Token, Destinatario, Asunto, Cuerpo, CuerpoenHTML, CC, BCC, ResponderA, AttachMents);
|
|
}
|
|
else
|
|
{
|
|
FuncionesInternas.EnviaCorreo(ServidorSMTP, Remitente, Destinatario, Asunto, Cuerpo, AttachMents, AlternateViews, CC, BCC, CuentaCorreo, ContraseñaCorreo, Puerto, UsarSSL, CuerpoenHTML, ResponderA, CredencialesConDominio, ProtocoloSeguridad, NombreRemitente);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message, ex);
|
|
}
|
|
}
|
|
public static async void EnviaCorreoAL(string ServidorSMTP, string Remitente, string Destinatario, string Asunto, string Cuerpo, ArrayList FicherosAdjuntos = null, string CC = "", string BCC = "", string CuentaCorreo = "", string ContraseñaCorreo = "", int Puerto = 25, bool UsarSSL = false, string NombreRemitente = "")
|
|
{
|
|
try
|
|
{
|
|
if (ServidorSMTP == "smtp.gmail.com")
|
|
{
|
|
var tkgm = await TokenGmail.ObtieneTokenAsync(Remitente);
|
|
await GmailOAuthSender.EnviarCorreoALAsync(tkgm.Config, tkgm.Token, Destinatario, Asunto, Cuerpo, false, CC, BCC, adjuntos: FicherosAdjuntos);
|
|
}
|
|
else
|
|
{
|
|
FuncionesInternas.EnviaCorreoAL(ServidorSMTP, Remitente, Destinatario, Asunto, Cuerpo, FicherosAdjuntos, CC, BCC, CuentaCorreo, ContraseñaCorreo, Puerto, UsarSSL, NombreRemitente);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message, ex);
|
|
}
|
|
|
|
}
|
|
public static void EnviaCorreoCompruebaHTML(string ServidorSMTP, string Remitente, string Destinatario, string Asunto, string Cuerpo, MemoryStream[] FicherosAdjuntos, string[] NombreFicherosAdjuntos, string CC = "", string BCC = "", string CuentaCorreo = "", string ContraseñaCorreo = "", int Puerto = 25, bool UsarSSL = false, bool CuerpoenHTML = false, string ResponderA = "", string NombreRemitente = "")
|
|
{
|
|
try
|
|
{
|
|
if (FicherosAdjuntos.Count() == 1 && NombreFicherosAdjuntos[0].EndsWith(".html.zip"))
|
|
{
|
|
string sDirectorioTMP = tsCorreos.Utilidades.ObtieneDirectorioAleatorio();
|
|
tsZip.Zip.ExtraeTodoDeZip(FicherosAdjuntos[0], sDirectorioTMP);
|
|
// tsZIP.zip.ExtraeTodoDeZip("f:\temp\csc.html.zip", sDirectorioTMP)
|
|
string sFichCuerpo = Directory.GetFiles(sDirectorioTMP, "*.html")[0];
|
|
string sCuerpo = System.Text.Encoding.UTF8.GetString(File.ReadAllBytes(sFichCuerpo));
|
|
var avHtml = AlternateView.CreateAlternateViewFromString(sCuerpo, null, MediaTypeNames.Text.Html);
|
|
string diradj = Directory.GetDirectories(sDirectorioTMP)[0];
|
|
string[] ficadj = Directory.GetFiles(diradj);
|
|
foreach (var f in ficadj)
|
|
{
|
|
var ms = new MemoryStream(File.ReadAllBytes(f));
|
|
var inline = new LinkedResource(ms, "image/" + Path.GetExtension(f).Trim('.'));
|
|
inline.ContentId = Path.GetFileNameWithoutExtension(f);
|
|
avHtml.LinkedResources.Add(inline);
|
|
}
|
|
var avs = new List<AlternateView>();
|
|
avs.Add(avHtml);
|
|
FuncionesInternas.EnviaCorreoHtml(ServidorSMTP, Remitente, Destinatario, Asunto, Cuerpo, null, avs, CC, BCC, CuentaCorreo, ContraseñaCorreo, Puerto, UsarSSL, true, ResponderA, NombreRemitente);
|
|
Directory.Delete(sDirectorioTMP, true);
|
|
}
|
|
else
|
|
{
|
|
EnviaCorreoMS(ServidorSMTP, Remitente, Destinatario, Asunto, Cuerpo, FicherosAdjuntos, NombreFicherosAdjuntos, CC, BCC, CuentaCorreo, ContraseñaCorreo, Puerto, UsarSSL, CuerpoenHTML, ResponderA, NombreRemitente);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message, ex);
|
|
}
|
|
|
|
}
|
|
|
|
public static async void EnviaCorreoMS(string ServidorSMTP, string Remitente, string Destinatario, string Asunto, string Cuerpo, MemoryStream[] FicherosAdjuntos, string[] NombreFicherosAdjuntos, string CC = "", string BCC = "", string CuentaCorreo = "", string ContraseñaCorreo = "", int Puerto = 25, bool UsarSSL = false, bool CuerpoenHTML = false, string ResponderA = "", string NombreRemitente = "")
|
|
{
|
|
|
|
|
|
try
|
|
{
|
|
if (ServidorSMTP == "smtp.gmail.com")
|
|
{
|
|
var tkgm = await TokenGmail.ObtieneTokenAsync(Remitente);
|
|
await GmailOAuthSender.EnviarCorreoMSAsync(tkgm.Config, tkgm.Token, Destinatario, Asunto, Cuerpo, false, CC, BCC,ResponderA, FicherosAdjuntos,NombreFicherosAdjuntos,ServidorSMTP,Puerto);
|
|
}
|
|
else
|
|
{
|
|
FuncionesInternas.EnviaCorreoMS(ServidorSMTP, Remitente, Destinatario, Asunto, Cuerpo, FicherosAdjuntos, NombreFicherosAdjuntos, CC, BCC, CuentaCorreo, ContraseñaCorreo, Puerto, UsarSSL, CuerpoenHTML,ResponderA, NombreRemitente);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message, ex);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
} |