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

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帐户后。我输入了这个链接:https://www.google.com/settings/security/lesssecureapps

然后我打开安全应用程序,它就工作了。 上面也说过

其他回答

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

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

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

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

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

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

享受发送邮件的乐趣!!

我在部署到Microsoft Azure的应用程序中遇到了同样的问题。

SmtpException: SMTP服务器需要安全连接,或者SMTP服务器需要安全连接 客户端未被验证。服务器的响应是:5.5.1 认证要求。

首先,我在以下页面上批准了所有未知设备(一些来自爱尔兰的ip地址)(以gmail用户登录): https://security.google.com/settings/u/1/security/secureaccount

我对客户端进行了如下设置:

var client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("my_user_name@gmail.com", "my_password"); 

只有当我在smtp-client上设置了以下属性后,它才开始工作:

client.TargetName = "STARTTLS/smtp.gmail.com";

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

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);
}

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

Host = "smtp.googlemail.com"

这对我很有用