In practice, I used the COM component function of ASP to implement a small component for sending mails in VB. In ASP, this function can be implemented only by simple calling. All email processor systems are encapsulated in this component, which is extremely convenient to use. The following describes the basic development principles of this component and its application in ASP.
1. Use the Winsock Control to contact SMTP sending emails
The process of contact with SMTP includes handshaking, sending data, and closing. The main program is as follows:
Create a frmsendmail form that contains a Winsock Control and has the following public variables:
Public mstmp as string
// Stmp for sending mail
Public mfrom as string
// FROM address
Public MTO as string
// Arrival address
Public msubject as string
// Email Subject
Public mtext as string
// Body of the email
Sock. Connect mstmp, 25
// Establish contact with the stmp sending email
Private sub sock_connect ()
Sflag = sfconn
// Set parameters after successful connection
End sub
Private sub sock_dataarrival (byval bytestotal as long)
On Error goto Daerr
Dim s as string
Sock. getdata s
Select case sflag
Case sfstart
Case sfconn
Sflag = sfhelo
// Send the handshake MESSAGE Hello
Send "HELO" & mmyname
Case sfhelo
Sflag = sffrom
Send "mail from:" & getreal (mfrom)
Case sffrom
If left (s, 3) <>" 250 "Then goto srverr
// If the email address is successfully sent and received
Sflag = sfrcpt
Send "rcpt to:" & getreal (MTO)
Case sfrcpt
If left (s, 3) <>" 250 "Then goto srverr
// If data is sent successfully
Sflag = sfdata
Send "data"
Case sfdata
If left (s, 3) <>" 354 "Then goto srverr
Sflag = sfsendover // The data includes four items, ending.
Send "from:" & mfrom
Send "to:" & MTO
Send "Subject:" & msubject & vbcrlf
Send mtext
Send "."
Case sfsendover
If left (s, 3) <>" 250 "Then goto srverr
Sflag = sfstart
Sendok = true
Send "quit"
End select
Exit sub
End sub
2. encapsulate the above functions in a class
The components that ASP can use cannot contain controls. Therefore, you must encapsulate the preceding forms through the class module. First, create a form during class initialization:
Private sub class_initialize ()
Set FRM = new frmsendmail
End sub
Encapsulate the public variables of the form as attributes in the class module.
Function interfaces of this form are:
Public sub send ()
FRM. sendstart
End sub
3. Register this component
Compile the above project into a DLL file and use VB registration or manual registration.
4. Application in ASP
The call method is as follows:
Set Smail = server. Createobject ("sendmailx. Mail ")
Smail. stmp = "166.166.1.1"
Smail. logfile = "E:/logs/mail. log"
Smail. mfrom = mfromname & "<" & mfromaddr & "> &&"〉"
Smail. MTO = mtoname & "<" & mtoaddr & "> &&"〉"
Smail. msubject = msubject
Smail. mtext = mtext
Smail. Send
The variables can be assigned values or from the previous request page.
Due to the limited layout, can not be described in detail, need this component of friends can download to the http://flyfly.533.net software programming column.