2013-02-14 220 views
0

我配置我的Outlook 2010的thie article發送和接收來自yahoo.com的電子郵件,它工作正常,沒有任何問題。雅虎smtp發送電子郵件時發生錯誤

我開發一個小應用程序通過我的應用程序發送我的電子郵件,但它給我的錯誤:

"unable to read data from the transport connection:an exist connection was 
forcibly closed by the remote host." 

我的代碼:

try 
     { 

      SmtpClient smtp = new SmtpClient("smtp.mail.yahoo.com", 465); 
      smtp.UseDefaultCredentials = true; 
      smtp.EnableSsl = true; 
      smtp.Credentials = new System.Net.NetworkCredential("myid", "mypass"); 

      smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 

      MailMessage mailMessage = new MailMessage(); 
      mailMessage.From = new System.Net.Mail.MailAddress("[email protected]", "blabla"); 
      mailMessage.To.Add(new System.Net.Mail.MailAddress("[email protected]", "[email protected]")); 
      mailMessage.Subject = "test"; 
      mailMessage.Body = "test"; 
      mailMessage.IsBodyHtml = false; 

      mailMessage.SubjectEncoding = System.Text.Encoding.UTF8; 
      mailMessage.BodyEncoding = System.Text.Encoding.UTF8; 

      mailMessage.Priority = MailPriority.High; 


      smtp.Send(mailMessage); 

      Console.WriteLine("hooooooooooraaaaaaaaaaaaaaa"); 
      Console.ReadKey(); 
     } 
     catch (Exception err) 
     { 

      Console.WriteLine(err.InnerException.Message); 
      Console.ReadKey(); 
      return; 
     } 
+0

我不認爲你應該使用'UseDefaultCredentials'屬性以及'Credentials'。我會將UseDefaultCredentials設置爲false。該屬性將導致SmtpClient嘗試使用當前用戶的系統憑據 – 2013-02-14 21:56:18

+0

@Adam我通過smtp.UseDefaultCredentials = false測試了它;不工作具有相同的錯誤,但感謝您的幫助 – motevalizadeh 2013-02-14 21:58:53

+0

請看一下:http:/ /stackoverflow.com/questions/5092235/c-sharp-smtp-email-sending-code-fails-for-yahoo-mail-but-works-fine-for-other-se – 2013-02-14 22:02:28

回答

0

From MSDN

一些SMTP服務器要求客戶端在 服務器代表它發送電子郵件之前進行身份驗證。如果服務器請求,此 SmtpClient對象應該使用當前登錄的用戶的默認憑據來認證 ,請將此屬性設置爲true。對於 客戶端應用程序,這是大多數情況下所需的行爲。

UseDefaultCredentials = true向SMTP服務器發送當前登錄用戶(即Windows用戶)的憑證而不是您定義的憑證。 嘗試UseDefaultCredentials = false

+0

我測試了它的smtp.UseDefaultCredentials = false ;,不工作有相同的錯誤,但謝謝 – motevalizadeh 2013-02-14 22:00:38

相關問題