How to send an email using smtpclient

Source: Internet
Author: User
Tags mailmessage smtpclient

ProgramYes:

CopyCode The Code is as follows: static void main (string [] ARGs)
{
Smtpclient client = new smtpclient ();
Client. Host = "localhost ";

Mailaddress from = new mailaddress ("from@test.com ");
Mailaddress to = new mailaddress ("to@test.com ");
Mailmessage message = new mailmessage (from, );

Client. Send (Message );

Console. Readline ();
}

When running, the message "unable to connect because the target computer is actively rejected" is reported ." . It is said that the SMTP service was not started. So I found the setting method on the internet, control panel-> program and function-> enable or disable WINDOWS function-> Internet Information Service-> application development function-> -〉.. Net scalability. Then, an SMTP Email item appears in the IIS manager. After setting the parameters, the results are still the same.

After studying it again, iis7 in Windows 7 has already removed the SMTP service, so no matter how you set it, it is useless. (We will discuss it here)

The solution is to install a third-party SMTP server. For example, free SMTP server.

After installation, you do not need to set any settings. Start the SMTP server and then run the above program. Everything is normal.

If you want to use third-party SMTP servers such as Netease to send emails, it is also very easy. Slightly modify the code:

Copy code The Code is as follows: static void main (string [] ARGs)
{
Smtpclient client = new smtpclient ();
Client. Host = "smtp.163.com ";
Client. Credentials = new networkcredential ("usenme", "password"); // required

Mailaddress from = new mailaddress ("from@163.com ");
Mailaddress to = new mailaddress ("to@test.com ");
Mailmessage message = new mailmessage (from, );

Client. Send (Message );

Console. Readline ();
}

This attempt took a lot of time. I hope other people can see this Article .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.