// variable donde se guarda el escrito o body del email formato HTML
public string message_pagos = "<html><body> Estimado(a) : {0} <br><br> Adjunto a este correo se encuentra el comprobante de pago del: <h1>{1}</h1></body></html>.";
// llamado del metodo en un boton en un formulario X
email = new sendmail();
email.to = "EMAIL-DESTINO";
// variable1 ==> Estimado(a) : {0}
// variable2 ==> pago del: <h1>{1}</h1>
email.messaje = string.Format(message_pagos, variable1, variable2);
if (!email.validarEmail(email.to))
throw new Exception("El correo no tiene el formato válido.");
// clase email
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Net; // Nueva
using System.Net.Mail;
using System.Net.Mime;
using System.Text.RegularExpressions;
namespace email
{
/// <summary>
/// inicializar la clase de configuaracion
/// </summary>
public class mailconfiguration
{
/// <summary>
/// Inicializa los datos de configuracion para envio de correos
/// </summary>
public mailconfiguration()
{
from = "correo@gmail.com";
passwordSend = "password";
}
/// <summary>
/// Destinatario
/// </summary>
public string to { get; set; }
/// <summary>
/// Quien envia
/// </summary>
public string from { get; set; }
/// <summary>
/// Título del correo
/// </summary>
public string subject { get; set; }
/// <summary>
/// Mensaje del correo
/// </summary>
public string messaje { get; set; }
/// <summary>
/// Password para envio
/// </summary>
public string passwordSend { get; set; }
}
/// <summary>
/// Clase para envio de correos
/// </summary>
public class sendmail
{
/// <summary>
/// Inicializa la
/// </summary>
private mailconfiguration email = new mailconfiguration();
/// <summary>
/// Verifica si el correo es válido
/// </summary>
private bool esValido;
/// <summary>
/// Si el correo es enviado
/// </summary>
private bool enviado;
/// <summary>
/// Destinatario
/// </summary>
public string to { get; set; }
/// <summary>
/// Mensaje
/// </summary>
public string messaje { get; set; }
/// <summary>
/// Titulo del mensaje
/// </summary>
public string subject { get; set; }
/// <summary>
/// Encabezado del correo
/// </summary>
public string titlemessage { get; set; }
/// <summary>
/// valida el formato del correo
/// </summary>
/// <param name="email">correo electronico</param>
/// <returns>valor boolean</returns>
public bool validarEmail(string email)
{
try
{
esValido = true;
string expresion = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
if (Regex.IsMatch(email, expresion))
{
if (Regex.Replace(email, expresion, String.Empty).Length != 0)
esValido = false;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return esValido;
}
/// <summary>
/// envia el correo sin datos adjuntos
/// </summary>
/// <returns>valor boolean</returns>
public bool send()
{
try
{
enviado = true;
if (subject != null)
{
email.to = "correo@gmail.com";
email.subject = subject;
titlemessage = "Sistema de Contáctenos.";
}
else
{
email.to = to;
email.subject = "Envio de Contraseña.";
titlemessage = "Sistema Automático de Recordatorio de Contraseña";
}
email.messaje = messaje;
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add(email.to);
msg.From = new MailAddress(email.from, titlemessage, System.Text.Encoding.UTF8);
msg.Subject = email.subject;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = email.messaje;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(email.from, email.passwordSend);
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Send(msg);
}
catch (Exception ex)
{
enviado = false;
throw new Exception(ex.Message);
}
return enviado;
}
/// <summary>
/// envia el correo con datos adjuntos
/// </summary>
/// <returns>valor boolean</returns>
public bool send(String file)
{
try
{
enviado = true;
email.to = to;
email.subject = "Envio de comprobante de pago semanal.";
email.messaje = messaje;
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.To.Add(email.to);
msg.From = new MailAddress(email.from, "Sistema Automático Envío de Planillas", System.Text.Encoding.UTF8);
msg.Subject = email.subject;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = email.messaje;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = true;
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
msg.Attachments.Add(data);
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(email.from, email.passwordSend);
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.Send(msg);
}
catch (Exception ex)
{
enviado = false;
throw new Exception(ex.Message);
}
return enviado;
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.