ASP.net the code for sending e-mail in the class library _ Practical Tips

Source: Internet
Author: User
Tags mailmessage smtpclient
The most common way to verify e-mail is that when the user fills in the registration data after the initial form of the site, the user is not able to use this account to log in, the system will be filled out at the user's email address to send an e-mail, the message gives a link, Only when the user clicks on the link to log on to the site, if the user filled out the e-mail address is not authentic or not his own, you will not receive this email, so still can not log in, this step is generally called e-mail activation.

There are two ways to send e-mail in the. NET class library, one in the following versions of. Net2.0, and one for. net2.0 the above version of the procedure. These two approaches are described below.

I. Practices in net1.0 and. net1.1 (sending e-mail using the CDOSYS message component):
The code is as follows:
Using System;
Using System.Web.Mail;
<summary>
Description: Example of how to send e-mail in the following version of. net2.0
Classes used are primarily located under the System.Web.Mail namespace
Author: Zhou Gong
Date: 2008-08-08
Starting Address: HTTP://BLOG.CSDN.NET/ZHOUFOXCN
</summary>
public class SendMail
{
Public SendMail ()
{
}
<summary>
Send mail
</summary>
<param name= "to" > Recipient email Address </param>
<param name= "from" > Sender email Address </param>
<param name= "Subject" > Message subject </param>
<param name= "Body" > Message content </param>
<param name= "username" > Log in to the SMTP host when the user name, note that the mail address ' @ ' before the part </param>
<param name= "Password" > the user password used when logging on to the SMTP host </param>
<param name= "SMTPHost" > Send mail to SMTP host </param>
public void Send (string to, string from, string subject, String, string Username, string password, string smtphost)
{
MailMessage mail = new MailMessage ();
Mail. to = to;//Set recipient address
Mail. from = from;//Set Sender address
Mail. Subject = subject;//Set Message subject
Mail. BodyFormat = mailformat.html;//Set message to send in Html format
Mail. BODY = body;//Set message content
Setup requires authentication when sending messages
Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
Set the username when you log on to the mail host, and note that if the sender address is abc@def.com, the user name is ABC instead of abc@def.com
Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
Set the user password to log on to the SMTP host
Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
Set up an SMTP host to send mail
Smtpmail.smtpserver = SMTPHost;
Send a message and throw an exception if the send is unsuccessful
Smtpmail.send (mail);
}
}
The CDOSYS component is used at the bottom of the method. The classes used are mainly System.Web.Mail.SmtpServer and System.Web.Mail.MailMessage, and this method has been used by Microsoft and replaced by the new classes and methods in net2.0.
Ii. practices in the net2.0
Code:
Using System;
Using System.Net.Mail;
Using System.Net;
<summary>
Description: Example of how to send e-mail in the above version of. net2.0
The classes used are primarily located under the System.Net.Mail and System.Net namespaces
Author: Zhou Gong
Date: 2008-08-08
Starting Address: HTTP://BLOG.CSDN.NET/ZHOUFOXCN
</summary>
public class SendMail2
{
Public SendMail2 ()
{
}
<summary>
Send mail
</summary>
<param name= "to" > Recipient email Address </param>
<param name= "from" > Sender email Address </param>
<param name= "Subject" > Message subject </param>
<param name= "Body" > Message content </param>
<param name= "username" > Log in to the SMTP host when the user name, note that the mail address ' @ ' before the part </param>
<param name= "Password" > the user password used when logging on to the SMTP host </param>
<param name= "SMTPHost" > Send mail to SMTP host </param>
public void Send (string to, string from, string subject, String, string userName, string password, string smtphost)
{
MailAddress from = new MailAddress (from);
MailAddress to = new mailaddress (to);
MailMessage message = new MailMessage (from, to);
Message. Subject = subject;//Set Message subject
Message. isbodyhtml = true;//Set message body to HTML format
Message. BODY = body;//Set message content
SmtpClient client = new SmtpClient (smtphost);
Set the way sent messages are authenticated
Note If the sender address is abc@def.com, the user name is ABC instead of abc@def.com
Client. Credentials = new NetworkCredential (userName, password);
Client. Send (message);
}
}
In the code above, The main use of the two new classes in. net2.0, the System.Net.Mail.MailMessage and System.Net.Mail.SmtpClient two classes, is used for SMTP authentication System.Net.NetworkCredent Ial class.
Need to note is: In that way, the authentication used by the account is a Web login is used in the account, if your e-mail address is zhou@163.com, in the mail.163.com mailbox, enter the account number is Zhou rather than zhou@163.com, The same is true when authenticating.
Related 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.