【问题标题】:Sending Mail using gmail SMTP from CSharp/.net从 CSharp/.net 使用 gmail SMTP 发送邮件
【发布时间】:2015-05-28 20:37:52
【问题描述】:
using(SmtpClient client = new SmtpClient("smtp.gmail.com", 587)) 
{
    // Configure the client
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential(textBox1.Text, textBox3.Text);

    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    MailMessage message = new MailMessage(
    textBox1.Text, // From field
    textBox2.Text, // Recipient field
    textBox4.Text, // Subject of the email message
    richTextBox1.Text // Email message body
    );

    client.Send(message);

    MessageBox.Show("Email has been sent.");
}

Error: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

我在使用 gmail 时遇到了这个错误,但我可以使用其他 SMTP 服务器来发送邮件。凭据正确。

【问题讨论】:

标签: c# .net smtp gmail


【解决方案1】:

Gmail 帐户似乎存在安全问题

我也遇到了同样的问题,然后从this post找到了解决方案。

帖子提到您需要在启用“访问不太安全的应用程序”的情况下更改帐户权限设置。

事实上,当您登录到您的 gmail 帐户时,您会收到通知。

【讨论】:

  • 我也有同样的问题,但是你的解决方案不能解决我的问题。。当我发送邮件时,服务不可用,对不起我的英语不好
【解决方案2】:
    message.To.Add(new MailAddress("abc@abc.com));  // replace with valid value 
        message.From = new MailAddress("abc@abc.com", "Contact Form");
        message.Subject = Subject;
        message.Body = string.Format(NewBody);
        message.IsBodyHtml = true;
        using (var smtp = new SmtpClient())
        {
            var credential = new NetworkCredential
            {
                UserName = "abc@gmail.com",  // replace with valid value
                Password = "qwerty123456"  // replace with valid value
            };
            smtp.Credentials = credential;
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Send(message);
        }

这只是我的工作代码,你可以使用它,这个代码的唯一问题是有时邮件会进入垃圾邮件文件夹。

【讨论】:

  • 这将有助于指定“有效值”的含义; NetworkCredential.Password 中使用什么密码;是 Google 密码还是电子邮件帐户密码?
【解决方案3】:

您需要为您的 Gmail 帐户启用两步验证并获取应用程序密码。然后使用该密码而不是常规密码。

【讨论】:

  • 嗨苏尔科夫。当您发布答案时,您还应该解释如何做而不只是做什么。
猜你喜欢
  • 2014-08-27
  • 2013-02-03
  • 1970-01-01
  • 2017-11-14
  • 2015-10-22
  • 2015-09-28
  • 1970-01-01
  • 2021-05-08
相关资源
最近更新 更多