Les dejo el siguiente código para enviar un correo electrónico:
Clase Smtp:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace ServerSmtp
{
public class Smtp
{
#region Variables
String _To;
String _From;
String _ServerSMTP;
String _Password;
String _Body;
String _subject;
bool _isBodyHtml;
int _puerto;
#endregion
#region Propiedades
public String To
{
set { _To = value; }
}
public String From
{
set { _From = value; }
}
public String SMTP
{
set { _ServerSMTP = value; }
}
public String Password
{
set { _Password = value; }
}
public String Body
{
set { _Body = value; }
}
public String Subject
{
set { _subject = value; }
}
public bool isBodyHtml
{
set { _isBodyHtml = value; }
}
public int Puerto
{
set { _puerto = value; }
}
#endregion
public void Send()
{
SmtpClient clienteSmtp = new SmtpClient(this._ServerSMTP, this._puerto);
clienteSmtp.UseDefaultCredentials = false;
clienteSmtp.EnableSsl = true;
if (this._Password.Trim() != "")
clienteSmtp.Credentials = new NetworkCredential(this._From, this._Password);
try
{
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress(this._To));
msg.From = new MailAddress(this._From);
msg.Subject = this._subject;
msg.IsBodyHtml = true;
msg.Body = this._Body;
clienteSmtp.Send(msg);
}
catch (SmtpException ex)
{
throw new Exception(ex.Message);
}
catch (Exception e)
{
throw e;
}
}
}
}
Como utilizar la clase:
Smtp email = new Smtp();
email.From = "prueba@gmail.com";
email.Password = "password";
email.SMTP = "smtp.gmail.com";
email.Puerto = 587;
email.To = para@gmail.com;
email.isBodyHtml = true;
email.Subject = "Subject";
email.Body = "<div style='font-family:Arial, Helvetica, sans-serif;font-size:11px;'>Prueba</div>";
email.Send();
-------------------------------------------------
Lo que se puede imaginar .. se puede programar
No hay comentarios:
Publicar un comentario