Private Sub EnviarEmail(remitente As String, contraseña As String, destinatatio As String, asunto As String, cuerpo As String, Optional archivoAdjunto As String = Nothing )
Try
Me.Cursor= Cursors.WaitCursor
Dim smtpServer As New SmtpClient()
Dim mail As New MailMessage()
smtpServer.Credentials = New Net.NetworkCredential(remitente, contraseña)
If remitente.EndsWith("@gmail.com") = True Then
smtpServer.Port = 587
smtpServer.Host = "smtp.gmail.com"
ElseIf remitente.EndsWith("@yahoo.es") = True Then
smtpServer.Port = 465
smtpServer.Host = "smtp.correo.yahoo.es"
ElseIf remitente.EndsWith("@hotmail.com") = True Then
smtpServer.Port = 465
smtpServer.Host = "smtp.live.com"
Else
MessageBox.Show("Ingrese un remitente válido; debe ser desde una cuenta de Gmail (*@gmail.com), Hotmail (*@hotmail.com) o Yahoo (*@yahoo.es)", "Remitente Inválido",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
smtpServer.EnableSsl = True
mail = New MailMessage()
mail.From = New MailAddress(remitente)
mail.To.Add(destinatario)
mail.Subject = asunto
mail.Body = cuerpo
mail.Priority = MailPriority.Normal
If (archivoAdjunto= Nothing) = False Then
Dim Adjunto As Attachment
Adjunto = New Net.Mail.Attachment(archivoAdjunto)
mail.Attachments.Add(Adjunto)
End If
smtpServer.Send(mail)
Me.Cursor= Cursors. Default
MsgBox("¡Correo satisfactoriamente enviado!", MsgBoxStyle.Information, "Correo")
Catch ex As Exception
Me.Cursor= Cursors.Default
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error")
End Try
End Sub