Why doesn't SmtpClient work correctly in VS 2019 version 16.9.4?

Robert Gustafson 606 Reputation points
2021-04-21T05:56:31.587+00:00

WHAT I HAVE:
Visual Basic 2019 (Version 16.9.4), .NET 4.6.1, WinForms

MY PROBLEM:
I have an VB WinForms app that uses SmtpClient to send emails. The problem is, although it worked just fine before I updated VS 2019 to version 16.9.4 (from version 16.8), now it won't send emails.

If I use Send, I get the following exception:

Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)

If I use SendAsync, I get no problems (depending on the port) until the SendCompleted event, where e.Error returns a "protocol violation" exception.

It makes no difference what the message is, what the host/port is, or what the credentials (username/password) are. This is highly critical, as I have a major app that's designed to use SmtpClient to send emails! I had no problems at all before the update.

Please give me some assistance ASAP, and in VB.NET.

PS. The host is usually "smtp.aol.com".

Sample code snippet (
I'm not providing specific values for the parameters or the full code for security reasons. It doesn't matter, though, what the parameters are. I always get the same problems. Sometimes the nature of the error varies a little based on the port.):

Public Shared Sub CreateTestMessage3(ByVal Host As String, ByVal Port As Integer, _
   ByVal UserName As String, ByVal Password As String, _
   ByVal WhoFrom As String, ByVal WhoTo As String, _
   ByVal Subject As String, ByVal Message As String, ByVal FileAttached As String)
Dim [from] As New MailAddress(WhoFrom), [to] As New MailAddress(WhoTo)
Dim message As New MailMessage()
message.From = [from] : message.To.Add([to])
message.Subject = Subject
message.IsBodyHtml = True : message.Body = Message
message.Attachments.Add(New Attachment(FileAttached))
Dim client As New SmtpClient(Host, Port)
client.UseDefaultCredentials = False : client.EnableSsl = True
client.Credentials = New Net.NetworkCredential(UserName, Password)
Console.WriteLine("Sending an e-mail message")
Try
    client.Send(message)
 Catch ex As Exception
    Console.WriteLine("Exception caught in CreateTestMessage3(): {0}", ex.ToString())
End Try
Console.WriteLine("Finished")
client.Dispose()
End Sub

PS. I've since replaced the built-in Systen.Net.Mail version of Smtp client with the System.Mailkit.Smtp version, and yet the following code still fails at the the smtp.Connect("smtp.aol.com") line (once again, the rest of the code is hidden for confidentiality reasons):

Using smtp As SmtpClient = New SmtpClient()
 smtp.Connect("smtp.aol.com") 'This is where I have problems
 smtp.Authenticate(UserName, Password)
 smtp.Send(message)
 smtp.Disconnect(True)
End Using

I get the following exception at the Connect("smtp.aol.com") line:

I get the following SslHandshakeException at the Connect("smtp.aol.com") line:

Exception thrown: 'MailKit.Security.SslHandshakeException' in mscorlib.dll
SslHandshakeException Exception: An error occurred while attempting to establish an SSL or TLS connection.

The server's SSL certificate could not be validated for the following reasons:
• The server certificate has the following errors:
• The revocation function was unable to check revocation for the certificate.

For some reason, I just can't seem to connect to "smtp.aol.com", no matter what my SMTP provider is! I've also tried different values for the port and socket-encryption parameters. WTF is going on?!! (And yes, I need to be able to connect to AOL, since it's the only server for which I have an email account to send from.)

Developer technologies | Windows Forms
Developer technologies | VB
{count} votes

Accepted answer
  1. Robert Gustafson 606 Reputation points
    2021-04-22T02:46:59.683+00:00

    I figured it out. I need the following line before smtp.Connect:

    smtp.CheckCertificateRevocation = False
    

    One little statement makes all the difference, and I stumbled upon this (actually, someone else did) by accident!

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-04-21T17:29:49.927+00:00

    Hello,

    Smtp classes built in are obsolete, look at MailKit, two versions are listed below.

    89978-figure1.png

    Simple example

    Option Infer On  
      
         
    Imports MailKit.Net.Smtp  
    Imports MailKit  
    Imports MimeKit  
      
    Namespace TestClient  
    	Friend Class Program  
    		Public Shared Sub Main(ByVal args() As String)  
    			Dim message = New MimeMessage()  
    			message.From.Add(New MailboxAddress("Joey Tribbiani", "******@friends.com"))  
    			message.To.Add(New MailboxAddress("Mrs. Chanandler Bong", "******@friends.com"))  
    			message.Subject = "How you doin'?"  
      
    			message.Body = New TextPart("plain") With {.Text = "Hey Chandler,  I just wanted to let you know that Monica and I were going to go play some paintball, you in?  -- Joey"}  
      
    			Using client = New SmtpClient()  
    				client.Connect("smtp.friends.com", 587, False)  
      
    				' Note: only needed if the SMTP server requires authentication  
    				client.Authenticate("joey", "password")  
      
    				client.Send(message)  
    				client.Disconnect(True)  
    			End Using  
    		End Sub  
    	End Class  
    End Namespace  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.