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

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


当前回答

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

其他回答

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

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

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

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

在今天花了几个小时尝试了这里的每个解决方案之后,我仍然无法克服这个错误。我以这种方式多次使用gmail,所以我知道这是一个愚蠢的东西,但我没有做任何事情来解决这个问题。我终于偶然发现了我的解决方案,所以我想分享一下。

首先,上面的大多数答案也是必需的,但在我的例子中,这只是在创建SmtpClient类时对代码进行排序的简单问题。

在下面的第一个代码片段中,请注意Credentials = creds行所在的位置。这个实现将生成这个问题中引用的错误,即使您已经正确设置了其他所有内容。

System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient
{
    Host = Emailer.Host,
    Port = Emailer.Port,
    Credentials = creds,
    EnableSsl = Emailer.RequireSSL,
    UseDefaultCredentials = false,
    DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network
}

但是,如果您将凭证设置调用移到底部,则电子邮件将正常发送。我没有改变周围的代码…用户名/密码等。显然,无论是EnableSSL、UseDefaultCredentials还是DeliveryMethod都依赖于先设置的凭证…不过,我并没有全部测试以确定是哪一个。

System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient
{
    Host = Emailer.Host,
    Port = Emailer.Port,
    EnableSsl = Emailer.RequireSSL,
    UseDefaultCredentials = false,
    DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
    Credentials = creds
}

希望这能帮助其他人在未来避免一些头痛。

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

            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)...

不要在等待smtp.SendMailAsync(邮件)前设置断点;

:)

当它等待一个断点给出这个错误时,当我删除断点时,它已经工作了