首先,必須有錯(cuò)誤繼續(xù)進(jìn)行的聲明On Error Resume Next
然后嘗試簡(jiǎn)歷jmail實(shí)例:
Dim JMail
Set JMail=Server.CreateObject("JMail.Message")
對(duì)實(shí)例做出判斷,如果組件沒有安裝成功,則沒有創(chuàng)建實(shí)例:
If JMail Is Nothing Then
Response.Write "不支持"
Else
Response.Write "支持"
End If
其他組件同樣處理,非常簡(jiǎn)單了。
最好在global文件中處理,里面使用的時(shí)候不用那么麻煩了。
最好的方法把郵件生成放到一個(gè)表里,然后再說發(fā)送的事情。
復(fù)制代碼代碼如下:
Function IsObjInstalled(strClassString)
On Error Resume Next
IsObjInstalled = False
Err = 0
Dim xTestObj
Set xTestObj = Server.CreateObject(strClassString)
If 0 = Err Then IsObjInstalled = True
Set xTestObj = Nothing
Err = 0
End Function
判斷代碼:
if IsObjInstalled("JMail.Message")=True then{
if IsObjInstalled("JMail.Message") =True then
SendStat = Jmail("***@jb51.net","來自網(wǎng)上的客戶留言","<html><head><meta http-equiv=""Content-Type"" content=""text/html; charset=gb2312""><title>網(wǎng)站用戶留言</title></head><body>留言人:"&txtname&"<br>性別:"&xingbie&"<br>咨詢網(wǎng)站:"&txtweb&"<br>聯(lián)系方式:"&txttel&"<br>留言內(nèi)容:"&content&"<br>IP地址:"&ipaddress&"<br>留言時(shí)間:"&now()&"<br><br>本郵件由系統(tǒng)自動(dòng)發(fā)送,無須回復(fù)<!--服務(wù)器之家www.ythuaji.com.cn--><br><br></body></html>","GB2312","text/html")
end if
}
jmail發(fā)信函數(shù)
復(fù)制代碼代碼如下:
' ============================================
' jmail發(fā)送郵件
' ============================================
Function Jmail(mailTo,mailTopic,mailBody,mailCharset,mailContentType)
'入口參數(shù):
' mailTo 收件人email地址
' mailTopic 郵件主題
' mailBody 郵件正文(內(nèi)容)
' mailCharset 郵件字符集,例如GB2312或US-ASCII
' mailContentType 郵件正文格式,例如text/plain或text/html
'返回值:
' 字符串,發(fā)送成功后返回OK,不成功返回錯(cuò)誤信息
'使用方法:
' 1)設(shè)置好常量,即以Const開頭的變量
' 2)使用類似如下代碼發(fā)信
'Dim SendStat
'SendStat = Jmail("[email protected]","測(cè)試Jmail","這是一封<br/>測(cè)試信!","GB2312","text/html")
'Response.Write SendStat
'***************根據(jù)需要設(shè)置常量開始*****************
Dim ConstFromNameCn,ConstFromNameEn,ConstFrom,ConstMailDomain,ConstMailServerUserName,ConstMailServerPassword
ConstFromNameCn = "彩票網(wǎng)"'發(fā)信人中文姓名(發(fā)中文郵件的時(shí)候使用),例如‘張三'
ConstFromNameEn = "bc5"'發(fā)信人英文姓名(發(fā)英文郵件的時(shí)候使用),例如‘zhangsan'
ConstFrom = "[email protected]"'發(fā)信人郵件地址,例如‘[email protected]'
ConstMailDomain = "smtp.163.com"'smtp服務(wù)器地址,例如smtp.163.com
ConstMailServerUserName = "[email protected]"'smtp服務(wù)器的信箱登陸名,例如‘zhangsan'。注意要與發(fā)信人郵件地址一致!
ConstMailServerPassword = "www.ythuaji.com.cn"'smtp服務(wù)器的信箱登陸密碼
'***************根據(jù)需要設(shè)置常量結(jié)束*****************
'-----------------------------以下內(nèi)容無需改動(dòng)------------------------------
On Error Resume Next
Dim myJmail
Set myJmail = Server.CreateObject("JMail.Message")
myJmail.Logging = False'記錄日志
myJmail.ISOEncodeHeaders = False'郵件頭不使用ISO-8859-1編碼
myJmail.ContentTransferEncoding = "base64"'郵件編碼設(shè)為base64
myJmail.AddHeader "Priority","3"'添加郵件頭,不要改動(dòng)!
myJmail.AddHeader "MSMail-Priority","Normal"'添加郵件頭,不要改動(dòng)!
myJmail.AddHeader "Mailer","Microsoft Outlook Express 6.00.2800.1437"'添加郵件頭,不要改動(dòng)!
myJmail.AddHeader "MimeOLE","Produced By Microsoft MimeOLE V6.00.2800.1441"'添加郵件頭,不要改動(dòng)!
myJmail.Charset = mailCharset
myJmail.ContentType = mailContentType
If UCase(mailCharset) = "GB2312" Then
myJmail.FromName = ConstFromNameCn
Else
myJmail.FromName = ConstFromNameEn
End If
myJmail.From = ConstFrom
myJmail.Subject = mailTopic
myJmail.Body = mailBody
myJmail.AddRecipient mailTo
myJmail.MailDomain = ConstMailDomain
myJmail.MailServerUserName = ConstMailServerUserName
myJmail.MailServerPassword = ConstMailServerPassword
myJmail.Send ConstMailDomain
myJmail.Close
Set myJmail=nothing
If Err Then
Jmail=Err.Description
Err.Clear
Else
Jmail="OK"
End If
On Error Goto 0
End Function