【问题标题】:why do I experience issues with SMTP server after upgrading from VB.net 3.5 to 4.8?为什么我在从 VB.net 3.5 升级到 4.8 后遇到 SMTP 服务器问题?
【发布时间】:2021-12-11 03:10:01
【问题描述】:

我有一个用 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。

我知道如何处理异常,但不知道如何修复异常。我相信这与我使用了 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: 身份验证失败,因为远程方已关闭传输流。在 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[] 传入, Int32 计数, AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.ForceAuthentication(布尔接收优先,字节 [] 缓冲区,AsyncProtocolRequest asyncRequest)在 System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResultlazyResult)在 System.Net.TlsStream.CallProcessAuthentication(对象状态)在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx)在 System.Threading.ExecutionContext t.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)at System.Net .TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)at System.Net.Mail.SmtpConnection.Flush()at System .Net.Mail.ReadLinesCommand.Send(SmtpConnection conn) at System.Net.Mail.EHelloCommand.Send(SmtpConnection 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 message)at ASP.processform_aspx.__Render__control1(HtmlTe xtWriter __w, Control parameterContainer) 在 C:\Users\user\Documents\Visual Studio 2012\Projects\program - Copy\program\processForm.aspx:line 385

【问题讨论】:

  • 认为您可能希望通过 nugets 而不是直接 SMTP 来使用 MailKit
  • 不确定是否有帮助,但您可以尝试指定协议System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12
  • 非常感谢您的帮助。
  • 将您的解决方案添加为答案,我会将其标记为答案。我希望你得到荣誉。

标签: vb.net smtp .net-framework-4.8


【解决方案1】:

您可以指定要使用的协议

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

【讨论】:

    猜你喜欢
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    相关资源
    最近更新 更多