首先,必須有錯誤繼續進行的聲明On Error Resume Next
然後嘗試簡歷jmail實例:
Dim JMail
Set JMail=Server.CreateObject("JMail.Message")
對實例做出判斷,如果組件沒有安裝成功,則沒有創建實例:
If JMail Is Nothing Then
Response.Write "不支持"
Else
Response.Write "支持"
End If
其他組件同樣處理,非常簡單了。
最好在global文件中處理,裡面使用的時候不用那麼麻煩了。
最好的方法把郵件生成放到一個表裡,然後再說發送的事情。
復制代碼 代碼如下:
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","來自網上的客戶留言","<html><head><meta http-equiv=""Content-Type"" content=""text/html; charset=gb2312""><title>網站用戶留言</title></head><body>留言人:"&txtname&"<br>性別:"&xingbie&"<br>咨詢網站:"&txtweb&"<br>聯系方式:"&txttel&"<br>留言內容:"&content&"<br>IP地址:"&ipaddress&"<br>留言
' ============================================
' jmail發送郵件
' ============================================
Function Jmail(mailTo,mailTopic,mailBody,mailCharset,mailContentType)
'入口參數:
' mailTo 收件人email地址
' mailTopic 郵件主題
' mailBody 郵件正文(內容)
' mailCharset 郵件字符集,例如GB2312或US-ASCII
' mailContentType 郵件正文格式,例如text/plain或text/html
'返回值:
' 字符串,發送成功後返回OK,不成功返回錯誤信息
'使用方法:
' 1)設置好常量,即以Const開頭的變量
' 2)使用類似如下代碼發信
'Dim SendStat
'SendStat = Jmail("
[email protected]","測試Jmail","這是一封<br/>測試信!","GB2312","text/html")
'Response.Write SendStat
'***************根據需要設置常量開始*****************
Dim ConstFromNameCn,ConstFromNameEn,ConstFrom,ConstMailDomain,ConstMailServerUserName,ConstMailServerPassword
ConstFromNameCn = "彩票網"'發信人中文姓名(發中文郵件的時候使用),例如‘張三'
ConstFromNameEn = "bc5"'發信人英文姓名(發英文郵件的時候使用),例如‘zhangsan'
ConstFrom = "
[email protected]"'發信人郵件地址,例如‘
[email protected]'
ConstMailDomain = "smtp.163.com"'smtp服務器地址,例如smtp.163.com
ConstMailServerUserName = "
[email protected]"'smtp服務器的信箱登陸名,例如‘zhangsan'。注意要與發信人郵件地址一致!
ConstMailServerPassword = "www.jb51.net"'smtp服務器的信箱登陸密碼
'***************根據需要設置常量結束*****************
'-----------------------------以下內容無需改動------------------------------
On Error Resume Next
Dim myJmail
Set myJmail = Server.CreateObject("JMail.Message")
myJmail.Logging = False'記錄日志
myJmail.ISOEncodeHeaders = False'郵件頭不使用ISO-8859-1編碼
myJmail.ContentTransferEncoding = "base64"'郵件編碼設為base64
myJmail.AddHeader "Priority","3"'添加郵件頭,不要改動!
myJmail.AddHeader "MSMail-Priority","Normal"'添加郵件頭,不要改動!
myJmail.AddHeader "Mailer","Microsoft Outlook Express 6.00.2800.1437"'添加郵件頭,不要改動!
myJmail.AddHeader "MimeOLE","Produced By Microsoft MimeOLE V6.00.2800.1441"'添加郵件頭,不要改動!
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