Summary of the methods for sending emails in the. NET class library

Source: Internet
Author: User
Tags valid email address mailmessage smtpclient

Email sending is a common function for many websites that require user registration. using regular expressions, We can filter out input that do not conform to the email format, however, there is still no way to ensure that the email address entered by the user must be a real and valid email address. Generally, the method to verify the authenticity of the email is as follows: after the registration information entered by the user passes preliminary format verification on the website, the user cannot log on using this account. The system will send an email to the email address filled in at the user registration, A link is provided in the email. the user can log on to the website only after clicking this link. If the email address entered by the user is not true or valid, this step is generally called email activation.
There are two ways to send email in the. NET class library. One is in the. net2.0 or lower version, and the other is in the. net2.0 or later version. The two methods are described below.

I. Practices in. net1.0 and. net1.1 (using the cdosys message component to send emails ):

The Code is as follows:

 

Using system;
Using system. Web. mail;

/// <Summary>
/// Example of how to send an email in versions earlier than. net2.0
/// The classes used are mainly located in the system. Web. Mail namespace
/// By Zhou Gong
/// Date: 2008-08-08
/// Starting address: http://blog.csdn.net/zhoufoxcn
/// </Summary>
Public class sendmail
{
Public Sendmail ()
{

}
/// <Summary>
/// Send an email
/// </Summary>
/// <Param name = "to"> recipient email address </param>
/// <Param name = "from"> sender's email address </param>
/// <Param name = "subject"> subject </param>
/// <Param name = "body"> email content </param>
/// <Param name = "username"> User name used to log on to the SMTP host. Note that the email address is '@' in the previous section. </param>
/// <Param name = "password"> User Password used to log on to the SMTP host </param>
/// <Param name = "smtphost"> SMTP host used for sending emails </param>
Public void send (string to, string from, string subject, string body, string username, string password, string smtphost)
{
Mailmessage mail = new mailmessage ();
Mail. To = to; // set the recipient address
Mail. From = from; // set the sender address
Mail. Subject = subject; // set the mail subject
Mail. bodyformat = mailformat. html; // set the email to be sent in HTML Format
Mail. Body = body; // set the mail content
// Set the authentication required for sending an email
Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1 ");
// Set the user name when logging on to the mail host, note that if the sender address is a abc@def.com, the user name is ABC instead of the 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 the SMTP host for sending mail
Smtpmail. smtpserver = smtphost;
// Send the email. If the email fails to be sent, an exception is thrown.
Smtpmail. Send (Mail );
}
}
The above method uses the cdosys component at the underlying layer, and the class used is mainly system. web. mail. smtpserver and system. web. mail. mailmessage, which has been rejected by Microsoft and replaced. added classes and methods in net2.0.

 

Ii. Practices in. net2.0

Code:

Using system;
Using system. net. mail;
Using system. net;

/// <Summary>
/// Example of how to send an email in. net2.0 or a later version
/// The classes used are mainly located in the system. net. Mail and system. Net namespaces.
/// By Zhou Gong
/// Date: 2008-08-08
/// Starting address: http://blog.csdn.net/zhoufoxcn
/// </Summary>
Public class sendmail2
{
Public sendmail2 ()
{
}

/// <Summary>
/// Send an email
/// </Summary>
/// <Param name = "to"> recipient email address </param>
/// <Param name = "from"> sender's email address </param>
/// <Param name = "subject"> subject </param>
/// <Param name = "body"> email content </param>
/// <Param name = "username"> User name used to log on to the SMTP host. Note that the email address is '@' in the previous section. </param>
/// <Param name = "password"> User Password used to log on to the SMTP host </param>
/// <Param name = "smtphost"> SMTP host used for sending emails </param>
Public void send (string to, string from, string subject, string body, string username, string password, string smtphost)
{
Mailaddress from = new mailaddress (from );
Mailaddress to = new mailaddress ();
Mailmessage message = new mailmessage (from, );
Message. Subject = subject; // set the mail subject
Message. isbodyhtml = true; // set the email body to HTML format.
Message. Body = body; // you can specify the email content.
Smtpclient client = new smtpclient (smtphost );
// Set the mail Sender authentication method
// Note that if the sender address is a abc@def.com, the user name is ABC instead of the abc@def.com
Client. Credentials = new networkcredential (username, password );
Client. Send (Message );
}
}

The above code is mainly used in. the two classes added in net2.0 are system. net. mail. mailmessage and system. net. mail. two smtpclient classes are used for SMTP authentication. net. networkcredential class.

 

Note: No matter in that way, the account used for identity authentication is Web login is used for the account, if your mailbox address is zhou@163.com, when logging on to mail.163.com mailbox, the entered account is Zhou rather than the zhou@163.com, as is the case during authentication.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zhoufoxcn/archive/2008/08/09/2789413.aspx

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.