821 lines
37 KiB
C#
821 lines
37 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Mail;
|
|
using System.Net.Mime;
|
|
using System.Net.Security;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
namespace TsCorreos
|
|
{
|
|
|
|
internal class FuncionesInternas
|
|
{
|
|
internal 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 = "")
|
|
{
|
|
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);
|
|
EnviaCorreoHtml(ServidorSMTP, Remitente, Destinatario, Asunto, Cuerpo, null, avs, CC, BCC, CuentaCorreo, ContraseñaCorreo, Puerto, UsarSSL, true, ResponderA);
|
|
Directory.Delete(sDirectorioTMP, true);
|
|
}
|
|
else
|
|
{
|
|
EnviaCorreoMS(ServidorSMTP, Remitente, Destinatario, Asunto, Cuerpo, FicherosAdjuntos, NombreFicherosAdjuntos, CC, BCC, CuentaCorreo, ContraseñaCorreo, Puerto, UsarSSL, CuerpoenHTML, ResponderA);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
internal static void EnviaCorreoHtml(string ServidorSMTP, string Remitente, string Destinatario, string Asunto, string Cuerpo, List<Attachment> AttachMents, List<AlternateView> AlternateViews, string CC = "", string BCC = "", string CuentaCorreo = "", string ContraseñaCorreo = "", int Puerto = 25, bool UsarSSL = false, bool CuerpoenHTML = false, string ResponderA = "",string NombreRemitente="")
|
|
{
|
|
try
|
|
{
|
|
if (NombreRemitente==null) NombreRemitente=Remitente;
|
|
SmtpClient SmtpMail;
|
|
MailMessage myMessage;
|
|
|
|
|
|
// Si es alguna de las máquinas de desarrollo de danmun, el correo se envía solamente a danmun. Son pruebas.
|
|
if (Environment.MachineName == "WIN81PDDANMUN" || (Environment.MachineName.ToUpper() ?? "") == ("INTI81".ToUpper() ?? "") || (Environment.MachineName.ToUpper() ?? "") == ("INTI10".ToUpper() ?? ""))
|
|
{
|
|
Destinatario = "danmun@tecnosis.eu";
|
|
}
|
|
|
|
Asunto = Asunto.Replace(Environment.NewLine, " ");
|
|
// myMessage = New MailMessage(Remitente, Destinatario, Asunto, Cuerpo)
|
|
myMessage = new MailMessage();
|
|
myMessage.Body = Cuerpo;
|
|
myMessage.Subject = Asunto;
|
|
string[] destinatarios = Destinatario.Split(';');
|
|
foreach (var currentDestinatario in destinatarios)
|
|
{
|
|
Destinatario = currentDestinatario;
|
|
myMessage.To.Add(new MailAddress(Destinatario.Trim(), Destinatario.Trim()));
|
|
}
|
|
myMessage.BodyEncoding = System.Text.Encoding.Default;
|
|
if (ResponderA is null || string.IsNullOrWhiteSpace(ResponderA))
|
|
{
|
|
myMessage.ReplyToList.Add(new MailAddress(Remitente, NombreRemitente));
|
|
}
|
|
else
|
|
{
|
|
myMessage.ReplyToList.Add(new MailAddress(ResponderA, ResponderA));
|
|
myMessage.ReplyToList.Add(new MailAddress(Remitente, NombreRemitente));
|
|
}
|
|
myMessage.Sender = new MailAddress(Remitente, NombreRemitente);
|
|
myMessage.From = new MailAddress(Remitente, NombreRemitente);
|
|
myMessage.IsBodyHtml = CuerpoenHTML;
|
|
if (!string.IsNullOrEmpty(CC))
|
|
{
|
|
foreach (var scc in CC.Split(';'))
|
|
myMessage.CC.Add(scc);
|
|
}
|
|
if (!string.IsNullOrEmpty(BCC))
|
|
{
|
|
foreach (var sbcc in BCC.Split(';'))
|
|
myMessage.Bcc.Add(sbcc);
|
|
}
|
|
if (AttachMents != null)
|
|
{
|
|
foreach (var att in AttachMents)
|
|
myMessage.Attachments.Add(att);
|
|
}
|
|
if (AlternateViews != null)
|
|
{
|
|
foreach (var av in AlternateViews)
|
|
myMessage.AlternateViews.Add(av);
|
|
}
|
|
SmtpMail = new SmtpClient();
|
|
|
|
if (!string.IsNullOrEmpty(ServidorSMTP))
|
|
SmtpMail.Host = ServidorSMTP;
|
|
SmtpMail.Port = Puerto;
|
|
if (!string.IsNullOrEmpty(CuentaCorreo))
|
|
{
|
|
SmtpMail.Credentials = new NetworkCredential(CuentaCorreo, ContraseñaCorreo);
|
|
}
|
|
SmtpMail.EnableSsl = UsarSSL;
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|
SmtpMail.Send(myMessage);
|
|
}
|
|
|
|
|
|
catch (Exception myexp)
|
|
{
|
|
throw new Exception(myexp.Message, myexp);
|
|
}
|
|
}
|
|
|
|
internal static void EnviaCorreo(string ServidorSMTP, string Remitente, string Destinatario, string Asunto, string Cuerpo, List<Attachment> AttachMents, List<AlternateView> AlternateViews, 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
|
|
{
|
|
|
|
SmtpClient SmtpMail;
|
|
MailMessage myMessage;
|
|
|
|
if (string.IsNullOrEmpty(NombreRemitente)) NombreRemitente=Remitente;
|
|
if (string.IsNullOrEmpty(Destinatario) & !string.IsNullOrEmpty(CC))
|
|
{
|
|
Destinatario = CC;
|
|
CC = "";
|
|
}
|
|
Asunto = Asunto.Replace(Environment.NewLine, " ");
|
|
// myMessage = New MailMessage(Remitente, Destinatario, Asunto, Cuerpo)
|
|
myMessage = new MailMessage();
|
|
myMessage.Subject = Asunto;
|
|
myMessage.Body = Cuerpo;
|
|
myMessage.From = new MailAddress(Remitente);
|
|
string[] sDestinatarios = null;
|
|
sDestinatarios = Destinatario.Split(';');
|
|
foreach (var dest in sDestinatarios)
|
|
{
|
|
if (!string.IsNullOrEmpty(dest.Trim()))
|
|
{
|
|
myMessage.To.Add(dest.Trim());
|
|
}
|
|
}
|
|
|
|
myMessage.BodyEncoding = System.Text.Encoding.Default;
|
|
if (ResponderA is null || string.IsNullOrWhiteSpace(ResponderA))
|
|
{
|
|
myMessage.ReplyToList.Add(new MailAddress(Remitente, NombreRemitente));
|
|
}
|
|
else
|
|
{
|
|
myMessage.ReplyToList.Add(new MailAddress(ResponderA, ResponderA));
|
|
myMessage.ReplyToList.Add(new MailAddress(Remitente, NombreRemitente));
|
|
}
|
|
myMessage.Sender = new MailAddress(Remitente, NombreRemitente);
|
|
myMessage.From = new MailAddress(Remitente, NombreRemitente);
|
|
myMessage.IsBodyHtml = CuerpoenHTML;
|
|
|
|
if (!string.IsNullOrEmpty(CC))
|
|
{
|
|
string[] scc = CC.Split(';');
|
|
foreach (var c in scc)
|
|
{
|
|
if (!string.IsNullOrEmpty(c))
|
|
myMessage.CC.Add(c);
|
|
}
|
|
}
|
|
if (!string.IsNullOrEmpty(BCC))
|
|
{
|
|
string[] sbcc = BCC.Split(';');
|
|
foreach (var b in sbcc)
|
|
{
|
|
if (!string.IsNullOrEmpty(b))
|
|
myMessage.Bcc.Add(b);
|
|
}
|
|
}
|
|
if (AttachMents != null)
|
|
{
|
|
foreach (var att in AttachMents)
|
|
myMessage.Attachments.Add(att);
|
|
}
|
|
if (AlternateViews != null)
|
|
{
|
|
foreach (var av in AlternateViews)
|
|
myMessage.AlternateViews.Add(av);
|
|
}
|
|
SmtpMail = new SmtpClient();
|
|
|
|
if (!string.IsNullOrEmpty(ServidorSMTP))
|
|
SmtpMail.Host = ServidorSMTP;
|
|
SmtpMail.Port = Puerto;
|
|
if (!string.IsNullOrEmpty(CuentaCorreo))
|
|
{
|
|
if (CredencialesConDominio)
|
|
{
|
|
SmtpMail.Credentials = new NetworkCredential(CuentaCorreo, ContraseñaCorreo, CuentaCorreo.Split('@')[1]);
|
|
}
|
|
else
|
|
{
|
|
SmtpMail.Credentials = new NetworkCredential(CuentaCorreo, ContraseñaCorreo);
|
|
}
|
|
}
|
|
SmtpMail.EnableSsl = UsarSSL;
|
|
// SmtpMail.TargetName = "STARTTLS/smtp.office365.com"
|
|
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls OrElse SecurityProtocolType.Tls11 OrElse SecurityProtocolType.Tls12 OrElse SecurityProtocolType.Tls13
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
// ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|
ServicePointManager.ServerCertificateValidationCallback =
|
|
(sender, certificate, chain, sslPolicyErrors) => true;
|
|
SmtpMail.Send(myMessage);
|
|
}
|
|
|
|
|
|
catch (Exception myexp)
|
|
{
|
|
throw new Exception(myexp.Message, myexp);
|
|
}
|
|
}
|
|
|
|
internal static 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 (NombreRemitente == "") NombreRemitente = Remitente;
|
|
Attachment myAttch;
|
|
SmtpClient SmtpMail;
|
|
MailMessage myMessage;
|
|
int i, iCnt;
|
|
|
|
// Si es alguna de las máquinas de desarrollo de danmun, el correo se envía solamente a danmun. Son pruebas.
|
|
if (Environment.MachineName == "WIN81PDDANMUN" || (Environment.MachineName.ToUpper() ?? "") == ("INTI81".ToUpper() ?? "") || (Environment.MachineName.ToUpper() ?? "") == ("INTI10".ToUpper() ?? ""))
|
|
{
|
|
Destinatario = "danmun@tecnosis.eu";
|
|
}
|
|
|
|
Asunto = Asunto.Replace(Environment.NewLine, " ");
|
|
myMessage = new MailMessage();
|
|
|
|
|
|
myMessage = new MailMessage();
|
|
myMessage.Body = Cuerpo;
|
|
myMessage.Subject = Asunto;
|
|
string[] destinatarios = Destinatario.Split(';');
|
|
foreach (var currentDestinatario in destinatarios)
|
|
{
|
|
Destinatario = currentDestinatario;
|
|
myMessage.To.Add(new MailAddress(Destinatario.Trim(), Destinatario.Trim()));
|
|
}
|
|
|
|
|
|
|
|
myMessage.BodyEncoding = System.Text.Encoding.Default;
|
|
if (ResponderA is null || string.IsNullOrWhiteSpace(ResponderA))
|
|
{
|
|
myMessage.ReplyToList.Add(new MailAddress(Remitente, NombreRemitente));
|
|
}
|
|
else
|
|
{
|
|
myMessage.ReplyToList.Add(new MailAddress(ResponderA, ResponderA));
|
|
myMessage.ReplyToList.Add(new MailAddress(Remitente, NombreRemitente));
|
|
}
|
|
myMessage.Sender = new MailAddress(Remitente, NombreRemitente);
|
|
myMessage.From = new MailAddress(Remitente, NombreRemitente);
|
|
myMessage.IsBodyHtml = CuerpoenHTML;
|
|
|
|
if (!string.IsNullOrEmpty(CC))
|
|
{
|
|
foreach (var scc in CC.Split(';'))
|
|
myMessage.CC.Add(scc);
|
|
}
|
|
if (!string.IsNullOrEmpty(BCC))
|
|
{
|
|
foreach (var sbcc in BCC.Split(';'))
|
|
myMessage.Bcc.Add(sbcc);
|
|
}
|
|
if (FicherosAdjuntos != null)
|
|
{
|
|
iCnt = FicherosAdjuntos.Count() - 1;
|
|
var loopTo = iCnt;
|
|
for (i = 0; i <= loopTo; i++)
|
|
{
|
|
myAttch = new Attachment(FicherosAdjuntos[i], NombreFicherosAdjuntos[i]);
|
|
myMessage.Attachments.Add(myAttch);
|
|
}
|
|
}
|
|
|
|
SmtpMail = new SmtpClient();
|
|
|
|
if (!string.IsNullOrEmpty(ServidorSMTP))
|
|
SmtpMail.Host = ServidorSMTP;
|
|
SmtpMail.Port = Puerto;
|
|
if (!string.IsNullOrEmpty(CuentaCorreo))
|
|
{
|
|
SmtpMail.UseDefaultCredentials = false;
|
|
SmtpMail.Credentials = new NetworkCredential(CuentaCorreo, ContraseñaCorreo);
|
|
}
|
|
SmtpMail.EnableSsl = UsarSSL;
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
// ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|
ServicePointManager.ServerCertificateValidationCallback =
|
|
(sender, certificate, chain, sslPolicyErrors) => true;
|
|
SmtpMail.Send(myMessage);
|
|
}
|
|
|
|
|
|
catch (Exception myexp)
|
|
{
|
|
throw new Exception(myexp.Message, myexp);
|
|
}
|
|
}
|
|
internal static void EnviaCorreo(string ServidorSMTP, string Remitente, string Destinatario, string Asunto, string Cuerpo, List<string> FicherosAdjuntos, string CC = "", string BCC = "", string CuentaCorreo = "", string ContraseñaCorreo = "", int Puerto = 25, bool UsarSSL = false, bool CuerpoenHTML = false, string ResponderA = "")
|
|
{
|
|
try
|
|
{
|
|
Attachment myAttch;
|
|
SmtpClient SmtpMail;
|
|
MailMessage myMessage;
|
|
int i, iCnt;
|
|
|
|
// Si es alguna de kas máquinas de desarrollo de danmun, el correo se envía solamente a danmun. Son pruebas.
|
|
// If Environment.MachineName = "WIN81PDDANMUN" OrElse Environment.MachineName.ToUpper = "INTI81".ToUpper OrElse Environment.MachineName.ToUpper = "INTI10".ToUpper Then
|
|
// Destinatario = "danmun@tecnosis.eu"
|
|
// End If
|
|
|
|
Asunto = Asunto.Replace(Environment.NewLine, " ");
|
|
myMessage = new MailMessage(Remitente, Destinatario, Asunto, Cuerpo);
|
|
myMessage.BodyEncoding = System.Text.Encoding.Default;
|
|
if (ResponderA is null || string.IsNullOrWhiteSpace(ResponderA))
|
|
{
|
|
myMessage.ReplyToList.Add(new MailAddress(Remitente, Remitente));
|
|
}
|
|
else
|
|
{
|
|
myMessage.ReplyToList.Add(new MailAddress(ResponderA, ResponderA));
|
|
myMessage.ReplyToList.Add(new MailAddress(Remitente, Remitente));
|
|
}
|
|
myMessage.Sender = new MailAddress(Remitente, Remitente);
|
|
myMessage.From = new MailAddress(Remitente, Remitente);
|
|
|
|
|
|
if (!string.IsNullOrEmpty(CC))
|
|
{
|
|
myMessage.CC.Add(CC);
|
|
}
|
|
if (!string.IsNullOrEmpty(BCC))
|
|
{
|
|
myMessage.Bcc.Add(BCC);
|
|
}
|
|
if (FicherosAdjuntos != null)
|
|
{
|
|
iCnt = FicherosAdjuntos.Count - 1;
|
|
var loopTo = iCnt;
|
|
for (i = 0; i <= loopTo; i++)
|
|
{
|
|
if (File.Exists(FicherosAdjuntos[i]))
|
|
{
|
|
myAttch = new Attachment(FicherosAdjuntos[i]);
|
|
myMessage.Attachments.Add(myAttch);
|
|
// myAttch.Dispose()
|
|
}
|
|
}
|
|
}
|
|
|
|
SmtpMail = new SmtpClient();
|
|
|
|
if (!string.IsNullOrEmpty(ServidorSMTP))
|
|
SmtpMail.Host = ServidorSMTP;
|
|
SmtpMail.Port = Puerto;
|
|
if (!string.IsNullOrEmpty(CuentaCorreo))
|
|
{
|
|
SmtpMail.Credentials = new NetworkCredential(CuentaCorreo, ContraseñaCorreo);
|
|
}
|
|
SmtpMail.EnableSsl = UsarSSL;
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
//ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|
SmtpMail.Send(myMessage);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new Exception(e.Message, e);
|
|
}
|
|
}
|
|
|
|
internal static void EnviaCorreo(string servidorSMTP, string remitente, List<string> destinatarios, string asunto, string cuerpo, List<string> ficherosAdjuntos, string cc = "", string bcc = "", string cuentaCorreo = "", string contraseñaCorreo = "", int puerto = 25, bool usarSSL = false, bool cuerpoEsHTML = false, string responderA = "")
|
|
{
|
|
try
|
|
{
|
|
Attachment myAttch;
|
|
SmtpClient SmtpMail;
|
|
MailMessage myMessage;
|
|
int i, iCnt;
|
|
|
|
// Si es alguna de kas máquinas de desarrollo de danmun, el correo se envía solamente a danmun. Son pruebas.
|
|
// If Environment.MachineName = "WIN81PDDANMUN" OrElse Environment.MachineName.ToUpper = "INTI81".ToUpper OrElse Environment.MachineName.ToUpper = "INTI10".ToUpper Then
|
|
// Destinatario = "danmun@tecnosis.eu"
|
|
// End If
|
|
|
|
asunto = asunto.Replace(Environment.NewLine, " ");
|
|
myMessage = new MailMessage();
|
|
myMessage.Body = cuerpo;
|
|
myMessage.Subject = asunto;
|
|
foreach (var destinatario in destinatarios)
|
|
myMessage.To.Add(new MailAddress(destinatario, destinatario));
|
|
|
|
myMessage.BodyEncoding = System.Text.Encoding.Default;
|
|
if (responderA is null || string.IsNullOrWhiteSpace(responderA))
|
|
{
|
|
myMessage.ReplyToList.Add(new MailAddress(remitente, remitente));
|
|
}
|
|
else
|
|
{
|
|
myMessage.ReplyToList.Add(new MailAddress(responderA, responderA));
|
|
myMessage.ReplyToList.Add(new MailAddress(remitente, remitente));
|
|
}
|
|
myMessage.Sender = new MailAddress(remitente, remitente);
|
|
myMessage.From = new MailAddress(remitente, remitente);
|
|
|
|
|
|
if (!string.IsNullOrEmpty(cc))
|
|
{
|
|
foreach (var scc in cc.Split(';'))
|
|
myMessage.CC.Add(scc);
|
|
}
|
|
if (!string.IsNullOrEmpty(bcc))
|
|
{
|
|
foreach (var sbcc in bcc.Split(';'))
|
|
myMessage.Bcc.Add(sbcc);
|
|
}
|
|
if (ficherosAdjuntos != null)
|
|
{
|
|
iCnt = ficherosAdjuntos.Count - 1;
|
|
var loopTo = iCnt;
|
|
for (i = 0; i <= loopTo; i++)
|
|
{
|
|
if (File.Exists(ficherosAdjuntos[i]))
|
|
{
|
|
myAttch = new Attachment(ficherosAdjuntos[i]);
|
|
myMessage.Attachments.Add(myAttch);
|
|
// myAttch.Dispose()
|
|
}
|
|
}
|
|
}
|
|
|
|
SmtpMail = new SmtpClient();
|
|
|
|
if (!string.IsNullOrEmpty(servidorSMTP))
|
|
SmtpMail.Host = servidorSMTP;
|
|
SmtpMail.Port = puerto;
|
|
if (!string.IsNullOrEmpty(cuentaCorreo))
|
|
{
|
|
SmtpMail.Credentials = new NetworkCredential(cuentaCorreo, contraseñaCorreo);
|
|
}
|
|
SmtpMail.EnableSsl = usarSSL;
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|
SmtpMail.Send(myMessage);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new Exception(e.Message, e);
|
|
}
|
|
}
|
|
|
|
internal static 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 (NombreRemitente == "") NombreRemitente=Remitente;
|
|
Attachment myAttch;
|
|
SmtpClient SmtpMail;
|
|
MailMessage myMessage;
|
|
int i, iCnt;
|
|
|
|
// myMessage = New MailMessage(Remitente, Destinatario, Asunto, Cuerpo)
|
|
myMessage = new MailMessage();
|
|
myMessage.Body = Cuerpo;
|
|
myMessage.Subject = Asunto;
|
|
string[] destinatarios = Destinatario.Split(';');
|
|
foreach (var currentDestinatario in destinatarios)
|
|
{
|
|
Destinatario = currentDestinatario;
|
|
myMessage.To.Add(new MailAddress(Destinatario.Trim(), Destinatario.Trim()));
|
|
}
|
|
|
|
|
|
myMessage.BodyEncoding = System.Text.Encoding.Default;
|
|
myMessage.ReplyTo = new MailAddress(Remitente, NombreRemitente);
|
|
myMessage.Sender = new MailAddress(Remitente, NombreRemitente);
|
|
myMessage.From = new MailAddress(Remitente, NombreRemitente);
|
|
|
|
|
|
if (!string.IsNullOrEmpty(CC))
|
|
{
|
|
string[] scc = CC.Split(';');
|
|
foreach (var c in scc)
|
|
{
|
|
if (!string.IsNullOrEmpty(c))
|
|
myMessage.CC.Add(c);
|
|
}
|
|
}
|
|
if (!string.IsNullOrEmpty(BCC))
|
|
{
|
|
string[] sbcc = BCC.Split(';');
|
|
foreach (var b in sbcc)
|
|
{
|
|
if (!string.IsNullOrEmpty(b))
|
|
myMessage.Bcc.Add(b);
|
|
}
|
|
}
|
|
if (FicherosAdjuntos != null)
|
|
{
|
|
iCnt = FicherosAdjuntos.Count - 1;
|
|
var loopTo = iCnt;
|
|
for (i = 0; i <= loopTo; i++)
|
|
{
|
|
if (File.Exists(FicherosAdjuntos[i].ToString()))
|
|
{
|
|
myAttch = new Attachment(FicherosAdjuntos[i].ToString());
|
|
myMessage.Attachments.Add(myAttch);
|
|
// myAttch.Dispose()
|
|
}
|
|
}
|
|
}
|
|
|
|
SmtpMail = new SmtpClient();
|
|
|
|
if (!string.IsNullOrEmpty(ServidorSMTP))
|
|
SmtpMail.Host = ServidorSMTP;
|
|
SmtpMail.Port = Puerto;
|
|
if (!string.IsNullOrEmpty(CuentaCorreo))
|
|
{
|
|
// SmtpMail.UseDefaultCredentials = True
|
|
SmtpMail.Credentials = new NetworkCredential(CuentaCorreo, ContraseñaCorreo);
|
|
}
|
|
SmtpMail.EnableSsl = UsarSSL;
|
|
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
ServicePointManager.ServerCertificateValidationCallback =
|
|
(sender, certificate, chain, sslPolicyErrors) => true;
|
|
|
|
|
|
SmtpMail.Send(myMessage);
|
|
}
|
|
catch (Exception myexp)
|
|
{
|
|
throw myexp;
|
|
}
|
|
}
|
|
|
|
internal static void EnviaCorreoVariosAdjuntos(string ServidorSMTP, string Remitente, string Destinatario, string Asunto, string Cuerpo, List<FicheroAdjunto> FicherosAdjuntos = null, string CC = "", string BCC = "", string CuentaCorreo = "", string ContraseñaCorreo = "", int Puerto = 25, bool UsarSSL = false)
|
|
{
|
|
try
|
|
{
|
|
Attachment myAttch;
|
|
SmtpClient SmtpMail;
|
|
MailMessage myMessage;
|
|
int i, iCnt;
|
|
|
|
myMessage = new MailMessage(Remitente, Destinatario, Asunto, Cuerpo);
|
|
myMessage.BodyEncoding = System.Text.Encoding.Default;
|
|
myMessage.ReplyTo = new MailAddress(Remitente, Remitente);
|
|
myMessage.Sender = new MailAddress(Remitente, Remitente);
|
|
myMessage.From = new MailAddress(Remitente, Remitente);
|
|
|
|
|
|
if (!string.IsNullOrEmpty(CC))
|
|
{
|
|
myMessage.CC.Add(CC);
|
|
}
|
|
if (!string.IsNullOrEmpty(BCC))
|
|
{
|
|
myMessage.Bcc.Add(BCC);
|
|
}
|
|
ContentDisposition cd;
|
|
if (FicherosAdjuntos !=null)
|
|
{
|
|
iCnt = FicherosAdjuntos.Count - 1;
|
|
var loopTo = iCnt;
|
|
for (i = 0; i <= loopTo; i++)
|
|
{
|
|
if (!string.IsNullOrEmpty(FicherosAdjuntos[i].Ruta))
|
|
{
|
|
if (File.Exists(FicherosAdjuntos[i].Ruta))
|
|
{
|
|
myAttch = new Attachment(FicherosAdjuntos[i].Ruta);
|
|
cd = myAttch.ContentDisposition;
|
|
cd.FileName = FicherosAdjuntos[i].NombreFichero;
|
|
myMessage.Attachments.Add(myAttch);
|
|
}
|
|
}
|
|
else if (FicherosAdjuntos[i].Fichero != null && FicherosAdjuntos[i].Fichero.Length > 0)
|
|
{
|
|
myAttch = new Attachment(new MemoryStream(FicherosAdjuntos[i].Fichero), FicherosAdjuntos[i].NombreFichero);
|
|
cd = myAttch.ContentDisposition;
|
|
cd.FileName = FicherosAdjuntos[i].NombreFichero;
|
|
myMessage.Attachments.Add(myAttch);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
SmtpMail = new SmtpClient();
|
|
|
|
if (!string.IsNullOrEmpty(ServidorSMTP))
|
|
SmtpMail.Host = ServidorSMTP;
|
|
SmtpMail.Port = Puerto;
|
|
if (!string.IsNullOrEmpty(CuentaCorreo))
|
|
{
|
|
SmtpMail.Credentials = new NetworkCredential(CuentaCorreo, ContraseñaCorreo);
|
|
}
|
|
SmtpMail.EnableSsl = UsarSSL;
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|
|
|
SmtpMail.Send(myMessage);
|
|
}
|
|
catch (Exception myexp)
|
|
{
|
|
throw myexp;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Envía un correo electrónico. Puede recibir adjuntos mediante un Dictionary(Of String, Stream).
|
|
/// </summary>
|
|
/// <param name="servidorSMTP"></param>
|
|
/// <param name="remitente"></param>
|
|
/// <param name="destinatario"></param>
|
|
/// <param name="asunto"></param>
|
|
/// <param name="cuerpo"></param>
|
|
/// <param name="adjuntos">Un Dictionary(Of String, Stream). La clave es el nombre del archivo adjunto, el valor es el contenido del archivo adjunto en forma de Stream.</param>
|
|
/// <param name="cc"></param>
|
|
/// <param name="bcc"></param>
|
|
/// <param name="cuentaCorreo"></param>
|
|
/// <param name="contraseñaCorreo"></param>
|
|
/// <param name="puerto"></param>
|
|
/// <param name="usarSSL"></param>
|
|
/// <remarks></remarks>
|
|
internal static void EnviarCorreoElectrónico(string servidorSMTP, string remitente, string destinatario, string asunto, string cuerpo, Dictionary<string, Stream> adjuntos = null, string cc = "", string bcc = "", string cuentaCorreo = "", string contraseñaCorreo = "", int puerto = 25, bool usarSSL = false, string ResponderA = "")
|
|
{
|
|
try
|
|
{
|
|
SmtpClient clienteSMTP;
|
|
MailMessage mensaje;
|
|
|
|
// Si es alguna de las máquinas de desarrollo de danmun, el correo se envía solamente a danmun. Son pruebas.
|
|
// If Environment.MachineName = "WIN81PDDANMUN" OrElse Environment.MachineName.ToUpper = "INTI81".ToUpper OrElse Environment.MachineName.ToUpper = "INTI10".ToUpper Then
|
|
// destinatario = "danmun@tecnosis.eu"
|
|
// End If
|
|
|
|
asunto = asunto.Replace(Environment.NewLine, " ");
|
|
mensaje = new MailMessage(remitente, destinatario, asunto, cuerpo);
|
|
mensaje.BodyEncoding = System.Text.Encoding.Default;
|
|
if (ResponderA is null || string.IsNullOrWhiteSpace(ResponderA))
|
|
{
|
|
mensaje.ReplyToList.Add(new MailAddress(remitente, remitente));
|
|
}
|
|
else
|
|
{
|
|
mensaje.ReplyToList.Add(new MailAddress(ResponderA, ResponderA));
|
|
mensaje.ReplyToList.Add(new MailAddress(remitente, remitente));
|
|
}
|
|
mensaje.Sender = new MailAddress(remitente, remitente);
|
|
mensaje.From = new MailAddress(remitente, remitente);
|
|
|
|
|
|
if (!string.IsNullOrEmpty(cc))
|
|
{
|
|
mensaje.CC.Add(cc);
|
|
}
|
|
if (!string.IsNullOrEmpty(bcc))
|
|
{
|
|
mensaje.Bcc.Add(bcc);
|
|
}
|
|
if (adjuntos != null)
|
|
{
|
|
if (adjuntos.Count > 0)
|
|
{
|
|
foreach (var adjunto in adjuntos)
|
|
mensaje.Attachments.Add(new Attachment(adjunto.Value, adjunto.Key));
|
|
}
|
|
}
|
|
|
|
clienteSMTP = new SmtpClient();
|
|
|
|
if (!string.IsNullOrEmpty(servidorSMTP))
|
|
clienteSMTP.Host = servidorSMTP;
|
|
clienteSMTP.Port = puerto;
|
|
if (!string.IsNullOrEmpty(cuentaCorreo))
|
|
{
|
|
clienteSMTP.Credentials = new NetworkCredential(cuentaCorreo, contraseñaCorreo);
|
|
}
|
|
clienteSMTP.EnableSsl = usarSSL;
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|
|
|
clienteSMTP.Send(mensaje);
|
|
}
|
|
catch (Exception myexp)
|
|
{
|
|
throw new Exception(myexp.Message, myexp);
|
|
}
|
|
}
|
|
|
|
internal static void EnviaCorreoMultiplesDestinatarios(string servidorSMTP, string remitente, List<string> listaDestinatarios, string asunto, string cuerpo, List<string> ficherosAdjuntos, string CC = "", string BCC = "", string cuentaCorreo = "", string contraseñaCorreo = "", int puerto = 25, bool usarSSL = false, string responderA = "")
|
|
{
|
|
try
|
|
{
|
|
Attachment misAdjuntos;
|
|
SmtpClient clienteSMTP;
|
|
MailMessage miMensaje;
|
|
int i, iCnt;
|
|
|
|
// // Si es alguna de kas máquinas de desarrollo de danmun, el correo se envía solamente a danmun. Son pruebas.
|
|
// If Environment.MachineName = "WINXP-PARALLELS" OrElse
|
|
// Environment.MachineName = "WINXP-DE-DANIEL" OrElse
|
|
// Environment.MachineName.ToUpper = "Win81PDdanmun".ToUpper OrElse
|
|
// Environment.MachineName.ToUpper.StartsWith("INTI") Then
|
|
// listaDestinatarios = New List(Of String)
|
|
// listaDestinatarios.Add("danmun@tecnosis.net")
|
|
// End If
|
|
|
|
foreach (var destinatario in listaDestinatarios)
|
|
{
|
|
asunto = asunto.Replace(Environment.NewLine, " ");
|
|
miMensaje = new MailMessage(new MailAddress(remitente, remitente), new MailAddress(destinatario, destinatario))
|
|
{
|
|
Subject = asunto,
|
|
Body = cuerpo,
|
|
BodyEncoding = System.Text.Encoding.UTF8
|
|
};
|
|
if (responderA is null || string.IsNullOrWhiteSpace(responderA))
|
|
{
|
|
miMensaje.ReplyToList.Add(new MailAddress(remitente, remitente));
|
|
}
|
|
else
|
|
{
|
|
miMensaje.ReplyToList.Add(new MailAddress(responderA, responderA));
|
|
miMensaje.ReplyToList.Add(new MailAddress(remitente, remitente));
|
|
}
|
|
miMensaje.Sender = new MailAddress(remitente, remitente);
|
|
|
|
|
|
if (!string.IsNullOrEmpty(CC))
|
|
{
|
|
miMensaje.CC.Add(CC);
|
|
}
|
|
if (!string.IsNullOrEmpty(BCC))
|
|
{
|
|
miMensaje.Bcc.Add(BCC);
|
|
}
|
|
if (ficherosAdjuntos != null)
|
|
{
|
|
iCnt = ficherosAdjuntos.Count - 1;
|
|
var loopTo = iCnt;
|
|
for (i = 0; i <= loopTo; i++)
|
|
{
|
|
if (File.Exists(ficherosAdjuntos[i]))
|
|
{
|
|
misAdjuntos = new Attachment(ficherosAdjuntos[i]);
|
|
miMensaje.Attachments.Add(misAdjuntos);
|
|
// misAdjuntos.Dispose()
|
|
}
|
|
}
|
|
}
|
|
|
|
clienteSMTP = new SmtpClient();
|
|
|
|
if (!string.IsNullOrEmpty(servidorSMTP))
|
|
clienteSMTP.Host = servidorSMTP;
|
|
clienteSMTP.Port = puerto;
|
|
if (!string.IsNullOrEmpty(cuentaCorreo))
|
|
{
|
|
clienteSMTP.Credentials = new NetworkCredential(cuentaCorreo, contraseñaCorreo);
|
|
}
|
|
clienteSMTP.EnableSsl = usarSSL;
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|
clienteSMTP.Send(miMensaje);
|
|
System.Threading.Thread.Sleep(1000 * (listaDestinatarios.Count - 1));
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new Exception(e.Message, e);
|
|
}
|
|
}
|
|
}
|
|
|
|
public class FicheroAdjunto
|
|
{
|
|
public string Ruta { get; set; }
|
|
public string NombreFichero { get; set; }
|
|
public byte[] Fichero { get; set; }
|
|
}
|
|
} |