April 22, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

Test SMTP Server for ASP.net Dev machines

Hi Guys,

If you need to test the email sending process from your asp.net application in local dev boxes (i.e. where you can’t connect to an smtp server), don’t worry, there is a fix for this.

First we need to change the web.config file

<system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="C:\Mails\"/>
      </smtp>
    </mailSettings>
  </system.net>

 

This changes implies that all emails sent are saved in C:\Mails folder, now, in order to send your email

public static bool Send(string To, string Subject, string Message, string From)
 {
      try
      {
          System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
           mailMessage.To.Add(To);
           mailMessage.From = new MailAddress(From);
           mailMessage.Subject = Subject;
           mailMessage.Body = Message;
           SmtpClient smtpClient = new SmtpClient();// Rememer to change this ("smtp.your-isp.com");
           smtpClient.Send(mailMessage);
           return true;
      }
      catch (Exception ex)
      {
           return false;
      }
 }