简体   繁体   English

通过 Gmail SMTP 服务器发送 email 与 C#

[英]Sending email through Gmail SMTP server with C#

For some reason neither the accepted answer nor any others work for me for " Sending email in .NET through Gmail ".由于某种原因,接受的答案和任何其他答案都不适用于“ 通过 Gmail 在 .NET 中发送 email ”。 Why would they not work?为什么他们不工作?

UPDATE: I have tried all the answers (accepted and otherwise) in the other question, but none of them work.更新:我已经尝试了另一个问题中的所有答案(接受的和其他的),但它们都不起作用。

I would just like to know if it works for anyone else, otherwise Google may have changed something (which has happened before).我只想知道它是否适用于其他任何人,否则谷歌可能已经改变了一些东西(以前发生过)。

When I try the piece of code that uses SmtpDeliveryMethod.Network , I quickly receive an SmtpException on Send(message).当我尝试使用SmtpDeliveryMethod.Network的代码时,我很快在 Send(message) 上收到了 SmtpException。 The message is消息是

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 需要验证。 Learn more at" <-- seriously, it ends there.了解更多信息”<——说真的,到此为止。

UPDATE:更新:

This is a question that I asked a long time ago, and the accepted answer is code that I've used many, many times on different projects.这是我很久以前提出的一个问题,公认的答案是我在不同项目中多次使用过的代码。

I've taken some of the ideas in this post and other EmailSender projects to create an EmailSender project at Codeplex .我采用了这篇文章和其他 EmailSender 项目中的一些想法,在 Codeplex 创建了一个 EmailSender 项目 It's designed for testability and supports my favourite SMTP services such as GoDaddy and Gmail.它专为可测试性而设计,并支持我最喜欢的 SMTP 服务,例如 GoDaddy 和 Gmail。

CVertex, make sure to review your code, and, if that doesn't reveal anything, post it. CVertex,请务必检查您的代码,如果没有发现任何内容,请将其发布。 I was just enabling this on a test ASP.NET site I was working on, and it works.我只是在我正在处理的测试 ASP.NET 站点上启用此功能,并且它有效。

Actually, at some point I had an issue on my code.实际上,在某些时候我的代码有问题。 I didn't spot it until I had a simpler version on a console program and saw it was working (no change on the Gmail side as you were worried about).我没有发现它,直到我在控制台程序上有一个更简单的版本并看到它正在工作(你担心的 Gmail 方面没有变化)。 The below code works just like the samples you referred to:下面的代码就像您提到的示例一样工作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
                EnableSsl = true
            };
            client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
            Console.WriteLine("Sent");
            Console.ReadLine();
        }
    }
}

I also got it working using a combination of web.config, http://msdn.microsoft.com/en-us/library/w355a94k.aspx and code (because there is no matching EnableSsl in the configuration file:( ).我还使用 web.config、 http://msdn.microsoft.com/en-us/library/w355a94k.aspx和代码的组合使其工作(因为配置文件中没有匹配的EnableSsl :()。

2021 Update 2021 更新

By default you will also need to enable access for "less secure apps" in your gmail settings page: google.com/settings/security/lesssecureapps.默认情况下,您还需要在 gmail 设置页面中启用“不太安全的应用程序”的访问权限:google.com/settings/security/lesssecureapps。 This is necessary if you are getting the exception "`The server response was: 5.5.1 Authentication Required. – thanks to @Ravendarksky如果您遇到异常“`服务器响应是:5.5.1 Authentication Required. - 感谢@Ravendarksky,这是必要的

THE FOLLOWING WILL ALMOST CERTAINLY BE THE ANSWER TO YOUR QUESTION IF ALL ELSE HAS FAILED :如果所有其他方法都失败了,以下几乎可以肯定是您问题的答案

I got the exact same error, it turns out Google's new password strength measuring algorithm has changed deeming my current password as too weak, and not telling me a thing about it (not even a message or warning)... How did I discover this?我得到了完全相同的错误,事实证明谷歌的新密码强度测量算法已经改变,认为我当前的密码太弱,并且没有告诉我任何关于它的事情(甚至没有消息或警告)......我是如何发现这个的? Well, I chose to change my password to see if it would help (tried everything else to no avail) and when I changed my password, it worked!好吧,我选择更改密码以查看是否有帮助(尝试了其他所有方法均无济于事),当我更改密码时,它起作用了!

