Inspired by System.IO.Abstractions and System.Configuration.Abstractions comes System.Net.Mail.Abstractions. A small and simple abstraction around SmtpClient to help you write TDD focused code.
PM> Install-Package System.Net.Mail.Abstractions
varbuilder=newContainerBuilder();// Register ISmtpClient using your favourite DI containerbuilder.RegisterInstance(newSmtpClient()).As<ISmtpClient>();// Register other dependencies// ...varcontainer=builder.Build();publicclassMyClass{privateISmtpClient_smtpClient;publicMyClass(ISmtpClientsmtpClient){_smtpClient=smtpClient;}publicvoidDoSomethingThenSendEmail(){// ...if(true){varfrom="alerts@thesystem.com";varrecipients="manager@thesystem.com";varsubject="Extreme warning";varbody=newStringBuilder().AppendLine("Dear Manager,").AppendLine("").AppendLine("The plant is about to explode").AppendLine("").AppendLine("Kind Regards,").AppendLine("The System").ToString();_smtpClient.Send(from,recipients,subject,body);}// ...}}[TestFixture]publicclassMyClassTests{[Test]publicvoidMyClass_WhenSomeCondition_SendsEmail(){// Arrange.varsmtpClient=Substitute.For<ISmtpClient>();// Using NSubstitute to create a mock of ISmtpClient.varsut=newMyClass(smtpClient);// Pass in the mocked class// Act.sut.DoSomethingThenSendEmail();// Assert.// Ensure ISmtpClient.Send was calledsmtpClient.Received(1).Send("alerts@thesystem.com","manager@thesystem.com","Extreme warning",Arg.Any<string>());}}