When writing online applications, we often encounter the problem of sending emails online. The email content is dynamically determined by the program. If you use ASP to write online applications, how can you implement this function simply and quickly?
In practice, the author uses the COM component function of ASP to implement a small component for sending emails in VB. This function can be realized by simply calling it in ASP. All email processing mechanisms are encapsulated in this component, which is extremely convenient to use. The basic development principles of this component and its application in ASP will be introduced in detail below.
1. Use the Winsock control to contact SMTP for sending emails
, and the SMTP contact includes the whole process of handshaking, sending data, and closing. The main procedures are as follows:
Create a frmsendmail form, which contains a winsock control and has the following public variables:
public mstmp as string
// stmp for sending email
public mfrom as string
// from address
public mto as string
// arrival address
public msubject as string
// email subject
public mtext as string
// email body
sock.connect mstmp, 25
// and send Establish contact via stmp of 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 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 receiving email address is successfully sent,
sflag = sfrcpt
send "rcpt to:" && getreal(mto)
case sfrcpt
if left(s, 3) 〈〉 "250" then goto srverr
//If successful, start sending data
sflag = sfdata
send "data"
case sfdata
if left(s, 3 ) 〈〉 "354" then goto srverr
sflag = sfsendover //The data includes 4 items and ends with .
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
Since controls cannot exist in the components that ASP can use, the above forms must be encapsulated through class modules. First, create a form during class initialization:
private sub class_initialize()
set frm = new frmsendmail
end sub
encapsulates the public variables of the form as attributes in the class module.
The function interface of this form is:
public sub send()
frm.sendstart
end sub
3. Register this component
to compile the above project into a dll file. You can register it through VB or manually.
4.
Theapplication calling method in ASP
is as follows:
set smail=server.createobject("sendmailx.mail")
smail.stmp="166.166.1.1"
smail.logfile="e:logsmail.log"
smail.mfrom = mfromname && " 〈" && mfromaddr && "〉"
smail.mto = mtoname && " 〈" && mtoaddr && "〉"
smail.msubject = msubject
smail.mtext = mtext
smail.send
where the variable can be assigned or come from the previous request page.