Envio de Email

Colapsar
X
  • Filtrar
  • Tiempo
  • Mostrar
Limpiar todos
nuevos mensajes

  • Envio de Email

    Estoy escribiendo una clase para envio de eMails en VBNet (2005), el código es el siguiente:
    Código:
    Imports System.Net.Mail
    Imports System.ComponentModel
    Public Class clsSendMail
        Private Shared mailSent As Boolean = False
    #Region "SMTP CONFIG"
        Public Host As String
        Public UserName As String
        Public Password As String
        Public Port As Long
        Public ReqSSL As Boolean
    
    #End Region
    #Region "BODY"
        Public ConfirmacionEntrega As Boolean
        Public FromName As String
        Public FromEMail As String
        Public ToEMails As String
        Public CC As String = ""
        Public BCC As String = ""
        Public Atachments As String
        Public Subject As String
        Public Mensaje As String
    
    #End Region
    
        Private Shared Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
            ' Get the unique identifier for this asynchronous operation.
            Dim token As String = CStr(e.UserState)
    
            If e.Cancelled Then
                MessageBox.Show(token & " Send canceled.", token)
            End If
            If e.Error IsNot Nothing Then
                MessageBox.Show(token & " " & e.Error.ToString)
            Else
                MessageBox.Show("Message sent.")
            End If
            mailSent = True
        End Sub
    
        Public Sub Send()
            Dim client As SmtpClient
            Dim message As MailMessage
            Try
    
                ' Command line argument must the the SMTP host.
                client = New SmtpClient
                With client
                    .Host = Host
                    .Port = Port
                    .EnableSsl = ReqSSL 'true
                    .UseDefaultCredentials = False
                    .Credentials = New Net.NetworkCredential(UserName, Password)
    
                    message = New MailMessage()
                    ' Specify the message content.
                    With message
    
                        .From = New MailAddress(Me.FromEMail, Me.FromName)
                        'Reemplazar los puntos y comas por comas.
                        .To.Add(ToEMails.Replace(";", ","))
                        If CC.Trim <> "" Then
                            .CC.Add(CC.Replace(";", ","))
                        End If
                        If BCC.Trim <> "" Then
                            .Bcc.Add(BCC.Replace(";", ","))
                        End If
    
                        .Subject = Subject
                        .Body = Mensaje
                        .BodyEncoding = System.Text.Encoding.UTF8
                        .SubjectEncoding = System.Text.Encoding.UTF8
                        If ConfirmacionEntrega Then
                            .DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure Or _
                                                           DeliveryNotificationOptions.OnSuccess
                        End If
                    End With
    
                    ' Set the method that is called back when the send operation ends.
                    AddHandler .SendCompleted, AddressOf SendCompletedCallback
                    ' The userState can be any object that allows your callback
                    ' method to identify this send operation.
                    ' For this example, the userToken is a string constant.
                    Dim userState As String = "Mensaje: " & Subject
                    .SendAsync(message, userState)
                    Dim answer As MsgBoxResult = MessageBox.Show("Mandando el mensaje", "aviso", MessageBoxButtons.OKCancel)
                    ' If the user canceled the send, and mail hasn't been sent yet,
                    ' then cancel the pending operation.
                    If answer = MsgBoxResult.Cancel AndAlso mailSent = False Then
                        .SendAsyncCancel()
                    End If
                    ' Clean up.
                    message.Dispose()
                End With
            Catch ex As System.Net.Mail.SmtpException
                If Not message Is Nothing Then
                    message.Dispose()
                End If
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
            End Try
    
        End Sub
    
    End Class
    Los parámetros Host, Puerto, Usuario, Password, etc. son correctos, pero se produce un error, que en la última InnerException es:

    "- InnerException {"No se ha podido establecer conexión ya que el equipo de destino ha denegado activamente dicha conexión"} System.Exception"

    Esta clase pretende ser una sustitución a una clase en VB6 y que si funciona correctamente con los mismos parámetros

    Una visita a las Normas del foro nunca viene mal
Trabajando...
X