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

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


当前回答

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

Host = "smtp.googlemail.com"

这对我很有用

其他回答

当您尝试从代码发送邮件时,发现错误“SMTP服务器需要安全连接或客户端未经过身份验证。服务器的响应是:5.5.1 Authentication Required”,错误可能发生在以下情况下。

情况1:密码错误

情况2:当你试图从某个App登录时

情况3:当您试图从您的时区/域/计算机以外的域登录时 (从代码发送邮件时,大多数情况都是这样)

每个问题都有解决方案

解决方法:输入正确的密码。

情况2的解决方案1:进入以下链接https://www.google.com/settings/security/lesssecureapps的安全设置,启用不太安全的应用程序。这样你就可以从所有的应用程序登录。

情况2的解决方案2:(参见https://stackoverflow.com/a/9572958/52277)启用双因素身份验证(又名两步验证),然后生成特定于应用程序的密码。使用新生成的密码通过SMTP进行身份验证。

情况3的解决方案1:(这可能有帮助)您需要回顾活动。但由于最新的安全标准,审查活动将没有帮助,链接将没有用处。所以试试下面的例子。

针对情况3的解决方案2:如果您将代码托管在生产服务器上的某个地方,并且可以访问生产服务器,那么请将远程桌面连接到生产服务器,并尝试从生产服务器的浏览器登录一次。 这将为登录谷歌添加奖励,您将被允许从代码登录。

但是如果您无法访问生产服务器呢? 尝试解决方案3

情况3的解决方案3:您必须为您的谷歌帐户启用从其他时区/ ip登录。

要做到这一点,请访问https://g.co/allowaccess链接,并通过单击继续按钮允许访问。

就是这样。给你。现在,您将能够从任何一台计算机和任何方式的应用程序登录到您的谷歌帐户。

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

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

你可以在我的博客里查阅

Tomasz Madeyski的评论解决了我的问题…他告诉存在一个错误的SetDefaultCredential,他说:

问题是在UseDefaultCredentials setter中有这样的代码:this.transport. credentials = value ?(ICredentialsByHost) CredentialCache。DefaultNetworkCredentials: (ICredentialsByHost) null;它覆盖由凭证设置器设置的凭证。对我来说,它看起来像SmtpClient的bug。”

如果你输入smtpClient。设置凭证后UseDefaultCredentials = false…这一行设置为空这些凭据…

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

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

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