e-Mail Online simple and fast implementation of ASP online email function
--------------------------------------------------------------------------------
In the preparation of online applications, often encountered the need to send mail Online, the content of the message is determined by the program, if you are using ASP to write online applications, how to achieve this function simply and quickly?
In practice, the author uses ASP's COM component function to implement a small component in VB, which can be realized only by easy invocation in ASP. All mail processing mechanisms are encapsulated in this component and are extremely handy to use. The basic development principle of the component and its application in ASP are described in detail below.
1. Use the Winsock control to contact SMTP for outgoing messages
The connection to SMTP includes the whole process of shaking hands, sending data, and shutting down, the main procedures are as follows:
Create a Frmsendmail form that contains a Winsock control with the following public variables:
Public mstmp As String
Stmp to send mail
Public Mfrom As String
From address
Public MTO As String
Arrival Address
Public Msubject As String
Message subject
Public Mtext As String
Message body
Sock.connect Mstmp, 25
Establish contact with the stmp that sent the message
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 a handshake message hello
Send "Helo" && mmyname
Case Sfhelo
Sflag = Sffrom
Send "mail from:" && getreal (Mfrom)
Case Sffrom
If left (S, 3) 〈〉 "then" Goto Srverr
If the receive mail address is sent successfully
Sflag = Sfrcpt
Send "RCPT TO:" && getreal (MTO)
Case SFRCPT
If left (S, 3) 〈〉 "then" Goto Srverr
If you start sending data successfully
Sflag = Sfdata
Send "Data"
Case Sfdata
If left (S, 3) 〈〉 "354" then Goto Srverr
Sflag = sfsendover//data includes 4 items, finally. End
Send "from:" && mfrom
Send "to:" && MTO
Send "Subject:" && Msubject && vbCrLf
Send Mtext
Send "."
Case Sfsendover
If left (S, 3) 〈〉 "then" Goto Srverr
Sflag = Sfstart
Sendok = True
Send "Quit"
End Select
Exit Sub
End Sub
2. Encapsulate the above functionality in a class
Because the controls cannot exist in the components that the ASP can use, the above form is encapsulated by a class module. First create a form when the class is initialized:
Private Sub Class_Initialize ()
Set frm = new Frmsendmail
End Sub
Encapsulates a form's public variable as a property in a class module.
The function interface for the form is:
Public Sub Send ()
Frm.sendstart
End Sub
3. Registering the component
Compile the above project into a DLL file, either through VB Registration or manual registration.
4. The application in ASP
The method is invoked 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
Where variables can be assigned or from the previous request page.