0

I have a watchdog like program, which should send a mail to my own mail address if case some conditions are met.

I have a gmail account, so basically I want to send a mail from my own gmail address to the same address.

This is the code that I am using.

Public Sub MyContactByMail(ByVal Username As String, ByVal Password As String, ByVal SmtpServerPort As Integer, ByVal SmtpServerHost As String, ByVal MailFrom As String, ByVal MailTo As String, ByVal MailSubject As String, ByVal MailText As String)

    Try
        Dim SmtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        SmtpServer.Credentials = New Net.NetworkCredential(Username, Password)
        SmtpServer.Port = SmtpServerPort
        SmtpServer.Host = SmtpServerHost
        mail = New MailMessage()
        mail.From = New MailAddress(MailFrom)
        mail.To.Add(MailTo)
        mail.Subject = MailSubject
        mail.Body = MailText
        SmtpServer.Send(mail)
    Catch ex As Exception
        Console.ForegroundColor = ConsoleColor.Red
        Console.WriteLine("Class -> ClassStorage, Method -> MyContactByMail, Error -> " & ex.Message)
    End Try
End Sub



Username... My own e-mail address (gmail)
Password... My password for the gmail account
SmtpServerPort... 465
SmtpServerHost... smtp.gmail.com
MailFrom... My own e-mail address (gmail)
MailTo... My own e-mail address (gmail)
MailSubject... some Text
MailText... some Text

I never tried to send mails this way, so this whole topic is pretty new to me.

I found this page to be helpful:

https://support.google.com/a/answer/176600?hl=en

I have tried the following:

a)Gmail SMTP server

smtp.gmail.com + port 465

This is the connection test with paping:

enter image description here

It seems as I can connect to this address + port.

However VB still throws an exception.

It means, that from the connection no data could be read.

  • InnerException {"Von der Übertragungsverbindung können keine Daten gelesen werden: net_io_connectionclosed."} System.Exception

    smtp.gmail.com + port 587

Again test the connection with paping

enter image description here

This is the exception of the VB program.

  • InnerException {"Die Verbindung mit dem Remoteserver kann nicht hergestellt werden."} System.Exception

b)Restricted Gmail SMTP server

aspmx.l.google.com + port 25

I tried to connect to the port using paping, but apparently I could not connect to it. However the firewall is configured to let paping do whatever it wants.

enter image description here

This is the exception of the VB program:

It says, the connection to the remote computer could not be established... Which is pretty much the same result as with paping.

  • InnerException {"Die Verbindung mit dem Remoteserver kann nicht hergestellt werden."} System.Exception

Currently I do not have a G-Suit account - I didnt understand if it is mandatory to have a g-suite account to make this work. Maybe this is the issue.

If possible I would try to avoid paying for this.


I have used this tutorial to come so far: http://vb.net-informations.com/communications/vb.net_smtp_mail.htm


It seems as if I have two problems:

a) A connection problem on my developement machine - it is a company laptop and I do not have full control over the firewall, so I guess there are some default settings which overrule my settings and make port 587 not work.

I tried to connect with paping on an private laptop and it worked.

b) Now on the private laptop I have a different error message:

enter image description here

It means: For the SMTP Server there is a secure connection neccesary, or the client was not authenticated. The Server answer was: 5.5.1 Authentication Required

This is the current code I am using:

Public Sub MyContactByMail(ByVal Username As String, ByVal Password As String, ByVal SmtpServerPort As Integer, ByVal SmtpServerHost As String, ByVal MailFrom As String, ByVal MailTo As String, ByVal MailSubject As String, ByVal MailText As String)

Try
    Dim SmtpServer As New SmtpClient()
    Dim mail As New MailMessage()
    SmtpServer.Credentials = New Net.NetworkCredential(Username, Password)
    SmtpServer.Port = SmtpServerPort
    SmtpServer.Host = SmtpServerHost
    SmtpServer.EnableSsl = True
    SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
    SmtpServer.UseDefaultCredentials = False
    mail = New MailMessage()
    mail.From = New MailAddress(MailFrom)
    mail.To.Add(MailTo)
    mail.Subject = MailSubject
    mail.Body = MailText
    SmtpServer.Send(mail)
Catch ex As Exception
    Console.ForegroundColor = ConsoleColor.Red
    Console.WriteLine("Class -> ClassProcessingError, Method -> MyContactByMail, Error -> " & ex.Message)
End Try

End Sub


I did check the settings in my google account to allow less secure apps. Interestingly the default setting was to allow less secure apps.

enter image description here

So this setting didnt help anything.

I did also change my password to a strong password... This did not change anything too.

I still get the same authentication error message.

There is a section with last used devices...

Interestingly the VB.Net application does not even show up there..

It only show me the browser connection attempts to my account.

enter image description here

Kr,

