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

Time:10-30

I have a vb.net forms application that was written in vb.net 3.5. It sends email via smtp email server: office 365. Office 365 is ending support for TLS 1.0 & 1.1.

The program has started tossing the error: Authentication failed because the remote party has closed the transport stream. The weird thing is what when the error occurs, the email still gets sent.

Research shows that it's due to the security protocol being used to connect to the server and it's getting refused. The weird thing is that the email still gets sent.

I did some research and found that I need to upgrade the version of VB.net the site is written in to at least 4.6 in order to support TLS 1.2. I upgraded the website to VB.net 4.8 and got a new error message: SMTPException not handled.

I figured out how to handle the exception, but not how to fix the exception. I believe it has to do with the fact that I have used EnableSsl() and thus the program in trying to use SSl instead of TLS. I don't know.

Below is the code I use to sen email:

Dim smtp As New SmtpClient(Session("SMTPClient"))
    smtp.Port = 587
    mail.From = New MailAddress("[email protected]")
    mail.To.Add("[email protected]")
    mail.ReplyTo = New MailAddress("[email protected]")
    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

The error I get when sending:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Authentication failed because the remote party has closed the transport 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 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)--- End of inner exception stack trace ---at System.Net.Mail.SmtpClient.Send(MailMessage message)at ASP.processform_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in C:\Users\user\Documents\Visual Studio 2012\Projects\program - Copy\program\processForm.aspx:line 385

CodePudding user response:

You can specify the protocol you want to use with

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