Then, for an experiment, I tried changing my password back to my previous password to see what would happen, and Gmail didn't actually allow me to do this, citing the reason "sorry we cannot allow you to save this change as your chosen password is too weak" and wouldn't let me go back to my old password.然后,作为一个实验,我尝试将我的密码改回我以前的密码,看看会发生什么,而 Gmail 实际上并没有允许我这样做,理由是“对不起,我们不能让你按照你的选择保存这个更改密码太弱”,不会让我 go 回到我的旧密码。 I figured from this that it was erroring out because either a) you need to change your password once every x amount of months or b).我从中发现它出错了,因为a)您需要每x个月更改一次密码或b)。 As I said before, their password strength algorithms changed and therefore the weak password I had was not accepted, even though they did not say anything about this when trying to login ANYWHERE, This (number 2) is the most likely scenario, as my weak password was about 4 months old.正如我之前所说,他们的密码强度算法发生了变化,因此我的弱密码不被接受,即使他们在尝试登录 ANYWHERE 时没有说明任何内容,这(第 2 号)是最有可能的情况,因为我的弱密码大约有 4 个月的历史。 and it let me use it in Gmail.它让我可以在 Gmail 中使用它。

It's pretty bad that they said nothing about this, but it makes sense.他们对此只字未提,这很糟糕,但这是有道理的。 Because most hijacked emails are logged into using software outside of gmail, and I'm guessing you are required to have a stronger password if you want to use Gmail outside of the Gmail environment.因为大多数被劫持的电子邮件都是使用 gmail 之外的软件登录的,我猜如果你想在 Gmail 环境之外使用 Gmail,你需要有一个更强的密码。

In addition to the other troubleshooting steps above, I would also like to add that if you have enabled two-factor authentication (also known as two-step verification ) on your GMail account, you must generate an application-specific password and use that newly generated password to authenticate via SMTP .除了上述其他故障排除步骤之外,我还想补充一点,如果您在 GMail 帐户上启用了双因素身份验证(也称为两步验证),则必须生成一个应用程序专用密码并重新使用该密码生成的密码通过 SMTP 进行身份验证

To create one, visit: https://www.google.com/settings/ and choose Authorizing applications & sites to generate the password.要创建一个,请访问: https://www.google.com/settings/并选择授权应用程序和站点以生成密码。

Turn On Access For Less Secure Apps and it will work for all no need to change password.

Link to Gmail Setting链接到 Gmail 设置

在此处输入图像描述

I've had some problems sending emails from my gmail account too, which were due to several of the aforementioned situations.我在从我的 gmail 帐户发送电子邮件时也遇到了一些问题,这是由于上述几种情况。 Here's a summary of how I got it working, and keeping it flexible at the same time:以下是我如何让它工作的摘要,同时保持它的灵活性:

  • First of all setup your GMail account:首先设置您的 GMail 帐户:
    • Enable IMAP and assert the right maximum number of messages (you can do so here)启用 IMAP 并声明正确的最大消息数(您可以在此处执行此操作)
    • Make sure your password is at least 7 characters and is strong (according to Google)确保您的密码至少包含 7 个字符且强度高(根据 Google 的说法)
    • Make sure you don't have to enter a captcha code first.确保您不必先输入验证码。 You can do so by sending a test email from your browser.您可以通过从浏览器发送测试 email 来做到这一点。
  • Make changes in web.config (or app.config, I haven't tried that yet but I assume it's just as easy to make it work in a windows application):在 web.config 中进行更改(或 app.config,我还没有尝试过,但我认为让它在 windows 应用程序中工作同样容易):
<configuration>
    <appSettings>
        <add key="EnableSSLOnMail" value="True"/>   
    </appSettings>

    <!-- other settings --> 

    ...

    <!-- system.net settings -->
    <system.net>
        <mailSettings>
            <smtp from="yourusername@gmail.com" deliveryMethod="Network">
                <network 
                    defaultCredentials="false" 
                    host="smtp.gmail.com" 
                    port="587" 
                    password="stR0ngPassW0rd" 
                    userName="yourusername@gmail.com"
                    />
                <!-- When using .Net 4.0 (or later) add attribute: enableSsl="true" and you're all set-->
            </smtp>
        </mailSettings>
    </system.net>
</configuration>
Add a Class to your project:

Imports System.Net.Mail

