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

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


当前回答

只需遵循谷歌电子邮件中的步骤,启用不太安全的应用程序。

其他回答

只需遵循谷歌电子邮件中的步骤,启用不太安全的应用程序。

我也遇到过同样的问题。当您打开2步验证(MFA)时,就会发生这种情况。 只要关闭2步验证,你的问题就解决了。

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

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

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

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

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

下面是我的代码。我也有同样的错误,但问题是我给我的密码错误。下面的代码将完美地工作..尝试一下

            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");             
            mail.From = new MailAddress("fromaddress@gmail.com");
            mail.To.Add("toaddress1@gmail.com");
            mail.To.Add("toaddress2@gmail.com");
            mail.Subject = "Password Recovery ";
            mail.Body += " <html>";
            mail.Body += "<body>";
            mail.Body += "<table>";
            mail.Body += "<tr>";
            mail.Body += "<td>User Name : </td><td> HAi </td>";
            mail.Body += "</tr>";

            mail.Body += "<tr>";
            mail.Body += "<td>Password : </td><td>aaaaaaaaaa</td>";
            mail.Body += "</tr>";
            mail.Body += "</table>";
            mail.Body += "</body>";
            mail.Body += "</html>";
            mail.IsBodyHtml = true;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("sendfrommailaddress.com", "password");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);

你可以在我的博客里查阅

我有一个以前工作的代码,现在抛出这个错误。密码没有问题。也不需要将消息转换为base64。事实证明,我需要做以下几点:

关闭双因素身份验证 将“允许不太安全的应用程序”设置为“开启” 从生产服务器登录到您的gmail帐户 也请到这里批准登录活动 在生产服务器上运行应用程序

工作代码

    public static void SendEmail(string emailTo, string subject, string body)
    {
        var client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential("youremail@gmail.com", "secretpassword"),
            EnableSsl = true
        };

        client.Send("youremail@gmail.com", emailTo, subject, body);
    }

关闭双重身份验证

将“允许不太安全的应用程序”设置为“开启”(同页,需要滚动到底部)