Utilizando el espacio de nombre (namespace) System.Net.Mail, enviar un correo electrónico desde .NET es tarea sencilla.
Las clases que utilizaremos son:
Para ejemplificar el uso, veamos directamente un poco de código escrito en C#:
SmtpClient client;
public mMail()
{
client = new SmtpClient();
client.Credentials = new NetworkCredential("nombre@servidor.com", "password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
}
public void From( String fromMail, String fromName )
{
this.fromMail = fromMail;
this.fromName = fromName;
}
public String Send(String to, String subject, String body)
{
MailMessage msg = new MailMessage();
msg.To.Add(to);
msg.From = new MailAddress(this.fromMail, this.fromName , System.Text.Encoding.UTF8);
msg.Subject = subject;
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = body;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = true;
try
{
this.client.Send(msg);
return "Mail enviado correctamente";
}
catch (Exception ex)
{
return ex.Message;
}
}
}
Una respuesta para "Cliente de email en .NET"
Genial muy buen código muchaS pero mcuhas gracias..
estaría padre poder enviar los emails desde cualquier cuenta se correo
electrónico (incluir tambien hotmail por ejemplo), seguiré investigando, soy nuevo en esto
muchisimas gracias.
Comentarios: