To send mail from .net 2 classes are available in System.Net.Mail. Classes are MailMessage and SmtpClient . But before coding need to check if Microsoft SMTP service is turned on and prepare to send mail.
Setup SMTP Server: Open Internet Services Manager directly or open Computer Management and Navigate to Internet Information Services -> Default SMTP Virtual Server and checkout if the button with the ‘play’ icon on it is disabled, that means it is already started. From properties of Default SMTP virtual server, go to “Access” tab then from “Replay” option add IP “127.0.0.1”, It considers itself being used as a mail relay.
Test mailroot for mail sending: Now check whether mail is going from your mail server or not. Just go to your IIS folder normally it is located in C:Inetpub. In addition, the folders that Microsoft SMTP Service uses are located here, more exactly the folder is called mailroot. The SMTP service uses these folders to store email messages. You can see a total of seven folders, the most important one for sending email is Pickup – open this folder. Write these lines in a file (better if notepad) and save the file in this directory.
Subject: Hello World
Just testing to send mail. This is just a test body for the mail.
If your mail server is ok mail should go to the specific recipient address. If this part works nice you can go for coding to send mail.
C# code: This is the simple code to send a mail
public void SendMail()
MailMessage message = new MailMessage(“firstname.lastname@example.org”,”email@example.com”,”Test subject”,”Test mail body”);
SmtpClient client = new SmtpClient(“localhost”);