1, .NET SMTP class
First, let's introduce the SMTP class that comes with the .NET class library. Under the system.web.mail name space in .NET, there is a class specifically using the SMTP protocol to send mail: SMTPMAIL, which can meet the most ordinary email requirements. This class has only one public function - Sernd () and a public property -SMTPServer
You must specify the name (or IP address) of the server sent by the SMTPServer property, and then call
Send () functions to send mail.
The code example is, for example:
(In C #)
Using system.web.mail;
Public void sendmail ()
{
Try
{
System.web.mail.mailmessage mymail = new mailmessage ();
MyMail.From = "myaccount@test.com";
MyMail.to = "myaccount@test.com";
mymail.subject = "mailtest";
Mymail.priority = mailpriority.low;
mymail.bodyformat = mailformat.text;
mymail.body = "test";
SMTPMAIL.SMTPSERVER = "smarthost"; // Your SMTP Server Here
SMTPMAIL.SEND (MyMail);
}
Catch (Exception E)
{
Throw e;
}
}
You can set the relevant properties of the message in the parameter MailMessage object of the Send function, such as priority, attachments, and more. In addition to the MilMessage object as a parameter (such as the above code), the Send function can also be called directly in four main information (from, to, subs, subs, messagetext) as a string parameter.
2. Send an email using the CDO component
CDO is the abbreviation of Collaboration Data Objects. It is a set of high-level COM object collections and has experienced several versions of evolution. Now use of CDO2.0 in Windows2000 and Exchange 2000 (CDOSYS.DLL and CDOEX, respectively. .dll). CDOSYS is built over the SMTP protocol and the NNTP protocol, and as a component of Windows2000 Server is installed, you can find it (cdosys.dll) in the System Directory (such as C: / Winnt or C: / Windows).
The CDO component is more rich relative to the previously introduced SMTPMail objects and provides some features available to the SMTPMAIL class, such as sending emails by using the SMTP server that requires authentication.
The following segment shows how to use the CDO component to send mail by using the CDO components to send mail by requiring authentication:
(In C #)
Public void cdosendmail ()
{
Try
{
CDO.MESSAGE OMSG = New CDO.MESSAGE ();
omsg.from = "myaccount@test.com";
omsg.to = "myaccount@test.com";
omsg.subject = "mailtest";
omsg.htmlbody = "
test body> html>"; cdo.iconfiguration iconfg = omsg.configuration;AdoDb.fields ofields = iconfg.fields;
Ofields ["http://schemas.microsoft.com/cdo/configuration/sendusing"] .value = 2;
Ofields ["http://schemas.microsoft.com/cdpo/configuration/sendemailaddress"]
.Value = "myaccount@test.com"; // sender mail
Ofields ["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"]
.Value = "myaccount@test.com"; // email account
Ofields ["http://schemas.microsoft.com/cdpo/configuration/sendusername"]
.Value = "username";
Ofields ["http://schemas.microsoft.com/cdo/configuration/sendpassword"]
.Value = "password";
Ofields ["http://schemas.microsoft.com/cdpo/configuration/smtpauthenticate"]
.Value = 1;
// Value = 0 represents an Anonymous authentication mode (no verification required)
// Value = 1 represents the Basic Authentication mode (using the Basic (Clear-text) Authentication.
// the configuration sendusername / sendpassword or postusername / postpassword field
Are used to specify credentials.)
// Value = 2 represents NTLM authentication mode (Secure Password Authentication in Microsoft Outlook Express)
Ofields ["http://schemas.microsoft.com/cdpo/configuration/languagecode"] .value = 0x0804;
Ofields ["http://schemas.microsoft.com/cdpo/configuration/smtpserver"] .value = "smtp.21cn.com";
Ofields.Update ();
OMSG.BODYPART.CHARSET = "GB2312";
omsg.htmlbodypart.charset = "GB2312";
omsg.send ();
OMSG = NULL;
}
Catch (Exception E)
{
Throw e;
}
}