Visual Basic.NET - enviar eMail

 
Vista:
Imágen de perfil de Alberto
Val: 224
Ha disminuido 1 puesto en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

enviar eMail

Publicado por Alberto (81 intervenciones) el 19/12/2017 12:18:59
Estoy escribiendo una clase para envio de eMails en VBNet (2005), el código es el siguiente:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder