2

我正在使用 Visual Studio 2010、.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。

4

2 回答 2

2

我相信你可以使用telnet。打开 telnet 并将该 ip 用作您的 smtp 服务器。它不会连接,所以它应该超时。没有测试过这个。

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

于 2013-04-18T17:48:02.027 回答
2

为了最终测试它是否有效,我在本地机器上编写了一个非常简单的 TCP 服务器控制台应用程序。我使用了以下教程来了解如何做到这一点: http: //bit.ly/XoHWPC

通过创建这个控制台应用程序,我能够使用发送电子邮件并将其指向我的本地计算机 (127.0.0.1,25) 的服务。TCP Server 控制台应用程序接受了连接,但我从未发回响应。

我发送电子邮件的服务按预期成功超时,因此我最终可以验证它是否正常工作。这是教程中的 TCP Server Console 应用程序片段,作为指南。如果您有时间,我建议您阅读整个教程。

程序.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();
        }
    }
}

服务器.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-19T18:07:35.267 回答