Testing SMTP with .net

后端 未结 17 1247
[愿得一人]
[愿得一人] 2020-12-04 09:54

I need to configure a SMTP server for testing my website which sends emails (for registration confirmation etc).

I dont actually want the email to be sent, I just w

相关标签:
17条回答
  • 2020-12-04 10:54

    If you've got Python installed, you can run the following one liner to run a debug smtp server in the console that'll dump messages to stdout:

    sudo python -m smtpd -n -c DebuggingServer localhost:25

    snagged from here: http://muffinresearch.co.uk/archives/2010/10/15/fake-smtp-server-with-python/

    0 讨论(0)
  • 2020-12-04 10:56

    You can also use netDumbster.

    http://netdumbster.codeplex.com/

    0 讨论(0)
  • 2020-12-04 10:57

    An alternative way to do this is to create a wrapper around the SmtpClient that implements the same interface. Then inject and use the wrapper in your class. When doing unit testing you can then substitute a mock wrapper that has expectations for the method calls and responses.

    EDIT: The wrapper is needed (for RhinoMocks, at least) because SmtpClient doesn't derive from an interface and doesn't have virtual methods. If you use a mocking framework that can mock a class without virtual methods directly, you can skip the wrapper and inject the SmtpClient mock directly.

    public class SmtpClientWrapper
    {
        private SmtpClient Client { get; set; }
    
        public SmtpClientWrapper( SmtpClient client )
        {
             this.Client = client;
        }
    
        public virtual void Send( MailMessage msg )
        {
             this.Client.Send( msg );
        }
    
        ...
    }
    
    
    public class MyClass
    {
        private SmtpClientWrapper Client { get; set; }
    
        public MyClass( SmtpClientWrapper client )
        {
             this.Client = client;
        }
    
        public void DoSomethingAndNotify()
        {
             ...
             this.Client.Send( msg );
        }
    }
    

    Tested (with RhinoMocks) as:

    public void DoSomethingAndNotifySendsAMessageTest()
    {
         SmtpClientWrapper client = MockRepository.GenerateMock<SmtpClientWrapper>();
         client.Expect( c => c.Send( new MailMessage() ) ).IgnoreArguments();
    
         MyClass klass = new MyClass( client );
    
         klass.DoSomethingAndNotify();
    
         client.VerifyAllExpectations();
    }
    
    0 讨论(0)
  • 2020-12-04 10:57

    I use Antix SMTP Server For Developers which is as easy as opening up an application. It stores the messages in a folder and you can view them with the UI. Pretty quick/easy solution. I wanted to mention it here.

    See also: development smtp server for windows

    0 讨论(0)
  • 2020-12-04 10:58

    The DevNull SMTP server logs all the gory details about communication between the client and the SMTP server. Looks like it would be useful if you were trying to diagnose why your sending code wasn't working.

    It's written in Java and deploys as an executable jar. Source code doesn't seem to be available.

    0 讨论(0)
提交回复
热议问题