question

RobertGustafson-1682 avatar image
0 Votes"
RobertGustafson-1682 asked RobertGustafson-1682 answered

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

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.)












dotnet-visual-basicwindows-forms
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @RobertGustafson-1682,
In order to find the cause more accurately, please provide the relevant code to reproduce the problem.
And here is a related thread you can refer to firstly.
Best Regards,
Daniel Zhang


0 Votes 0 ·

See the additions above. Note that for confidentiality reasons, I haven't provided info like who the email is being sent from or two, or like the username or password. None of that seems to matter anyway. I also looked at that thread you mentioned and tried making sure the port was 587. Still doesn't work! (And it stalls on port 2525. SmtpClient doesn't really support port 465, and port 25 doesn't work either.)

I've seen on the web similar complaints regarding certain hosts, including smtp.aol.com. Is there a glitch in VS 16.9.4 or in AOL lately?

0 Votes 0 ·
RobertGustafson-1682 avatar image
0 Votes"
RobertGustafson-1682 answered

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!



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered RobertGustafson-1682 commented

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", "joey@friends.com"))
             message.To.Add(New MailboxAddress("Mrs. Chanandler Bong", "chandler@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



figure1.png (15.4 KiB)
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I've tried replacing System.Net.Mail's version of SmtpClient with System.Mailkit.Smtp's version, and yet the following code doesn't work (rest of code withheld for security reasons)!

 Using smtp As SmtpClient = New SmtpClient()
     smtp.Connect("smtp.aol.com") 'this is where I have the problem, no matter what the port or encryption
     smtp.Authenticate(UserName, Password)
     smtp.Send(message)
     smtp.Disconnect(True)
 End Using

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.

0 Votes 0 ·

The "SSL Handshake" problem I'me having applies no matter what port or socket-encryption method I use (when I specify those parameters in Connect). And I need to be able to use "smtp.aol.com"; The only email address I can send from is an AOL one!


0 Votes 0 ·

HELP ME!!!!

0 Votes 0 ·
Show more comments

What port is being used? Try port 25. And look at SecureSocketOptions for 3rd parameter to the Connect method.

Look at smtp.SslProtocols = System.Security.Authentication.SslProtocols.Tls11

Before .Connect

0 Votes 0 ·
Show more comments