为什么我在从 VB.net 3.5 升级到 4.8 后遇到 SMTP 服务器问题?

why do I experience issues with SMTP server after upgrading from VB.net 3.5 to 4.8?

我有一个用 vb.net 3.5 编写的 vb.net 表单应用程序。它通过 smtp 电子邮件服务器发送电子邮件:office 365。Office 365 将结束对 TLS 1.0 和 1.1 的支持。

程序开始折腾报错:对方关闭传输流,认证失败。奇怪的是当错误发生时,电子邮件仍然被发送。

研究表明,这是由于用于连接服务器的安全协议被拒绝。奇怪的是电子邮件仍然被发送。

我做了一些研究,发现我需要将 VB.net 网站写入的版本升级到至少 4.6 才能支持 TLS 1.2。我将网站升级到 VB.net 4.8 并收到一条新错误消息:SMTPException not handled.

我想出了如何处理异常,但不知道如何修复异常。我相信这与我使用 EnableSsl() 以及尝试使用 SSl 而不是 TLS 的程序有关。我不知道。

下面是我用来发送邮件的代码:

Dim smtp As New SmtpClient(Session("SMTPClient"))
    smtp.Port = 587
    mail.From = New MailAddress("email@company.com")
    mail.To.Add("email@company.com")
    mail.ReplyTo = New MailAddress("email@company.com")
    smtp.UseDefaultCredentials = False
    smtp.Credentials = New System.Net.NetworkCredential("user", "password", "domain")
    smtp.EnableSsl() = True
   
    If Session("sent") = 0 Then
        Try
            smtp.Send(mail)
        Catch ex As SmtpException
            Response.Write(ex)
        End Try
        Session("sent") = 1
    End If

发送时出现的错误:

System.Net.Mail.SmtpException: 发送邮件失败。 ---> System.IO.IOException: 身份验证失败,因为远程方已关闭传输 stream.at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)at System.Net.Security.SslState.StartReceiveBlob(Byte [] buffer, AsyncProtocolRequest asyncRequest)at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[ ] buffer, AsyncProtocolRequest asyncRequest)at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)at System.Net.TlsStream.CallProcessAuthentication(Object state)at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)at System.Threading.ExecutionContext.Run(ExecutionContext 执行上下文,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx)在 System.Threading.ExecutionContext.Run(ExecutionContext 执行上下文,ContextCallback 回调,对象状态)在 System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult 结果)在 System.Net.TlsStream.Write(字节 []缓冲区,Int32 偏移量,Int32 大小)在 System.Net.PooledStream.Write(字节 [] 缓冲区,Int32 偏移量,Int32 大小)在 System.Net.Mail.SmtpConnection.Flush()在 System.Net.Mail.ReadLinesCommand.Send(SmtpConnection conn)在 System.Net.Mail.EHelloCommand.Send (SmtpConn ection conn, String domain)at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)at System.Net.Mail.SmtpClient.GetConnection()at System.Net.Mail.SmtpClient.Send(MailMessage message)--- 内层结束异常堆栈跟踪 --- 在 System.Net.Mail.SmtpClient.Send(MailMessage 消息)在 ASP.processform_aspx.__Render__control1(HtmlTextWriter __w,控制参数容器)在 C:\Users\user\Documents\Visual Studio 2012\Projects\program - Copy\program\processForm.aspx:第 385 行

您可以指定要使用的协议

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12