首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法测试SmtpClient.TimeOut

无法测试SmtpClient.TimeOut
EN

Stack Overflow用户
提问于 2013-04-19 01:42:35
回答 2查看 1.4K关注 0票数 2

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

下面是我的代码:

代码语言:javascript
复制
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。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-20 02:07:35

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

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

我发送电子邮件的服务如预期的那样成功超时,所以我最终可以验证它是否正常工作。下面是本教程中的TCP Server Console应用程序的一个片段,作为指南。不过,如果你有时间的话,我建议你阅读整个教程。

Program.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
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();
        }
    }
}
票数 2
EN

Stack Overflow用户

发布于 2013-04-19 01:48:02

我相信你可以使用telnet。打开telnet并将该ip用作smtp服务器。它不会连接,因此应该会超时。还没有对此进行测试。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16089745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档