2013-04-18 26 views
2

我使用的是Visual Studio 2010,.NET 4.0,我試圖測試SMTPClient.TimeOut屬性,並且實際上讓它拋出一個SmtpException。但是,即使將TimeOut屬性設置爲1毫秒,它仍會發送電子郵件,好像在毫秒內發送,但我覺得有趣的是,當我檢查對象時,可以看到名爲timedOut的私有成員變量設置爲這表示它實際超時。無法測試SmtpClient.TimeOut

enter image description here

這裏是我的代碼:

try 
{ 
      MailMessage emailMsg = new MailMessage(this.EmailFrom, this.EmailTo, this.EmailSubject, msg); 
      //SmtpClient emailClient = new SmtpClient(this.EmailServer); 
      SmtpClient emailClient = new SmtpClient(); 
      emailClient.Credentials = new System.Net.NetworkCredential("username", "password"); 


       emailMsg.IsBodyHtml = true; 
       emailClient.Timeout = Properties.Settings.Default.SMTPTimeOut; 
       emailClient.Timeout = 1; 

       emailClient.Send(emailMsg); 
       sendingEmail = true; 

      return sendingEmail; 
     } 
     catch (Exception ex) 
     { 
       // Handle time out exception here. 
     } 

有沒有人見過這樣或知道一個更好的方法來測試呢?現在我正在打gmail的smtp。

回答

2

到最後測試,這是工作,我寫我的本地機器上的一個非常簡單的TCP服務器的控制檯應用程序。我使用以下教程如何做到這一點:http://bit.ly/XoHWPC

通過創建此控制檯應用程序,我能夠接受我的服務,它發送電子郵件並將其指向我的本地計算機(127.0.0.1,25)。 TCP服務器控制檯應用程序接受了連接,但是我從未發送迴應。

我的服務發送成功的電子郵件超時的預期,所以我終於可以驗證它正常工作。以下是指南教程中的TCP Server Console應用程序片段。如果你有時間,我建議閱讀整個教程。內置的Windows作爲我的回答

Program.cs的

using System; 
using System.Text; 
using System.Net.Sockets; 
using System.Threading; 
using System.Net; 

namespace TCPServer 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      Server server = new Server(); 
     } 
    } 
} 

Server.cs

using System; 
using System.Text; 
using System.Net.Sockets; 
using System.Threading; 
using System.Net; 

namespace TCPServer 
{ 
    public class Server 
    { 

     private TcpListener tcpListener; 
     private Thread listenThread; 

     public Server() 
     { 
      this.tcpListener = new TcpListener(IPAddress.Any, 25); 
      this.listenThread = new Thread(new ThreadStart(ListenForClients)); 
      this.listenThread.Start(); 
     } 

     private void ListenForClients() 
     { 
      this.tcpListener.Start(); 

      while (true) 
      { 
       //blocks until a client has connected to the server 
       TcpClient client = this.tcpListener.AcceptTcpClient(); 

       //create a thread to handle communication 
       //with connected client 
       Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm)); 
       clientThread.Start(client); 
      } 
     } 

     private void HandleClientComm(object client) 
     { 
      TcpClient tcpClient = (TcpClient)client; 
      NetworkStream clientStream = tcpClient.GetStream(); 

      byte[] message = new byte[4096]; 
      int bytesRead; 

      while (true) 
      { 
       bytesRead = 0; 

       try 
       { 
        //blocks until a client sends a message 
        bytesRead = clientStream.Read(message, 0, 4096); 
       } 
       catch 
       { 
        //a socket error has occured 
        break; 
       } 

       if (bytesRead == 0) 
       { 
        //the client has disconnected from the server 
        break; 
       } 

       //message has successfully been received 
       ASCIIEncoding encoder = new ASCIIEncoding(); 
       System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead)); 
      } 

      tcpClient.Close(); 
     } 
    } 
} 
+0

這證明了什麼?沒有電子郵件被髮送......因爲你不發送電子郵件?真正的服務器_does_發送郵件,所以發生的不是你想要的。 – CodeCaster

+0

它證明與服務器建立了連接,並且服務器不回覆,出現超時異常並且未發送電子郵件,這正是我想要測試的內容。 – Flea

2

我相信你可以使用telnet。打開telnet並使用該ip作爲您的smtp服務器。它不會連接,所以它應該超時。沒有測試過這個。

http://technet.microsoft.com/en-us/library/aa995718(v=exchg.65).aspx

+0

你沒有寫自己的服務器,你可以只使用遠程登錄說明。好吧。 –

+0

Scott你的回答確實有幫助,我查看了幫助我引導到此的telnet。我編寫了服務器部分,以便逐步拋出其代碼。我會投票給你肯定有幫助。 – Flea