Andreas

Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43
Andreas
  • 545
  • 2
  • 11
  • 24
  • Have you followed this example? https://stackoverflow.com/a/32336/3424212 the code is in C# but you can get the whole idea. What I see missing is : `SmtpServer.EnableSsl = true SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network. SmtpServer.UseDefaultCredentials = false` – mqueirozcorreia Nov 29 '17 at 10:14
  • Seems to be a connection issue on your side. Tried `paping smtp.gmail.com -p 587 -c 4`. Result: `Connected to 74.125.206.109: time=29.00ms protocol=TCP port=587` – MatSnow Nov 29 '17 at 10:26
  • I have modifyed my code accordingly but I still have a connection problem, when I use port 587... – Andreas Nov 29 '17 at 10:34
  • @Andreas Did you see my comment? Why should the code work if there is basically a connection problem? – MatSnow Nov 29 '17 at 10:41
  • Yes I do have a connection problem. I have just tried the code on another computer and there I have connection to port 587 – Andreas Nov 29 '17 at 11:01
  • I have edited my original message. It seems as if I have two issues.. a the connection on my company laptop and b a authentication issue on a different computer. – Andreas Nov 29 '17 at 11:09
  • See [here](https://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) or [here](https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not?lq=1) – MatSnow Nov 29 '17 at 11:14
  • In this case is better to open a second question. But the answer for the second problem is in this answer: https://stackoverflow.com/a/25414099/3424212 "enable access to your account from other devices/applications... " – mqueirozcorreia Nov 29 '17 at 11:28

3 Answers3

2

Answer #1 (unable to connect to the remote server)

It look like you need to add this (specially line 1). Use all the lines below in the specified order:

SmtpServer.EnableSsl = true
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpServer.UseDefaultCredentials = false
SmtpServer.Credentials = new NetworkCredential(fromAddress.Address, fromPassword)

I quote this answer below:

The other two ports have encryption; 587 uses TLS, 465 uses SSL.

To use 587 you should set SmtpClient.EnableSsl = true.

465 wont work with SmtpClient, it will work with the deprecated class SmtpMail instead.


Answer #2 (5.5.1 Authentication Required) for the error below:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

You need to allow your Google account to send e-mail using other devices/applications. See Google Account Security:

Under the "Access for less secure apps" section, you can enable access to your account from other devices/applications... like your C# application.

mqueirozcorreia
  • 905
  • 14
  • 33
  • I have edited my original message. It seems as if I have two issues.. a the connection on my company laptop and b a authentication issue on a different computer. I guess if the authentication issue is solved it should finally work. – Andreas Nov 29 '17 at 11:10
  • Didnt help, the access for less secure apps was activated by default - which I did not expect. – Andreas Nov 29 '17 at 12:00
  • In my case it was disabled, since in your case is enabled, it is good! Good to see that all is working now! – mqueirozcorreia Nov 29 '17 at 12:24
1

This code finally worked for me:

I had to put SmtpServer.UseDefaultCredentials = False before I set the network credentials.

Its really kind of a stupid issue :-) however I am happy that it is working now.

Thanks to everyone for the help - the links helped a ton.

Public Sub MyContactByMail(ByVal Username As String, ByVal Password As String, ByVal SmtpServerPort As Integer, ByVal SmtpServerHost As String, ByVal MailFrom As String, ByVal MailTo As String, ByVal MailSubject As String, ByVal MailText As String)

    Try
        Dim SmtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        SmtpServer.UseDefaultCredentials = False
        SmtpServer.Credentials = New Net.NetworkCredential(Username, Password)
        SmtpServer.Port = SmtpServerPort
        SmtpServer.Host = SmtpServerHost
        SmtpServer.EnableSsl = True
        SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
        mail = New MailMessage()
        mail.From = New MailAddress(MailFrom)
        mail.To.Add(MailTo)
        mail.Subject = MailSubject
        mail.Body = MailText
        SmtpServer.Send(mail)
    Catch ex As Exception
        Console.ForegroundColor = ConsoleColor.Red
        Console.WriteLine("Class -> ClassProcessingError, Method -> MyContactByMail, Error -> " & ex.Message)
    End Try
End Sub
Andreas
  • 545
  • 2
  • 11
  • 24
0

This works properly

Try
    Dim SmtpServer As New SmtpClient()
    Dim mail As New MailMessage()
    SmtpServer.UseDefaultCredentials = False
    SmtpServer.Credentials = New Net.NetworkCredential(Username, Password)
    SmtpServer.Port = SmtpServerPort
    SmtpServer.Host = SmtpServerHost
    SmtpServer.EnableSsl = True
    SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
    mail = New MailMessage()
    mail.From = New MailAddress(MailFrom)
    mail.To.Add(MailTo)
    mail.Subject = MailSubject
    mail.Body = MailText
    SmtpServer.Send(mail)
Catch ex As Exception
    Console.ForegroundColor = ConsoleColor.Red
    Console.WriteLine("Class -> ClassProcessingError, Method -> MyContactByMail, Error -> " & ex.Message)
End Try
David Buck
  • 3,752
  • 35
  • 31
  • 35