Public Class SSLMail

    Public Shared Sub SendMail(ByVal e As System.Web.UI.WebControls.MailMessageEventArgs)

        GetSmtpClient.Send(e.Message)

        'Since the message is sent here, set cancel=true so the original SmtpClient will not try to send the message too:
        e.Cancel = True

    End Sub

    Public Shared Sub SendMail(ByVal Msg As MailMessage)
        GetSmtpClient.Send(Msg)
    End Sub

    Public Shared Function GetSmtpClient() As SmtpClient

        Dim smtp As New Net.Mail.SmtpClient
        'Read EnableSSL setting from web.config
        smtp.EnableSsl = CBool(ConfigurationManager.AppSettings("EnableSSLOnMail"))
        Return smtp
    End Function

End Class

And now whenever you want to send emails all you need to do is call SSLMail.SendMail :现在,每当您想发送电子邮件时,您只需调用SSLMail.SendMail

eg in a Page with a PasswordRecovery control:例如在带有 PasswordRecovery 控件的页面中:

Partial Class RecoverPassword
Inherits System.Web.UI.Page

Protected Sub RecoverPwd_SendingMail(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MailMessageEventArgs) Handles RecoverPwd.SendingMail
    e.Message.Bcc.Add("webmaster@example.com")
    SSLMail.SendMail(e)
End Sub

End Class

Or anywhere in your code you can call:或者您可以在代码中的任何位置调用:

SSLMail.SendMail(New system.Net.Mail.MailMessage("from@from.com","to@to.com", "Subject", "Body"})

I hope this helps anyone who runs into this post.我希望这对遇到这篇文章的人有所帮助。 (I used VB.NET but I think it's trivial to convert it to any .NET language.) (我使用了 VB.NET 但我认为将其转换为任何 .NET 语言都很简单。)

Oh...It's amazing... First I Couldn't send an email for any reasons.哦……太棒了……首先,由于任何原因,我无法发送 email。 But after I changed the sequence of these two lines as below, it works perfectly.但是在我如下改变这两行的顺序之后,它就可以完美地工作了。

//(1)
client.UseDefaultCredentials = true;
//(2)
client.Credentials = new System.Net.NetworkCredential("username@gmail.com", "password");
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")

If you get an error like "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at" as I get before this, make sure the line SMTPClientObj.UseDefaultCredentials = False included and this line should before the SMTPClientObj.Credentials .如果您收到类似“SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。在此了解更多信息”之类的错误,请确保行SMTPClientObj.UseDefaultCredentials = False包含SMTPClientObj.UseDefaultCredentials = False并且此行应该在SMTPClientObj.Credentials之前。

I did try to switch these 2 lines the opposite way and the 5.5.1 Authentication Required error returned.我确实尝试以相反的方式切换这两行,并返回了5.5.1 Authentication Required 错误

The problem is not one of technical ability to send through gmail.问题不是通过gmail发送的技术能力之一。 That works for most situations.这适用于大多数情况。 If you can't get a machine to send, it is usually due to the machine not having been authenticated with a human at the controls at least once.如果您无法让机器发送,这通常是由于该机器没有至少一次通过控制人员的身份验证。

The problem that most users face is that Google decides to change the outbound limits all the time.大多数用户面临的问题是谷歌决定一直更改出站限制。 You should always add defensive code to your solution.您应该始终将防御性代码添加到您的解决方案中。 If you start seeing errors, step off your send speed and just stop sending for a while.如果您开始看到错误,请降低发送速度并停止发送一段时间。 If you keep trying to send Google will sometimes add extra time to your delay period before you can send again.如果您继续尝试发送,Google 有时会在您再次发送之前增加额外的延迟时间。

What I have done in my current system is to send with a 1.5 second delay between each message.我在当前系统中所做的是在每条消息之间延迟 1.5 秒发送。 Then if I get any errors, stop for 5 minutes and then start again.然后,如果我遇到任何错误,请停止 5 分钟,然后重新开始。 This usually works and will allow you to send up to the limits of the account (last I checked it was 2,000 for premier customer logins per day).这通常有效,并且允许您发送到帐户的限制(最后我检查它是每天 2,000 的主要客户登录)。

Simple steps to fix this:解决此问题的简单步骤:

1)Sign in to your Gmail 1) 登录您的 Gmail

2)Navigate to this page https://www.google.com/settings/security/lesssecureapps & set to "Turn On" 2)导航到此页面https://www.google.com/settings/security/lesssecureapps并设置为“打开”

