我使用的是Visual Studio2010,.NET 4.0,我正在尝试测试SMTPClient.TimeOut属性,并实际让它抛出一个SmtpException。但是,即使当我将TimeOut属性设置为1毫秒时,它仍然会发送电子邮件,看起来不到1毫秒,但我发现有趣的是,当我检查对象时,我可以看到一个名为timedOut的私有成员变量设置为true,这表明它实际上超时了。

下面是我的代码:
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。
发布于 2013-04-20 02:07:35
为了最终测试它的工作,我在我的本地机器上编写了一个非常简单的TCP服务器控制台应用程序。关于如何做到这一点,我使用了以下教程:http://bit.ly/XoHWPC
通过创建这个控制台应用程序,我能够使用我的服务发送电子邮件并将其指向我的本地计算机(127.0.0.1,25)。TCP服务器控制台应用程序接受了该连接,但之后我再也没有发回响应。
我发送电子邮件的服务如预期的那样成功超时,所以我最终可以验证它是否正常工作。下面是本教程中的TCP Server Console应用程序的一个片段,作为指南。不过,如果你有时间的话,我建议你阅读整个教程。
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();
}
}
}发布于 2013-04-19 01:48:02
我相信你可以使用telnet。打开telnet并将该ip用作smtp服务器。它不会连接,因此应该会超时。还没有对此进行测试。
http://technet.microsoft.com/en-us/library/aa995718(v=exchg.65).aspx
https://stackoverflow.com/questions/16089745
复制相似问题