Articles → .NET → Test email functionality without smtp port enabled in C#

Test email functionality without smtp port enabled in C#








Create a sample project




string recipient = string.Empty;
string subject = string.Empty;
string message = string.Empty;

Console.WriteLine("From:gyansangrah@gyansangrah.com");
Console.WriteLine("Recipient:");
recipient = Console.ReadLine();
Console.WriteLine("Subject:");
subject = Console.ReadLine();
Console.WriteLine("Message:");
message = Console.ReadLine();

// Send email
MailMessage mailMessage = new MailMessage();
MailAddress fromAddress = new MailAddress("gyansangrah@gyansangrah.com");
mailMessage.From = fromAddress;
mailMessage.To.Add(recipient);
mailMessage.Subject = subject;
mailMessage.Body = message;
            

SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "localhost";
smtpClient.Send(mailMessage);

Console.WriteLine("Email Sent. Press any key to close the application......");
Console.ReadLine();





Settings in app.config




<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
    </startup>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="C:\MailFolder\"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>





Output





Click to Enlarge




Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, March 09, 2016

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250