The program looks like this:
Copy the codeThe code is as follows:
static void Main(string[] args)
{
SmtpClient client = new SmtpClient();
= "localhost";
MailAddress from = new MailAddress("from@");
MailAddress to = new MailAddress("to@");
MailMessage message = new MailMessage(from, to);
(message);
();
}
When running, an error "The target computer is actively rejected, unable to connect." will be reported. I checked that it is said that the SMTP service has not started. So I found the setting method online, control panel -> programs and functions -> turn on or off Windows functions -> Internet information services -> application development functions -> .NET extensibility, and check this item. Then the SMTP email item will appear in the IIS Manager. After trying to make relevant settings, the result was the same error.
I also studied it and found that IIS7 in Windows 7 has removed the SMTP service, so it is useless no matter how you set it up. (There are related discussions here)
The solution is to install a third-party SMTP server. For example, the free Free SMTP Server.
After installation, no settings are required. Start the SMTP server and then run the above program. Everything is normal.
It is also very simple if you want to use a third-party SMTP server such as NetEase to send emails. The code is slightly modified:
Copy the codeThe code is as follows:
static void Main(string[] args)
{
SmtpClient client = new SmtpClient();
= "smtp.";
= new NetworkCredential("usenme", "password");// Must be set
MailAddress from = new MailAddress("from@");
MailAddress to = new MailAddress("to@");
MailMessage message = new MailMessage(from, to);
(message);
();
}
This attempt took a lot of time, and I hope others can help after reading this article.