primero se incluye este namespace
using System.Net.Mail;
y dentro de un boton por ejemplo se escribe esto:
MailMessage correo = new MailMessage();
correo.From = new MailAddress("Tu_correo");
correo.To.Add("E-mail_destinatario");
correo.Subject = "Prueba";
correo.Body = "prueba de correo";
SmtpClient cliente = new SmtpClient("smtp.gmail.com");
cliente.Port = 587;
cliente.Credentials = new System.Net.NetworkCredential("Tu_correo_gmail", "Tu_password");
cliente.EnableSsl = true;
cliente.Send(correo);
adema puede kedar mucho mas lindo si usamos un formulario windows y cambiamos los datos por campos de texto
ademas si se agrega esto:
correo.IsBodyHtml = true;
en el cuerpo del mensaje podemos agregar html
y para finalizar con estas lineas podemos adjuntar un archivo:
Attachment adjuntos = new Attachment("ruta_de_nuestro_archivo/carta.txt");
correo.Attachments.Add(adjuntos);
espero les guste
Hola cuando pongo tu codigo me sale esto:
Información adicional: El servidor SMTP requiere una conexión segura o el cliente no se autenticó. La respuesta del servidor fue: 5.5.1 Authentication Required. Learn more at
¿Que puedo hacer?
Buenas
1) Está prohibido revivir temas antiguos para preguntar, debes formular tu duda en un nuevo post.
2) Debes mostrar tu código si esperas poder recibir mejor ayuda.
3) La razón del error:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated
CitarThat error message is typically caused by one of the following:
Incorrect connection settings, such as the wrong port specified for the secured or non-secured connection
Incorrect credentials. I would verify the username and password combination, to make sure the credentials are correct.
4) Un ejemplo que a mi me funciona:
' GMail Sender
' By Elektro
'
' Usage Examples :
' GMailSender("Username@Gmail.com", "Password", "Email Subject", "Message Body", "Receiver@Address.com")
'
''' <summary>
''' Sends an e-mail through GMail service.
''' </summary>
''' <param name="Username">Indicates the GMail account username.</param>
''' <param name="Password">Indicates the GMail account password.</param>
''' <param name="Subject">Indicates e-mail subject.</param>
''' <param name="Body">Indicates e-mail body.</param>
''' <param name="Addresses">Indicates the address(es) to send.</param>
Private Sub GMailSender(ByVal Username As String,
ByVal Password As String,
ByVal Subject As String,
ByVal Body As String,
ByVal Addresses As String)
Using MailSetup As New System.Net.Mail.MailMessage
MailSetup.Subject = Subject
MailSetup.To.Add(Addresses)
MailSetup.From = New System.Net.Mail.MailAddress(Username)
MailSetup.Body = Body
Using SMTP As New System.Net.Mail.SmtpClient("smtp.gmail.com")
SMTP.Port = 587
SMTP.EnableSsl = True
SMTP.Credentials = New Net.NetworkCredential(Username, Password)
SMTP.Send(MailSetup)
End Using ' SMTP
End Using ' MailSetup
End Sub
Tema cerrado.