Methods for sending e-mail messages in the. NET class Library

Source: Internet
Author: User
Tags mail mailmessage

Sending e-mail is a common feature of many Web sites that require user registration, and regular expressions allow us to filter out input that does not conform to the email format, but there is no way to ensure that the e-mail address that the user fills out must be his real and valid e-mail address. 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
/// The classes used are primarily located under the System.Web.Mail namespace
///Author: Weekly
///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 mail address </param>
///<param Name = "from" > Sender mail address </param>
///<param name= "subject" > Message theme </param>
///<param name= "body "> Message content </param>
///<param name=" username "> the username used to log on to the SMTP host, note that the previous part of the mail address ' @ ' </param>
/// <param name= "Password" > the user password used to log on to the SMTP host </PARAM>
///<param name= "SMTPHost" > Outgoing SMTP hosts for mail < /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 the recipient address
Mail. From = from;//Sets the sender Address
Mail. Subject = subject;//Set the message subject
Mail. BodyFormat = mailformat.html;//Sets the message to send in Html format
Mail. BODY = body;//Set the message content
//Setup requires authentication when sending mail
Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//Set the user name to log in to the mail host, 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 user password to log on to the SMTP host
Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", password);     
//Set SMTP host to send mail
Smtpmail.smtpserver = smtphost;
   Send the message, if the send is unsuccessful, throw an exception
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.

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.