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:
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
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.
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:
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.
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.
Kr,
Andreas