我如何使用tls发送电子邮件?

时间:2012-09-05 19:34:21

标签: vb.net email

我需要使用tls发送电子邮件如何在vb.net中发送? 这是我在thunderbird中确认的服务器信息,因为它需要我的服务器手动输入: 1. smtp服务器:pod51004.outlook.com 港口:587 3. TLSSTART是我记忆中的连接安全性

我在这个场景中使用live @ edu电子邮件服务。 继承了我的尝试代码:

Class Mailer
        ''one static method for sending e-mails
        Shared Sub SendMail(ByVal [From] As String, ByVal [To] As String, _
                            ByVal Subject As String, ByVal Body As String, ByVal MailServer _
                            As String, Optional ByVal IsBodyHtml As Boolean = True, _
                            Optional ByVal MailPort As Integer = 25, _
                            Optional ByVal Attachments() As String = Nothing, Optional _
                            ByVal AuthUsername As String = Nothing, Optional ByVal _
                            AuthPassword As String = Nothing)

            'On Error GoTo ErrorHandler
            'create a SmtpClient object to allow applications to send 
            'e-mail by using the Simple Mail Transfer Protocol (SMTP).
            Dim MailClient As SmtpClient = New SmtpClient(MailServer, MailPort)
            MailClient.EnableSsl = True
            MailClient.DeliveryMethod = SmtpDeliveryMethod.Network
            'MailClient.UseDefaultCredentials = False
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
            Dim nc As NetworkCredential = New NetworkCredential(AuthUsername, AuthPassword)
            ''create a MailMessage object to represent an e-mail message
            ''that can be sent using the SmtpClient class
            Dim MailMessage = New MailMessage( _
            [From], [To], Subject, Body)
            ''sets a value indicating whether the mail message body is in Html.
            MailMessage.IsBodyHtml = IsBodyHtml
            ''sets the credentials used to authenticate the sender
            If (AuthUsername IsNot Nothing) AndAlso (AuthPassword _
                                                     IsNot Nothing) Then
                MailClient.Credentials = nc


            End If
            Dim 
            ''add the files as the attachments for the mailmessage object
            If (Attachments IsNot Nothing) Then
                For Each FileName In Attachments
                    MailMessage.Attachments.Add( _
                    New Attachment(FileName))
                Next
            End If
            MailClient.Send(MailMessage)
            ''ErrorHandler:
            '' MsgBox("My error")
        End Sub
    End Class

任何人都想要创意吗?

1 个答案:

答案 0 :(得分:0)

很抱歉让回答者感到困惑,但是归结为一般性错误它给出的是拒绝访问权限。事实证明,这是由于mcafee阻止连接,认为这是一种蠕虫试图批量发送电子邮件(笑)。但感谢你的帮助。附:记得在将来出现类似问题的情况下检查mcafee。让自动电子邮件与mcafee一起工作很痛苦。