If nothing else has worked here for you you may need to allow access to your gmail account from third party applications.如果这里没有其他方法对您有用,您可能需要允许从第三方应用程序访问您的 gmail 帐户。 This was my problem.这是我的问题。 To allow access do the following:要允许访问,请执行以下操作:

  1. Sign in to your gmail account.登录您的 gmail 帐户。
  2. Visit this page https://accounts.google.com/DisplayUnlockCaptcha and click on button to allow access.访问此页面https://accounts.google.com/DisplayUnlockCaptcha并单击按钮以允许访问。
  3. Visit this page https://www.google.com/settings/security/lesssecureapps and enable access for less secure apps.访问此页面https://www.google.com/settings/security/lesssecureapps并启用安全性较低的应用程序的访问权限。

This worked for me hope it works for someone else!这对我有用,希望对其他人有用!

I had the same problem, but it turned out to be my virus protection was blocking outgoing "spam" email.我遇到了同样的问题,但事实证明是我的病毒防护阻止了传出的“垃圾邮件”email。 Turning this off allowed me to use port 587 to send SMTP email via GMail关闭它允许我使用端口 587 通过 GMail 发送 SMTP email

I'm not sure which .NET version is required for this because eglasius mentioned there is no matching enableSsl setting (I'm using .NET 4.0, but I suspect it to work in .NET 2.0 or later), but this configuration justed worked for me (and doesn't require you to use any programmatic configuration):我不确定为此需要哪个 .NET 版本,因为 eglasius 提到没有匹配enableSsl设置(我使用的是 .NET 4.0,但我怀疑它可以在 Z303CB0EF9EDB9082D61 中工作,但这个配置适用于我 BBBE5825D972AZ 或更高版本)并且不需要您使用任何编程配置):

<system.net>
  <mailSettings>
    <smtp from="myusername@gmail.com" deliveryMethod="Network">
      <network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" port="587" password="password" userName="myusername@gmail.com"/>
    </smtp>
  </mailSettings>
</system.net>

You might have to enable POP or IMAP on your Gmail account first: https://mail.google.com/mail/?shva=1#settings/fwdandpop您可能必须先在 Gmail 帐户上启用 POP 或 IMAP: https://mail.google.com/mail/?shva=1#settings/fwdandpop

I recommend trying it with a normal mail client first...我建议先用普通的邮件客户端试试...

I was using corporate VPN connection.我使用的是企业 VPN 连接。 It was the reason why I couldn't send email from my application.这就是我无法从我的应用程序中发送 email 的原因。 It works if I disconnect from VPN.如果我断开与 VPN 的连接,它会起作用。

@Andres Pompiglio: Yes that's right you must change your password at least once.. this codes works just fine: @Andres Pompiglio:是的,您必须至少更改一次密码。此代码可以正常工作:

//Satrt Send Email Function
public string SendMail(string toList, string from, string ccList,
    string subject, string body)
{

    MailMessage message = new MailMessage();
    SmtpClient smtpClient = new SmtpClient();
    string msg = string.Empty;
    try
    {
        MailAddress fromAddress = new MailAddress(from);
        message.From = fromAddress;
        message.To.Add(toList);
        if (ccList != null && ccList != string.Empty)
            message.CC.Add(ccList);
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        // We use gmail as our smtp client
        smtpClient.Host = "smtp.gmail.com";   
        smtpClient.Port = 587;
        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new System.Net.NetworkCredential(
            "Your Gmail User Name", "Your Gmail Password");

        smtpClient.Send(message);
        msg = "Successful<BR>";
    }
    catch (Exception ex)
    {
        msg = ex.Message;
    }
    return msg;
}
//End Send Email Function

AND you can make a call to the function by using:并且您可以使用以下方法调用 function:

Response.Write(SendMail(recipient Address, "UserName@gmail.com", "ccList if any", "subject", "body"))

I was getting the same error and none of the above solutions helped.我遇到了同样的错误,上述解决方案都没有帮助。

My problem was that I was running the code from a remote server, which had never been used to log into the gmail account.我的问题是我从远程服务器运行代码,该服务器从未用于登录 gmail 帐户。

I opened a browser on the remote server and logged into gmail from there.我在远程服务器上打开了一个浏览器并从那里登录到 gmail。 It asked a security question to verify it was me since this was a new location.它询问了一个安全问题来验证它是我,因为这是一个新位置。 After doing the security check I was able to authenticate through code.完成安全检查后,我能够通过代码进行身份验证。

I ran into this same error ( "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at" ) and found out that I was using the wrong password.我遇到了同样的错误( “SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。了解更多信息” )并发现我使用了错误的密码。 I fixed the login credentials, and it sent correctly.我修复了登录凭据,并正确发送。

I know this is late, but maybe this will help someone else.我知道这已经晚了,但也许这会对其他人有所帮助。

Turn on less secure apps for your account: https://www.google.com/settings/security/lesssecureapps为您的帐户打开不太安全的应用程序: https://www.google.com/settings/security/lesssecureapps

  1. First check your gmail account setting & turn On from "Access for less secure apps"首先检查您的 gmail 帐户设置并从“访问不太安全的应用程序”中打开在此处输入图像描述

We strongly recommend that you use a secure app, like Gmail, to access your account.我们强烈建议您使用 Gmail 等安全应用程序来访问您的帐户。 All apps made by Google meet these security standards. Google 制作的所有应用都符合这些安全标准。 Using a less secure app, on the other hand, could leave your account vulnerable.另一方面,使用安全性较低的应用程序可能会使您的帐户容易受到攻击。 Learn more.学到更多。

  1. Set

    smtp.UseDefaultCredentials = false;

    before

    smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);

