I just saw an article about sending emails in .NET "Sending emails from asp.net pages" and I saw that the classes under the System.Web.Mail namespace are still used. We all know that under .NET 1.1, most classes in this namespace are used to send emails. Using the Send static method of SmtpMail can easily send emails, but since this class calls operations on Windows 2000 and above, The CDO component of the system is used to send emails. No matter what the error is, it will prompt that the CDO.Message object cannot be accessed or other CDO.Message exceptions. We cannot get very detailed exception information, which brings great inconvenience to our debugging. . Especially when we need Smtp verification, we have to add three mail header definitions to MailMessage, which is relatively troublesome. Well, under .NET 2.0, the function of sending emails has been designed very well. Using the classes in the System.Net.Mail space allows us to send various emails very simply. However, in order to be compatible with the original version, the System.Web.Mail space has not been deleted, and the classes in this space have also been marked with Obsolete. If you use .NET 2.0 classes to compile, you will be prompted to use System.Web.Mail. space class instead.
Now let’s take a look at what needs to be done to send emails. Here I also found an article in the garden about .NET 2.0 sending emails, "Email sending component based on .NET 2.0 System.Net.Mail namespace". It covers sending and receiving mail. In this article, we only discuss how to send it. I feel that it does not really use the configuration file. We also need to specify some Stmp account information for SmtpClient ourselves. In fact, we only need to configure the Smtp account we will use through web.config (or app.config):
< /system.net>
The configuration system of .NET 2.0 is so powerful that we can write the above configuration very simply without referring to any information. As above, the from attribute is the mailbox from which you will send the email, and the host is the SMTP server address. Needless to say, password, port, and userName need to be noted. As for defaultCredentials, you need to pay attention to it. When we do not need the SMTP server to verify the sending user, Set it to false, otherwise set it to true. Most current SMTP servers require user authentication, so generally set it to true and let it use the username and password we configured to verify whether it is a legitimate user.
After the configuration file is written, the next step is how to call the program. After I write the following to send the 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(" [email protected] ");
m_message.To.Add(new MailAddress(" [email protected] "));
m_message.Subject = "Send mail using .NET 2.0";
m_message.Body = "Very simple";
SmtpClient m_smtpClient = new SmtpClient();
m_smtpClient.Send(m_message);
We don't even have a single extra line of code, that's Construct a MailMessage object, then construct a SmtpClient and use it to send emails directly. Compared with the original static method, there is no more code. Some friends here may want to ask? Where does the SMTP account information come from? In fact, you can debug it. At this time, the attributes in the m_smtpClient object are the values configured in our previous configuration file. When reconfiguring an SmtpClient, the value under the system.net configuration section will be automatically read. If the user has configured smtp information, it will be automatically transferred to the current SmtpClient object, and then used to send emails. Sending email in .NET 2.0 is just so much work, and it's very stable.
Summary: Although under .NET 2.0, it is very simple to use System.Web.Mail to send emails, and you can get more detailed exception information when errors occur, but sometimes problems can make you confused. The most common thing that happens is that when your machine has an anti-virus firewall installed, you may not be able to send emails, but the prompt message prevents you from diagnosing the problem. The main effect is that the email is refused. This situation occurs because the firewall refuses our use of port 25, resulting in the inability to communicate normally with the SMTP server. The solution is to close it. I believe there are many cases on the Internet. Another point to pay special attention to is that the machine to send emails must be directly connected to the external network. Since the company accesses the Internet through a proxy, this is particularly impressive. There is one more thing to note in the above code. The From value of the MailMessage object must be the same as the From value in the configuration file. It seems that there is no need to set it here. You can try it.
Okay, here's the code, I hope it can be helpful to you http://hjf1223.cnblogs.com/archive/2006/07/06/444716.html