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

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步验证,你的问题就解决了。

其他回答

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

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

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

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

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

尝试改变主机,这是一个新的,我得到了这个配置mozilla雷鸟

Host = "smtp.googlemail.com"

这对我很有用

2018年11月,我尝试了以上所有方法,但都没有成功。

下面是最终有效的解决方案。不幸的是,它不使用SSL,但它工作!!

var fromAddress = new MailAddress(asd@asd.com, "From Name");
var toAddress = new MailAddress("tosend@asd.com", "To Name");

const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
{
    Host = "aspmx.l.google.com",
    Port = 25,
    EnableSsl = false
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}

2022年5月30日之后,Gmail改变了对外部应用程序的政策 使用谷歌SMTP发送电子邮件详细信息。 Solition是登录谷歌帐户,您要发送电子邮件,启用谷歌两步验证,并为外部应用程序创建密码