Do you have a integration test for your send mail code?
Last updated by Brady Stroud [SSW] 8 months ago.See history This rule has been archived
Archived Reason: Sending emails in applications is usually handled through 3rd party providers nowadays e.g. SendGrid. As a result mail sending checks should be covered off by HealthChecks
The code below shows how you could use TestSmtpServer to test your send mail code:
DotNetOpenMailProvider provider = new DotNetOpenMailProvider();
NameValueCollection configValue = new NameValueCollection();
configValue["smtpServer"] = "127.0.0.1";
configValue["port"] = "8081";
provider.Initialize("providerTest", configValue);
TestSmtpServer receivingServer = new TestSmtpServer();
try
{
receivingServer.Start("127.0.0.1", 8081);
provider.Send("phil@example.com",
"nobody@example.com",
"Subject to nothing",
"Mr. Watson. Come here. I need you.");
}
finally
{
receivingServer.Stop();
}
// So Did It Work?
Assert.AreEqual(1, receivingServer.Inbox.Count);
ReceivedEmailMessage received = receivingServer.Inbox[0];
Assert.AreEqual("phil@example.com", received.ToAddress.Email);
Figure: This code could help you validate the send mail code