首页 > 解决方案 > 使用启用了 tls 的服务器在 VB.Net 中发送电子邮件

问题描述

我正在开发使用启用了 TLS 的 SMTP 服务器发送电子邮件的应用程序,我想在 windows server 2003 上运行此应用程序。当我在 windows server 2012 R2 上运行相同的应用程序时,它运行完美,但在 windows server 2003 上无法运行。有吗它不能在window server 2003 上运行的任何具体原因?

错误:SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.1 客户端未通过身份验证。

我在我的应用程序中使用了以下代码:

Public Sub sendemail()
    Dim SMTPMailServer As New System.Net.Mail.SmtpClient("xyz") 'tls enabled SMTP Server Name 
    Dim myMail As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage("FromEmail", "ToEmail")
    With myMail
        .Subject = "Test Email with TLS enabled server"
        .Body = "Test Body"
        .Priority = Net.Mail.MailPriority.Normal
        .IsBodyHtml = True
    End With
    SMTPMailServer.Send(myMail)
    myMail = Nothing
End Sub

标签: vb.netemailsmtptls1.2windows-server-2003

解决方案


Imports System.Net.Mail
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New _
        Net.NetworkCredential("username@gmail.com", "password")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            mail = New MailMessage()
            mail.From = New MailAddress("YOURusername@gmail.com")
            mail.To.Add("TOADDRESS")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail from GMAIL"
            SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

推荐阅读