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



我有一个用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)at System.Net.Security.SslState。StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest)在System.Net.Security.SslState。在System.Net.Security.SslState. CheckCompletionBeforeNextReceive(ProtocolToken消息,AsyncProtocolRequest asyncRequest)。StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest)at System.Net.Security.SslState。ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest)at System.Net.Security.SslState。在System.Net.TlsStream处理认证(LazyAsyncResult lazyResult)。在System.Threading.ExecutionContext. CallProcessAuthentication(对象状态)。在System.Threading.ExecutionContext. RunInternal(ExecutionContext, ExecutionContext, ContextCallback,回调,对象状态,布尔保存,syncctx)。在System.Threading.ExecutionContext中运行(ExecutionContext ExecutionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)。在System.Net.TlsStream上运行(ExecutionContext ExecutionContext, ContextCallback callback, Object state)。在System.Net.TlsStream上ProcessAuthentication(LazyAsyncResult result)。写(Byte[] buffer, Int32偏移量,Int32大小)在System.Net.PooledStream。写(字节[]缓冲区,Int32偏移量,Int32大小)在System.Net.Mail.SmtpConnection.Flush()在System.Net.Mail.ReadLinesCommand。发送(SmtpConnection conn)在System.Net.Mail.EHelloCommand。发送(SmtpConnection conn,字符串域)到System.Net.Mail.SmtpConnection。在System.Net.Mail.SmtpTransport上GetConnection(ServicePoint)。在System.Net.Mail.SmtpClient.GetConnection()发送(MailMessage消息)——内部异常堆栈跟踪的结束——在System.Net.Mail.SmtpClient。发送(MailMessage消息)在ASP.processform_aspx。__Render__control1(HtmlTextWriter __w, Control parameterContainer) in C:UsersuserDocumentsVisual Studio 2012Projectsprogram - CopyprogramprocessForm。aspx: 385行

您可以使用

指定要使用的协议
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12

相关内容

最新更新