I just saw an article about sending an Email in. NET, "sending an Email from the asp.net page". The class in the System. Web. Mail namespace is still used. We all know that. NET 1.1 uses classes in this namespace to Send emails. The static sending method of SmtpMail can be used to Send emails easily, however, this class calls the CDO component of the windows or later operating system to send emails. No matter what error occurs, it will prompt CDO. the Message object cannot be accessed or other CDO. message exception. We cannot obtain detailed exception information, which makes debugging inconvenient. Especially when we need Smtp verification, we also need to add three mail header definitions for MailMessage, which is relatively troublesome. In. NET 2.0, the Email sending function has been designed very well. Using the classes in the System. Net. Mail space allows us to send various emails very easily. However, to be compatible with the original version. web. the Mail space is not deleted, and classes in this space are marked with Obsolete. if the NET 2.0 Framework is compiled, the System is recommended. net. the Mail space class.
Next, let's take a look at what needs to be done before sending an Email. Here I also found an article about it in the garden. NET 2.0 sends an Email based on. NET 2.0 System. net. the Mail sending component of Mail namespace, which covers sending and receiving mails. In this article, we will only discuss how to send it. I feel that it does not seem to really use the configuration file. You also need to specify the Stmp account information for SmtpClient. In fact, you only need to configure the Smtp account we will use through web. config (or app. config:
<Configuration>
<System.net>
<MailSettings>
<Smtp from = "* @ *. com">
<Network host = "smtp.tom.com" password = "" port = "25" userName = "*" defaultCredentials = "false"/>
</Smtp>
</MailSettings>
</System.net>
</Configuration>
The. NET 2.0 Configuration System is powerful. We can simply write the above configuration without referring to any materials. As shown above, the from attribute is the Email address from which you want to send an Email, and the host is the smtp server address, password, port, and userName. Pay attention to defaultCredentials, when we do not need the smtp server to verify the sender, set it to false, and vice versa to true. Currently, most SMTP servers need user authentication, therefore, it is usually set to true, so that it uses the user name and password we configured to verify whether it is a legal user.
After the configuration file is written, the next step is how the program is called. After I write the following email, you will feel very strange. Is it really that simple? Yes, it's that simple. Let's take a look:
Mailmessage m_message = new mailmessage ();
M_message.from = new mailaddress ("* @ *. com ");
M_message.to.add (New mailaddress ("* @ *. com "));
M_message.subject = "use. NET 2.0 to send an email ";
M_message.body = "very simple ";
Smtpclient m_smtpclient = new smtpclient ();
M_smtpclient.send (m_message );
We don't even have a piece of extra code, that is, to construct a mailmessage object, and then construct an smtpclient to directly use it to send emails. There is no more code than the original static method. Some friends may ask? Where does SMTP account information come from? In fact, you can debug it. At this time, the attribute in the m_smtpclient object is the value configured in the previous configuration file. When a smtpclient is created, the value in the system.net configuration section is automatically read. If you have configured SMTP information, it will be automatically started to the current smtpclient object, and then use it to send an email. Sending emails in. NET 2.0 only requires so much work and is very stable.
Summary. in. NET 2.0, use system. web. sending emails via mail is very simple. You can also get detailed exception information when errors occur, but sometimes problems may make you confused. Most often, when your machine is installed with the anti-virus firewall, you may not be able to send an email, but the prompt information makes it impossible to diagnose the problem, the message is rejected. In this case, the firewall rejects the use of port 25, resulting in failure to communicate with the SMTP server. The solution is to close it. I believe there are many cases on the Internet. Pay special attention to the fact that the machine that sent the email must be on the Internet directly. This is especially impressive because the company uses a proxy to access the Internet. The above Code also requires that the from Value of the mailmessage object must be the same as the from Value in the configuration file. You can try it.
Now, I have attached the code to help you.