I also found that the account I used to log in was de-activated by google for some reason.我还发现我用来登录的帐户由于某种原因被谷歌停用了。 Once I reset my password (to the same as it used to be), then I was able to send emails just fine.一旦我重置了我的密码(和以前一样),我就可以发送电子邮件了。 I was getting 5.5.1 message also.我也收到了 5.5.1 消息。

I had also try to many solution but make some changes it will work我也尝试了很多解决方案,但做了一些改变它会起作用

host = smtp.gmail.com
port = 587
username = email@gmail.com
password = password
enabledssl = true

with smtpclient above parameters are work in gmail使用 smtpclient 以上参数在 gmail 中工作

Another thing that I've found is that you must change your password at least once.我发现的另一件事是您必须至少更改一次密码。 And try to use a secure level password (do not use the same user as password, 123456, etc.)并尽量使用安全级别的密码(不要使用与密码相同的用户,123456等)

Yet another possible solution for you.为您提供另一种可能的解决方案。 I was having similar problems connecting to gmail via IMAP.我在通过 IMAP 连接到 gmail 时遇到了类似的问题。 After trying all the solutions that I came across that you will read about here and elsewhere on SO (eg. enable IMAP, enable less secure access to your mail, using https://accounts.google.com/b/0/displayunlockcaptcha and so on), I actually set up a new gmail account once more.在尝试了我遇到的所有解决方案之后,您将在此处和其他地方阅读 SO(例如启用 IMAP,启用对您的邮件的不太安全的访问,使用https://accounts.google.com/b/0/displayunlockcaptcha和等等),我实际上再次设置了一个新的 gmail 帐户。

In my original test, the first gmail account I had created, I had connected to my main gmail account.在我最初的测试中,我创建的第一个 gmail 帐户已连接到我的主 gmail 帐户。 This resulted in erratic behaviour where the wrong account was being referenced by google.这导致了谷歌引用错误帐户的不稳定行为。 For example, running https://accounts.google.com/b/0/displayunlockcaptcha opened up my main account rather than the one I had created for the purpose.例如,运行https://accounts.google.com/b/0/displayunlockcaptcha打开了我的主帐户,而不是我为此目的创建的帐户。

So when I created a new account and did not connect it to my main account, after following all the appropriate steps as above, I found that it worked fine!因此,当我创建了一个新帐户并且没有将其连接到我的主帐户时,按照上述所有适当的步骤进行操作后,我发现它工作正常!

I haven't yet confirmed this (ie. reproduced), but it apparently did it for me...hope it helps.我还没有证实这一点(即转载),但它显然是为我做的......希望它有所帮助。

One or more reasons are there for these error.这些错误有一个或多个原因。

  • Log in with your Gmail( or any other if ) in your local system.在本地系统中使用您的 Gmail(或任何其他 if )登录。

  • Also check for Less Secure App and Set it to "Turn On" here is the Link for GMAIL.还要检查不太安全的应用程序并将其设置为“打开”这里是 GMAIL 的链接。 https://www.google.com/settings/security/lesssecureapps https://www.google.com/settings/security/lesssecureapps

  • check for EnableSsl in your Email code, and also set it to true.检查 Email 代码中的 EnableSsl,并将其设置为 true。

     smtp.EnableSsl = true;
  • Also check which port are you using currently.还要检查您当前使用的端口。 25 is Global, but you can check it for others too like 587. check here. 25 是全球性的,但您也可以为其他人检查它,例如 587。检查这里。 Does all SMTP communication happen over 25? 是否所有 SMTP 通信都发生在 25 岁以上?

  • IF YOU ARE ON REMOTE : Check the answer of Vlad Tamas above.如果您在远程:检查上面Vlad Tamas的答案。

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.您也可以通过端口 465 进行连接,但由于 System.Net.Mail 命名空间的一些限制,您可能必须更改您的代码。 This is because the namespace does not offer the ability to make implicit SSL connections.这是因为命名空间不提供建立隐式 SSL 连接的能力。 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 ).这在http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx进行了讨论,我在另一个讨论中提供了如何使用 CDO(协作数据对象)的示例( 所有端口上的 GMail SMTP 通过 C#.Net 错误)。

If you have 2-Step Verification step up on your Gmail account, you will need to generate an App password.如果您的 Gmail 帐户有两步验证步骤,您将需要生成应用程序密码。 https://support.google.com/accounts/answer/185833?p=app_passwords_sa&hl=en&visit_id=636903322072234863-1319515789&rd=1 Select How to generate an App password option and follow the steps provided. https://support.google.com/accounts/answer/185833?p=app_passwords_sa&hl=en&visit_id=636903322072234863-1319515789&rd=1 ZE0626222614BDEE31951D846 提供的步骤如何生成应用程序的步骤 BDEE31951D8466。 Copy and paste the generated App password somewhere as you will not be able to recover it after you click DONE.将生成的应用程序密码复制并粘贴到某处,因为单击完成后您将无法恢复它。

I had this problem resolved.我已经解决了这个问题。 Aparently that message is used in multiple error types.显然,该消息用于多种错误类型。 My problem was that i had reached my maximum of 500 sent mails.我的问题是我已达到最多 500 封已发送邮件。

log into the account and manually try to send a mail.登录帐户并手动尝试发送邮件。 If the limit has been reached it will inform you如果已达到限制,它将通知您

smtp.Host = "smtp.gmail.com"; //host name
smtp.Port = 587; //port number
smtp.EnableSsl = true; //whether your smtp server requires SSL
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;

Change your gmail password and try again, it should work after that.更改您的 gmail 密码并重试,之后应该可以使用。

Don't know why, but every time you change your hosting you have to change your password.不知道为什么,但是每次您更改主机时,您都必须更改密码。

This code works for me, and also allows in gmail access from unsecure apps, and removing authentication in 2 steps:此代码适用于我,还允许在 gmail 中从不安全的应用程序访问,并通过 2 个步骤删除身份验证:

Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(MailAddress, Password)

Google no longer allows "less secure apps", so this is not possible any longer for normal gmail users. Google 不再允许“不太安全的应用程序”,因此对于普通 gmail 用户来说,这不再是可能的。

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

From Google:来自谷歌:

To help keep your account secure, from May 30, 2022 , Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.为了帮助确保您的帐户安全,从2022 年 5 月 30 日起,Google 不再支持使用第三方应用或设备,这些应用或设备要求您仅使用您的用户名和密码登录您的 Google 帐户。

Important: This deadline does not apply to Google Workspace or Google Cloud Identity customers.重要提示:此截止日期不适用于 Google Workspace 或 Google Cloud Identity 客户。 The enforcement date for these customers will be announced on the Workspace blog at a later date.这些客户的执行日期将在稍后的 Workspace 博客上公布。

For more information, continue to read.欲了解更多信息,请继续阅读。

Google no longer allows "less secure apps", so this is not possible for normal Gmail users. Google 不再允许“安全性较低的应用程序”,因此普通 Gmail 用户无法做到这一点。

Changes by Google after May 2022. 2022 年 5 月之后 Google 所做的更改。

For these changes, we have to do the following changes to our code.对于这些更改,我们必须对我们的代码进行以下更改。

  1. First go to Gmail account security.首先 go 到 Gmail 帐户安全。

  2. Enable two-factor authentication.启用双重身份验证。

  3. Configure the App password in the google account.在 google 账户中配置 App 密码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM