[英]Send mail with VB.Net
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.我有一个 gmail 帐户,所以基本上我想从我自己的 gmail 地址发送一封邮件到同一个地址。
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 https://support.google.com/a/answer/176600?hl=en
I have tried the following:我尝试了以下方法:
a)Gmail SMTP server a)Gmail SMTP 服务器
smtp.gmail.com + port 465
This is the connection test with paping:这是带有 paping 的连接测试:
It seems as I can connect to this address + port.看来我可以连接到这个地址+端口。
However VB still throws an exception.但是 VB 仍然抛出异常。
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 InnerException {"Von der Übertragungsverbindung können keine Daten gelesen werden: net_io_connectionclosed。"} System.Exception
smtp.gmail.com + port 587 smtp.gmail.com + 端口 587
Again test the connection with paping再次测试与 paping 的连接
This is the exception of the VB program.这是 VB 程序的例外。
b)Restricted Gmail SMTP server b) 受限 Gmail SMTP 服务器
aspmx.l.google.com + port 25
I tried to connect to the port using paping, but apparently I could not connect to it.我尝试使用 paping 连接到端口,但显然我无法连接到它。 However the firewall is configured to let paping do whatever it wants.
然而,防火墙配置为让 paping 随心所欲。
This is the exception of the VB program:这是 VB 程序的例外:
It says, the connection to the remote computer could not be established... Which is pretty much the same result as with paping.它说,无法建立到远程计算机的连接......这与使用 paping 的结果几乎相同。
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.目前我没有 G-Suit 帐户 - 我不明白是否必须拥有一个 G-Suite 帐户才能完成这项工作。 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到目前为止,我已经使用本教程: 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. a) 我的开发机器上的连接问题 - 这是一台公司笔记本电脑,我无法完全控制防火墙,所以我猜有一些默认设置会否决我的设置并使端口 587 无法工作。
I tried to connect with paping on an private laptop and it worked.我尝试在私人笔记本电脑上使用 paping 进行连接,并且成功了。
b) Now on the private laptop I have a different error message: b) 现在在私人笔记本电脑上,我有一条不同的错误消息:
It means: For the SMTP Server there is a secure connection neccesary, or the client was not authenticated.这意味着:对于 SMTP 服务器,需要安全连接,或者客户端未通过身份验证。 The Server answer was: 5.5.1 Authentication Required
服务器的回答是:5.5.1 需要身份验证
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) 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..有趣的是,VB.Net 应用程序甚至没有出现在那里。
It only show me the browser connection attempts to my account.它只显示浏览器连接到我的帐户的尝试。
Kr,氪,
Andreas安德烈亚斯
Answer #1 (unable to connect to the remote server)答案 #1(无法连接到远程服务器)
It look like you need to add this (specially line 1).看起来你需要添加这个(特别是第 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.
587 使用 TLS,465 使用 SSL。
To use 587 you should set SmtpClient.EnableSsl = true.
要使用 587,您应该设置 SmtpClient.EnableSsl = true。
465 wont work with SmtpClient, it will work with the deprecated class SmtpMail instead.
465 无法与 SmtpClient 一起使用,它将与已弃用的类 SmtpMail 一起使用。
Answer #2 (5.5.1 Authentication Required) for the error below:针对以下错误的答案 #2(需要 5.5.1 身份验证):
The SMTP server requires a secure connection or the client was not authenticated.
SMTP 服务器需要安全连接或客户端未通过身份验证。 The server response was: 5.5.1 Authentication Required.
服务器响应为:5.5.1 需要身份验证。
You need to allow your Google account to send e-mail using other devices/applications.您需要允许您的 Google 帐户使用其他设备/应用程序发送电子邮件。 See Google Account Security :
请参阅 Google 帐户安全:
Under the "Access for less secure apps" section, you can enable access to your account from other devices/applications... like your C# application.
在“访问不太安全的应用程序”部分下,您可以启用从其他设备/应用程序访问您的帐户...例如您的 C# 应用程序。
This code finally worked for me:这段代码终于对我有用:
I had to put SmtpServer.UseDefaultCredentials = False before I set the network credentials.在设置网络凭据之前,我必须设置 SmtpServer.UseDefaultCredentials = False。
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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.