2015-11-19 68 views
0

錯誤:異常,同時發送電子郵件

System.Mail.SmtpException: System.IO.IOEXCEPTION {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}

代碼:

string mailServer = "outlook.domain.com"; 

SmtpClient client = new SmtpClient(mailServer, 587); 
client.DeliveryMethod = SmtpDeliveryMethod.Network; 
client.EnableSsl = true; 
client.UseDefaultCredentials = false; 

var AuthenticationDetails = new NetworkCredential("[email protected]", "password"); 
client.Credentials = AuthenticationDetails; 

using (MailMessage message = new MailMessage("[email protected]", recipient)) 
{ 
    message.IsBodyHtml = true; 
    message.Body = htmlString; 
    message.Subject = "Test Email"; 

    client.Send(message); 
} 
+1

閱讀例外情況:「現有連接被遠程主機強制關閉。」。嘗試閱讀以下內容:https://technet.microsoft.com/en-us/library/ms187005%28v=sql.105%29.aspx 將客戶端計算機更新爲服務器版本的SQL Server Native Client。 –

+0

「無法讀取數據」聽起來像指定編碼可以提供幫助。試試'mail.BodyEncoding = Encoding.UTF8;' –

回答

1

解決通過提供正確的主機名和端口的問題。謝謝大家幫助我。

1

我相信你打錯服務器ADRESS。看看這裏: http://windows.microsoft.com/en-us/windows/outlook/send-receive-from-app

使用正確的地址供您使用。從這個問題

string mailServer = "smtp-mail.outlook.com"; 

SmtpClient client = new SmtpClient(mailServer, 25); // or 587 
client.DeliveryMethod = SmtpDeliveryMethod.Network; 
client.EnableSsl = true; 
client.UseDefaultCredentials = false; 

var AuthenticationDetails = new NetworkCredential("[email protected]", "password"); 
client.Credentials = AuthenticationDetails; 

using (MailMessage message = new MailMessage("[email protected]", recipient)) 
{ 
    message.IsBodyHtml = true; 
    message.Body = htmlString; 
    message.Subject = "Test Email"; 

    client.Send(message); 
} 

提示:

Cant emails through exchange: An existing connection was forcibly closed by the remote host

+0

嘗試獲取錯誤 - 連接嘗試失敗,因爲連接方在一段時間後沒有正確響應,或建立的連接失敗,因爲連接的主機未能響應 –

+0

@ShankarAnumula您是否打開了端口25?你可以嘗試使用端口587 –

+0

是的都嘗試過..沒有工作。 –