???????SmtpClient????Email?????????????ISmtpClient????SmtpClientWrapper???????????????ж?ISmtpClient????Mock???????FackeSmtpClient????nDumbster??Facke SMTP Server????????????????????????е??????????????Nuget????nDumbster????????????netDumbster??
????1.IEmailSender???
????public interface IEmailSender
????{
????void SendMail(string from?? string to?? string subject?? string body);
????}
????2.SMTPAdapter?????
public interfacepublic class SMTPAdapter : IEmailSender
{
public void SendMail(string from?? string to?? string subject?? string body)
{
var message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress(from);
message.To.Add(new MailAddress(to));
message.Subject = subject;
message.Body = body;
using (var smtpClient = new SmtpClient())
{
if (smtpClient.Host == null)
{
smtpClient.Host = "localhost";
}
smtpClient.Send(message);
}
}
}
????3.???nDumbster???????
public class SMTPAdapterTest
{
[Fact]
public void SendMailTest()
{
SimpleSmtpServer server = SimpleSmtpServer.Start(25);
IEmailSender sender = new SMTPAdapter();
sender.SendMail("sender@here.com"?? "receiver@there.com"?? "subject"?? "body");
Assert.Equal(1?? server.ReceivedEmailCount);
SmtpMessage mail = (SmtpMessage)server.ReceivedEmail[0];
Assert.Equal("sender@here.com"?? mail.Headers["From"]);
Assert.Equal("receiver@there.com"?? mail.Headers["To"]);
Assert.Equal("subject"?? mail.Headers["Subject"]);
Assert.Equal("body"?? mail.MessageParts[0].BodyData);
server.Stop();
}
}
???????н????