If you want to use ASP for an email sending system, you must think this is a complicated task. This is indeed the case. However, when his successor product ASP. NET was launched, its powerful functions made it easier. Is that amazing? We will use ASP. NET as an email sending system to see if there are any mysteries.
I. Runtime Environment
Although Microsoft has launched Visual Studio. NET for some time, the. NET Framework SDK is still in beta version, namely Beta 1 and beta 2. There are also great differences between the two versions. For example, many namespaces have different names. Here we choose beta 2. The operating system is Windows 2000 Professional.
Ii. Basic Knowledge
First, we will introduce some knowledge used in the mail sending system.
(1) first, let's take a brief look atProgramThe Protocol to be used-Simple Mail Transfer Protocol-SMTP, SMTP server, is used to send mail and transit mail. This article also mentions the Post Office Protocol 3 (postoffice Protocol 3)-POP3. The POP3 server is used to receive mails. The mails in our account are stored on such servers. As this article only describes sending emails, only SMTP is involved.
(2 ). next, let's take a look at the namespace-system. web. mail (Note :. net Framework SDK Beta 1 is called system. web. util ). This large Class Library provides a wide range of objects, attributes, and methods for sending emails, that is, it makes sending emails easy and pleasant. Next, we will briefly introduce the specific usage of various objects, attributes, and methods:
(1). Object
In the system. Web. Mail namespace, there are three main objects used to send Emails: smtpmail, mailmessage, and mailattachment. Smtpmail is used to set the SMTP server to select which server to send the mail. Mailmessage is the most abundant among the three objects. A large number of attributes are packaged as the constituent elements of our current email, and the mailattachment object corresponds to the attachment in the email.
2). Attributes
In the system. Web. Mail namespace, the attributes of mailmessage objects are the most abundant and important. The following table lists their most important attributes, which are directly related to our emails:
Attribute name |
Meaning |
From |
Source Address |
To |
Destination Address |
Subject |
Email Subject |
Priority |
Mail priority (high, low, normal) |
Attachments |
Attachment |
BCC |
Dark delivery address |
CC |
CC address |
Body |
Email Subject |
Bodyformat |
Email format (HTML, text) |
Bodyencoding |
Email encoding (base64, uuencode) |
(3). Method
The most important method in the system. Web. Mail namespace is the send method of the smtpmail object, which is called in the program to send emails. There are two ways to call this function:
I>. Direct call
The send method of the smtpmail object can be called as long as there are four parameters, that is, as long as there are four correct parameters, an email can be sent. The call format is as follows:
Smtpmail. Send ("mail Source Address", "mail target address", "mail subject", "Mail content ") |
However, this call is too simple and can only be used to send simple emails. For complicated emails, you must use the second method to call it:
II>. Call the mailmessage object as a parameter. The specific call is as follows:
Smtpmail. Send (mailmessage) |
Because there are many attributes of the mailmessage object, this call can enrich the mail content. Generally, this method is used to send emails. This call method is used in the following example.
3. Design Ideas and steps:
The first thing to note is that the program language we use in this ASP. NET program is Visual Basic. net.
Step 1: Include the namespace system. Web. Mail, and add the program language type used in the program:
Step 2: Initialize smtpmail and mailmessage objects:
Dim mailobj as new mailmessage Dim SMTP as new smtpmail 'Define the SMTP server name SMTP. smtpserver = "smtp.yeah.net" 'Define the mail sending Address Mailobj. From = zhangchulan@yesky.com" 'Define the mail Receiving address Mailobj. To = "majinhu@yesky.com" 'Defines the mail's dark sending Address Mailobj. bcc = "majinhu@163.net" 'Define the CC address of the mail Mailobj. Cc = "majinhu@yesky.com" 'Define the subject of the email. Mailobj. Subject = "my email system. Welcome! " 'Define the subject of the mail Mailobj. Body = "this is the subject of the email! " 'Emails are sent in HTML format. Mailobj. bodyformat = mailformat. html 'Define the mail's limited level, which is set to high here Mailobj. Priority = mailpriority. High 'Attach an attachment to the sent Email Mailobj. attachments. Add (New mailattachment ("C: \ test.doc ")) |
Step 3: send an email
4. ProceduresSource code
<Script language = VB runat = "server"> <br> <Br> Sub Sendmail (OBJ as object, e as eventargs) <br> <Br> Dim mailobj as new mailmessage <br> <Br> Dim SMTP as new smtpmail <br> <Br> 'Define the SMTP server name <br> <Br> SMTP. smtpserver = "smtp.yesky.com" <br> <Br> 'Define the mail sending address <br> <Br> Mailobj. From = "zhangchunlan@yesky.com" <br> <Br> 'Define the email Receiving address. <br> <Br> Mailobj. To = "majinhu@yesky.com" <br> <Br> 'Defines the mail's dark delivery address. <br> <Br> Mailobj. bcc = "majinhu@163.net" <br> <Br> 'Define the CC address of the email. <br> <Br> Mailobj. Cc = "majinhu@yesky.com" <br> <Br> 'Define the subject of the email. <br> <Br> Mailobj. Subject = "my email system. Welcome! "<Br> <Br> 'Define the subject of the email. <br> <Br> Mailobj. Body = "this is the subject of the email! "<Br> <Br> 'Emails are sent in HTML format. <br> <Br> Mailobj. bodyformat = mailformat. html <br> <Br> 'Define the mail's limited level <br> <Br> Mailobj. Priority = mailpriority. High <br> <Br> 'Add an attachment to the sent mail. Make sure that the root directory of drive C contains the test.doc file. <br> <Br> Mailobj. attachments. Add (New mailattachment ("C: \ test.doc ")) <Br> <Br> SMTP. Send (mailobj) <br> <Br> End sub <br> <Br> </SCRIPT> |