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

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


当前回答

我有一个以前工作的代码,现在抛出这个错误。密码没有问题。也不需要将消息转换为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);
    }

关闭双重身份验证

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

其他回答

当您尝试从不同的时区或IP地址计算机登录时,通常会发生这种情况。您的生产服务器和您使用的邮件id都位于不同的时区。以下两种方案任选其一:

1)通过远程访问登录生产服务器,并使用您的凭证登录gmail一次。他们会要求确认,确认后注销。

或者2)登录gmail到你的本地计算机,按照这个链接,选择审查这个活动,并采取适当的行动。

我尝试了这里找到的所有建议,从启用不太安全的应用程序,到尝试587端口……毫无效果。最后,我只注释掉了UseDefaultCredentials = false这一行。如果我不碰这个布尔值,一切都能正常工作。

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

我在部署到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";

对我有效的是激活不太安全的应用程序的选项(我使用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