2015-03-25 215 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