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




Picture showing the mail gets stored in the specified folder
Click to Enlarge



Picture showing the content of the email when mail stored in the folder is opened
Click to Enlarge


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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250