2015-03-25 216 views
1
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从CSHARP/.NET

发送邮件我已经收到此错误与Gmail,但我能够使用其他SMTP服务器来发送邮件。凭据是正确的。

+0

阅读此链接: http://email.about.com/od/gmailtips/qt/How-To-Unlock-Gmail-For-A-New-Email-Program-Or-Service.htm – 2015-03-25 09:08:11

+0

选中此。 http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp – 2015-03-25 09:08:29

+0

@ZoharPeled链接工作,我能够发送邮件..谢谢 – sooraj 2015-03-25 09:14:30

回答

5

看起来像是有一个安全问题与Gmail帐户。

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

该帖子提到您需要更改“允许访问安全性较低的应用程序”的帐户权限设置。

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

+0

我有同样的问题,但你的解决方案不能解决我的问题..当我发送邮件,服务不aviable,对不起我的英文不好, – 2016-02-12 06:41:14

2
message.To.Add(new MailAddress("[email protected])); // replace with valid value 
     message.From = new MailAddress("[email protected]", "Contact Form"); 
     message.Subject = Subject; 
     message.Body = string.Format(NewBody); 
     message.IsBodyHtml = true; 
     using (var smtp = new SmtpClient()) 
     { 
      var credential = new NetworkCredential 
      { 
       UserName = "[email protected]", // 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); 
     } 

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

+0

它会帮助指定“有效值”的含义; NetworkCredential.Password中使用了什么密码;是Google密码还是电子邮件帐户密码? – user34660 2017-02-16 18:01:33

0

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

+0

嗨Surkov。当你发布答案时,你还应该解释如何去做,而不仅仅是做什么。 – 2017-05-30 11:14:42