出于某种原因,既不是接受的答案,也没有其他工作为我“通过Gmail在。net发送电子邮件”。为什么它们不会起作用?

更新:我已经在另一个问题中尝试了所有的答案(接受的和其他的),但没有一个有效。

我只是想知道它是否适用于其他人,否则谷歌可能已经改变了一些东西(以前发生过)。

当我尝试使用SmtpDeliveryMethod的代码段时。网络,我很快收到一个SmtpException发送(消息)。信息是

SMTP服务器需要安全连接,否则客户端未经过身份验证。

服务器的回复是:

5.5.1认证要求在“<——说真的,它到此结束。

更新:

这是我很久以前问过的一个问题,公认的答案是我在不同的项目中使用过很多很多次的代码。

我采用了这篇文章和其他EmailSender项目中的一些想法,在Codeplex上创建了一个EmailSender项目。它是为可测试性而设计的,支持我最喜欢的SMTP服务,如GoDaddy和Gmail。


当前回答

Dim SMTPClientObj As New Net.Mail.SmtpClient
SMTPClientObj.UseDefaultCredentials = False
SMTPClientObj.Credentials = New System.Net.NetworkCredential("myusername@gmail.com", "mypwd")
SMTPClientObj.Host = "smtp.gmail.com"
SMTPClientObj.Port = 587
SMTPClientObj.EnableSsl = True
SMTPClientObj.Send("myusername@gmail.com","yourusername@gmail.com","test","testbody")

如果您得到诸如“SMTP服务器需要安全连接或客户端未经过身份验证。服务器的响应是:5.5.1需要身份验证。要了解更多信息,请访问“在此之前,确保行SMTPClientObj。UseDefaultCredentials = False,这一行应该在smtpclientb . credentials之前。

我确实尝试将这两行反向切换,并返回5.5.1 Authentication Required错误。

其他回答

我也遇到了同样的问题,但原来是我的病毒防护程序阻止了发出的“垃圾邮件”。关闭这个允许我使用端口587通过GMail发送SMTP电子邮件

You can also connect via port 465, but due to some limitations of the System.Net.Mail namespace you may have to alter your code. This is because the namespace does not offer the ability to make implicit SSL connections. This is discussed at http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx, and I have supplied an example of how to use the CDO (Collaborative Data Object) in another discussion (GMail SMTP via C# .Net errors on all ports).

谷歌不再允许“不太安全的应用程序”,所以这对普通Gmail用户来说是不可能的。

2022年5月后谷歌更改。

对于这些更改,我们必须对代码进行以下更改。

First go to Gmail account security. Enable two-factor authentication. Configure the App password in the google account. Use the App password in the application. MailMessage mail = new MailMessage(); mail.To.Add(email.Text.ToString().Trim()); mail.From = new MailAddress("your_Gmail_address"); mail.Subject = "Hello test email"; mail.Body = "<p>hello user<br/> How are you?</p>"; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Port = 587; // 25 465 smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Host = "smtp.gmail.com"; smtp.Credentials = new System.Net.NetworkCredential("your_Gmail_address", "Your_gmail_app_password"); smtp.Send(mail);

谷歌不再允许“不太安全的应用程序”,所以这对普通gmail用户来说是不可能的。

https://support.google.com/accounts/answer/6010255?hl=en#zippy=%2Cif-less-secure-app-access-is-on-for-your-account

从谷歌:

为了保证您的账户安全,从2022年5月30日起,谷歌号 long支持使用第三方应用程序或设备来询问您 仅使用您的用户名和登录您的谷歌帐户 密码。 重要提示:此截止日期不适用于谷歌工作区或谷歌工作区 云身份客户。这些客户的执行日期 将于晚些时候在工作空间博客上公布。 欲了解更多信息,请继续阅读。

解决这个问题的简单步骤:

1)登录你的Gmail

2)导航到此页面https://www.google.com/settings/security/lesssecureapps并设置“打开”