martes, 21 de febrero de 2017

Clase Visual Basic .NET para enviar correo con GMAIL. Este código también es válido para otros servidores de correo haciendo pequeños cambios.

Imports System.Net.Mail

'using a google account to send email if you got authentication error, you need to enable Allowing less secure apps or Sign in using App Passwords.
'https://www.youtube.com/watch?v=MOnyExcIPVY'
'
'NOTA: Aún habiendo activado el uso de aplicaciones de terceros para enviar a través del servidor de Gmail, Google también puede bloquear los envíos si el dispositivo no es de confianza. Aconsejo a identificarse en Gmail via web en el dispositivo que usará la aplicación para que sea reconocido como dispositivo de confianza.

Public Class clsMailSender
    Const GMAILUSER = "your_account@gmail.com"
    Const GMAILPASS = "your_password"
    Const GMAILSMTPHOST = "smtp.gmail.com"
    Const GMAILSMTPPORT = 587
    Const GMAILbEnableSSL = True
 
    'Si no funciona por el puerto SMTP 587, prueba con el 25

    Public Function SendEmailGMAIL(ByVal MailSubject As String, ByVal MessageBody As String, ByVal ToAddresses As String) As Boolean
        'SendEmailGMAIL("subject","messagebody","address1;address2;...")
        Try
            Dim Mail As New MailMessage()
            Mail.Subject = MailSubject
            Mail.From = New System.Net.Mail.MailAddress(GMAILUSER)
            For Each Address As String In Split(ToAddresses, ";")
                Mail.To.Add(Address)
            Next
            Mail.Body = MessageBody
            Dim SmtpServer As New SmtpClient()
            SmtpServer.Credentials = New Net.NetworkCredential(GMAILUSER, GMAILPASS)
            SmtpServer.Host = GMAILSMTPHOST
            SmtpServer.Port = GMAILSMTPPORT
            SmtpServer.EnableSsl = GMAILbEnableSSL
            SmtpServer.Send(Mail)

            Return True
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Return False
        End Try
    End Function

 End Class

1 comentario:

  1. Este video me sirvió mucho, espero les sirva, en la descripción del video esta para descargar el código: https://www.youtube.com/watch?v=67h7WbCKh_g&t=3s

    Si te ha el error SMTP requiere una conexión segura la solución la encontré en este video:https://www.youtube.com/watch?v=oZOZvcrM3-k

    ResponderEliminar

VBA Access. Redondeo de números decimales con el método medio redondeo. Alternativa a la función Round (bankers round)

 Private Function Redondeo(ByVal Numero As Variant, ByVal Decimales As Integer) As Double     'Aplica método medio redondeo (half round ...