我正在使用以下代码发送电子邮件。代码在我的本地机器上正常工作。但是在生产服务器上,我得到了错误消息

var fromAddress = new MailAddress("mymailid@gmail.com");
var fromPassword = "xxxxxx";
var toAddress = new MailAddress("yourmailid@yourdoamain.com");

string subject = "subject";
string body = "body";

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)       
};

using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})

smtp.Send(message);

在我的Gmail A/c上,我从生产服务器运行代码后收到了以下电子邮件

Hi , Someone recently used your password to try to sign in to your Google Account mymailid@gmail.com. This person was using an application such as an email, client or mobile device. We prevented the sign-in attempt in case this was a hijacker trying to access your account. Please review the details of the sign-in attempt: Friday, 3 January 2014 13:56:08 o'clock UTC IP Address: xxx.xx.xx.xxx (abcd.net.) Location: Philadelphia PA, Philadelphia, PA, USA If you do not recognise this sign-in attempt, someone else might be trying to access your account. You should sign in to your account and reset your password immediately. Reset password If this was you and you are having trouble accessing your account, complete the troubleshooting steps listed at http://support.google.com/mail?p=client_login Yours sincerely, The Google Accounts team


当前回答

您可能需要从gmail创建/生成一个特定的APP密码。 您的应用程序或脚本将使用这个新密码,而不是您的常规密码。 您的常规密码仍然可以正常工作。

这对我来说很重要。 我仍然使用相同的电子邮件帐户,但必须生成一个新的应用程序特定的密码。

https://support.google.com/accounts/answer/185833?hl=en

基本上你可以在这里做: https://security.google.com/settings/security/apppasswords

其他回答

对我有效的是激活不太安全的应用程序的选项(我使用VB.NET)

Public Shared Sub enviaDB(ByRef body As String, ByRef file_location As String)
        Dim mail As New MailMessage()
        Dim SmtpServer As New SmtpClient("smtp.gmail.com")
        mail.From = New MailAddress("from@gmail.com")
        mail.[To].Add("to@gmail.com")
        mail.Subject = "subject"
        mail.Body = body
        Dim attachment As System.Net.Mail.Attachment
        attachment = New System.Net.Mail.Attachment(file_location)
        mail.Attachments.Add(attachment)
        SmtpServer.Port = 587
        SmtpServer.Credentials = New System.Net.NetworkCredential("user", "password")
        SmtpServer.EnableSsl = True
        SmtpServer.Send(mail)
    End Sub

所以登录你的账户,然后去 google.com/settings/security/lesssecureapps

我真的看过很多想法,唯一的解决方案是这种方式(适用于不同的电子邮件提供商):

            try
        {
            ViewProgressbar("Try to connect mail-server...", progressBar1.Value = 20);
            string host = dsProvider.Rows[y]["POP_hostOut"].ToString();
            int port = int.Parse(dsProvider.Rows[y]["POP_portOut"].ToString());  //587
            string[] email = von1.Split('@');
            string userName = (dsProvider.Rows[y]["login"].ToString() == "email[0]@email[1]")? email[0]+"@"+email[1] : email[0];
            string password = layer.getUserPassword(listSender.SelectedValue.ToString());
            SmtpClient client = new SmtpClient(host, port);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            //A idea from MSDN but it not works. You got "The server response was: 5.5.1 Authentication Required."
            //System.Net.NetworkCredential myCreds = new System.Net.NetworkCredential(userName, password, host);
            //System.Net.CredentialCache cache = new System.Net.CredentialCache();
            //cache.Add(host, port, "NTLM", myCreds);
            ///cache.GetCredential(host, port, "NTLM");   //NTLM
            client.Credentials = new System.Net.NetworkCredential(userName, password);
            client.Host = host;
            client.Port = port;
            client.EnableSsl = true;
            client.Send(message);
            ViewProgressbar();
        }
        catch (SmtpException ex)...

Gmail/谷歌最近删除了(从2022年5月起)不安全的应用程序访问。他们是这么说的:

为确保您的账户安全,自2022年5月30日起,谷歌将不再支持仅使用用户名和密码登录谷歌账户的第三方应用程序或设备。

现在任何有直接SMTP电子邮件发送的电子邮件将抛出错误“535-5.7.8用户名和密码不接受”。这是因为现在谷歌要求应用程序特定的密码和电子邮件帐户密码不是应用程序密码,所以他们显示用户名/密码不接受。

您需要创建和使用应用程序特定的密码。应用程序密码就像你账户的备用密码一样。它只能被与您共享它的应用程序使用,因此它比共享您的主密码更安全。

创建应用程序特定的密码:

启用2因素认证。 进入Gmail的帐户设置,选择应用程序密码。 命名为任何您想要的名称,并创建密码。 使用这个新的16位密码和gmail电子邮件SMTP(在密码的地方使用这个16位密码)

享受发送邮件的乐趣!!

因此,正如其他人所说的那样,自5月30日起,谷歌不再支持使用第三方应用程序访问该帐户。

为了能够从我的代码访问帐户,我必须做的是首先启用两步验证。

一旦这样做,我创建了一个应用程序密码。

然后我将生成的密码替换为我用来访问我的帐户的密码。换句话说,你需要用这个新的应用程序密码删除gmail帐户密码(我们用来登录gmail的密码),功能就重新上线了!

我希望这能帮助到一些人:-)

更新2022

更不安全的应用程序方法不再工作

您应该迁移到GMail SMTP API

仅使用gmail id和密码发送电子邮件不再有效。您必须从谷歌云控制台设置api并创建OAuth凭据。

链接到谷歌云控制台

连结至官方指南

链接到一个简单的指南

原来的答案

这是一个安全问题。Gmail默认情况下阻止自定义应用程序访问您的电子邮件帐户。您可以将其设置为接受应用程序的登录。

登录您的电子邮件后,点击这里

这将带您到下面的页面