TFS_ANTIGUO/SInCathay/ULA.Cathay.Credito/Commons/EmailManager.cs

90 lines
No EOL
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Web;
using Ultimus.Utilitarios;
namespace ULA.Cathay.Credito.Commons
{
public class EmailManager
{
public void Send(string Body, bool IsBodyHtml, string Subject, List<string> Attach, List<string> MailTo)
{
Email email = new Email();
email.Attach = Attach;
email.Body = Body;
email.Domain = ConfigurationManager.AppSettings["SMTP.Domain"].ToString();
email.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["SMTP.EnableSsl"]);
email.FromEmail = ConfigurationManager.AppSettings["SMTP.FromEmail"].ToString();
email.IsBodyHtml = IsBodyHtml;
email.MailTo = MailTo;
email.Password = ConfigurationManager.AppSettings["SMTP.Password"].ToString();
email.SmtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["SMTP.SmtpPort"]);
email.SmtpServer = Convert.ToString(ConfigurationManager.AppSettings["SMTP.SmtpServer"]);
email.Subject = Subject;
email.UserName = Convert.ToString(ConfigurationManager.AppSettings["SMTP.UserName"]);
email.SendMail();
}
//metodo temporal hasta que se actualice el framework
public void Send2(Email email, List<Attachment> AttachFile = null)
{
email.Domain = ConfigurationManager.AppSettings["SMTP.Domain"].ToString();
email.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["SMTP.EnableSsl"]);
email.FromEmail = ConfigurationManager.AppSettings["SMTP.FromEmail"].ToString();
email.Password = ConfigurationManager.AppSettings["SMTP.Password"].ToString();
email.SmtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["SMTP.SmtpPort"]);
email.SmtpServer = Convert.ToString(ConfigurationManager.AppSettings["SMTP.SmtpServer"]);
email.UserName = Convert.ToString(ConfigurationManager.AppSettings["SMTP.UserName"]);
System.Net.Mail.MailMessage ObjMail = new System.Net.Mail.MailMessage();
System.Net.Mail.SmtpClient ObjSmpt = new System.Net.Mail.SmtpClient();
System.Net.NetworkCredential ObjCredential = new System.Net.NetworkCredential();
ObtieneParametros ObjParametros = new ObtieneParametros();
email.Domain = ObjParametros.GetValueKeyRegedit("Email_SMTP.Domain", false);
email.EnableSsl = ObjParametros.GetValueKeyRegedit("Email_SMTP.EnableSsl", false) == "1" ? true : false;
email.FromEmail = ObjParametros.GetValueKeyRegedit("Email_SMTP.FromEmail", false);
email.Password = ObjParametros.GetValueKeyRegedit("Email_SMTP.Password", true);
email.SmtpServer = ObjParametros.GetValueKeyRegedit("Email_SMTP.SmtpServer", false);
email.UserName = ObjParametros.GetValueKeyRegedit("Email_SMTP.UserName", false);
int port;
if (int.TryParse(ObjParametros.GetValueKeyRegedit("Email_SMTP.SmtpPort", false), out port))
#region CODIGO TEMPORAL
ObjMail.From = new System.Net.Mail.MailAddress(email.FromEmail);
ObjMail.Subject = email.Subject;
ObjMail.Body = email.Body;
ObjMail.IsBodyHtml = email.IsBodyHtml;
ObjMail.Priority = System.Net.Mail.MailPriority.High;
foreach (string mailto in email.MailTo)
ObjMail.To.Add(mailto);
foreach (string attach in email.Attach)
ObjMail.Attachments.Add(new Attachment(attach));
if (AttachFile != null)
foreach (Attachment attach in AttachFile)
ObjMail.Attachments.Add(attach);
ObjCredential.Domain = email.Domain;
ObjCredential.UserName = email.UserName;
ObjCredential.Password = email.Password;
ObjSmpt.Credentials = ObjCredential;
ObjSmpt.UseDefaultCredentials = true;
ObjSmpt.EnableSsl = email.EnableSsl;
ObjSmpt.Host = email.SmtpServer;
ObjSmpt.Port = email.SmtpPort;
ObjSmpt.Send(ObjMail);
#endregion
}
}
}