Many times, we need to send email notifications in our application. However, during the local development stage, we may not have access to SMTP server, therefore the email function would throw errors.
The easiest way to solve this problem is to use the built-in function provided by SmtpClient in C# to save the email to specified folder location.
using (var smtp = new SmtpClient(_host, _port))
{
using (var message = new MailMessage())
{
message.Subject = "this is a test";
message.Body = "<p>Test email body</p>";
message.IsBodyHtml = true;
message.To.Add(new MailAddress("tester@test.com", "tester"));
message.From = new MailAddress("tester1@test.com", tester1);
#if DEBUG
smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
const string emailPickupDirectory = "C:\\Temp\\Emails";
if (!Directory.Exists(emailPickupDirectory))
{
Directory.CreateDirectory(emailPickupDirectory);
}
smtp.PickupDirectoryLocation = emailPickupDirectory;
#endif
smtp.UseDefaultCredentials = false;
smtp.Send(message);
}
}
{
using (var message = new MailMessage())
{
message.Subject = "this is a test";
message.Body = "<p>Test email body</p>";
message.IsBodyHtml = true;
message.To.Add(new MailAddress("tester@test.com", "tester"));
message.From = new MailAddress("tester1@test.com", tester1);
#if DEBUG
smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
const string emailPickupDirectory = "C:\\Temp\\Emails";
if (!Directory.Exists(emailPickupDirectory))
{
Directory.CreateDirectory(emailPickupDirectory);
}
smtp.PickupDirectoryLocation = emailPickupDirectory;
#endif
smtp.UseDefaultCredentials = false;
smtp.Send(message);
}
}
Comments