How to Create a SMTP Server and Send Mail Using C#

Many companies forbid access to certain websites like Gmail and Yahoo. You might want to integrate email functionality in your application and check whether it’s working properly, but how do you check?

Instead of depending on the Gmail or Yahoo, you can set up your own SMTP server and check the functionality.

There are many free applications available, hMailServer is one of them that is open-source and supports common protocols (SMTP, IMAP and POP3).

Download the hMailServer that is about 3MB and follow the installation procedure from the following link.

URL - https://www.hmailserver.com/documentation/latest/?page=howto_install

Let me show you how to configure the hMailServer.

After installation, we must configure the DNS in our local system to check the functionality. Go to the hMailServer folder and Launch “hMailServer Administration”.


Figure 1: HMailServer Administration

Click on the “Add domain” button from the welcome screen and name it as you wish.


Figure 2: Add Domain

Click on the “Save” button. Open the Domains from the right side where you can see 3 folders are created by default.


Figure 3: Default Folder

Right-click on the “Accounts” folder and select the “Add” option. Let us create 2 users for our testing purposes.


Figure 4: Account

Fill in all 3 fields (Address, Password and Maximum size) and click on the save button.

Create another user with the same procedure as above. Now you have created a domain “Demo.com” with 2 users ([email protected], [email protected]).

Add one entry for our domain in the Hosts file. Use the following procedure.

  1. Open the Run prompt.
  2. Type “Drivers” and click on the “Ok” button.
  3. Open the “etc” folder and open the “Hosts” file.
  4. Add the following entry & save the file.
    127.0.0.1 demo.com

Note: Before doing any changes on the Hosts file, please take a backup of it.

Now let us check whether we have configured the SMTP properly or not. Open your command prompt and type in the following command.

telnet demo.com 25

You will see the following screen if you have done the configuration correctly.


Figure 5: Output

Next we must configure the database. This is where all the settings are stored. It supports the following databases.

  • Microsoft SQL Server 2000 or later.
  • MySQL.
  • PostgreSQL.

Let us see how to configure the database for hMailServer. Open the hMailServer Database Setup from the installation folder.


Figure 6:
Configure Database

Click the Next button in the preceding window.


Figure 7: New Window

Select “Create a new hMailServer Database” and proceed. Next.Select “Microsoft SQL Server” and click Next.


Figure 8: Microsoft SQL Server


Figure 9: Click Next

Provide an existing server address and database name as you wish and authentication details and click Next.

It will create a database with the following tables in the SQL Server .All mail related settings are in the database.


Figure 10: Database Table

That’s it. Now you have configured the SMTP server in your local system. Let us create a simple console application in C# and test the mail functionality.

  1. class Program   
  2. {  
  3.     static void Main(string[] args)   
  4.     {  
  5.         string mailstatus = SendEmail("[email protected]""Testmail""Hey i have setup my own SMTP server.Let us check it out!!!");  
  6.         Console.WriteLine(mailstatus);  
  7.         Console.ReadKey();  
  8.     }  
  9.     public static string SendEmail(string toAddress, string subject, string body) {  
  10.         string result = "Message Sent Successfully..!!";  
  11.         string senderID = "[email protected]";  
  12.         const string senderPassword = "password";  
  13.         try {  
  14.             SmtpClient smtp = new SmtpClient   
  15.             {  
  16.                 Host = "demo.com",  
  17.                 Port = 25,  
  18.                 DeliveryMethod = SmtpDeliveryMethod.Network,  
  19.                 Credentials = new System.Net.NetworkCredential(senderID, senderPassword),  
  20.                 Timeout = 30000,  
  21.             };  
  22.             MailMessage message = new MailMessage(senderID, toAddress, subject, body);  
  23.             smtp.Send(message);  
  24.         }   
  25.         catch (Exception ex)   
  26.         {  
  27.             result = "Error sending email.!!!";  
  28.         }  
  29.         return result;  
  30.     }  
  31. }  

Figure 11: Output 2

If you get the preceding screen then you have done a great job. To confirm whether user1 has sent a mail to user2, let us check in the database.

Open your SQL Server Management Studio and connect to your database. Run the following query in your database.


Figure 12: SQL Query

I am checking whether the user2 got mail from a user.Yes he got the mail from the user1. All the mails will be stored in the folder in the encrypted form.

Go to the path where you have installed your hMailServer. I have installed the application in the following path. C:\Program Files\hMailServer\.


Figure 13:
Select a path

You can see the folder with your domain name. Open that folder. There you can see a folder with your “user2” name. Right-click on the file and open it in Notepad.


Figure 14: Domain Name

You can see all the information that we set in C#. I hope you have enjoyed the article. Thanks for reading. I’m sharing in articles whenever I learn something new. Thanks a lot for your patience.


Similar Articles