首先是jmail.smtpmail的核心代碼:
Set jmail = Server.CreateObject("JMAIL.SMTPMail") '創(chuàng)建一個JMAIL對象
jmail.silent = true 'JMAIL不會拋出例外錯誤,返回的值為FALSE跟TRUE
jmail.logging = true '啟用使用日志
jmail.Charset = "GB2312" '郵件文字的代碼為簡體中文
jmail.ContentType = "text/html" '郵件的格式為HTML的
jmail.ServerAddress = "Server Address" '發(fā)送郵件的服務(wù)器
jmail.AddRecipient Email '郵件的收件人
jmail.SenderName = "SenderName" '郵件發(fā)送者的姓名
jmail.Sender = "Email Address" '郵件發(fā)送者的郵件地址
jmail.Priority = 1 '郵件的緊急程序,1 為最快,5 為最慢, 3 為默認值
jmail.Subject = "Mail Subject" '郵件的標題
jmail.Body = "Mail Body" '郵件的內(nèi)容
jmail.AddRecipientBCC Email '密件收件人的地址
jmail.AddRecipientCC Email '郵件抄送者的地址
jmail.Execute() '執(zhí)行郵件發(fā)送
jmail.Close '關(guān)閉郵件對象
--------------------------------------------------------------------------------
接下來是jmail.message的核心代碼:
Set jmail = Server.CreateObject("JMAIL.Message") '建立發(fā)送郵件的對象
jmail.silent = true '屏蔽例外錯誤,返回FALSE跟TRUE兩值
jmail.logging = true '啟用郵件日志
jmail.Charset = "GB2312" '郵件的文字編碼為國標
jmail.ContentType = "text/html" '郵件的格式為HTML格式
jmail.AddRecipient Email '郵件收件人的地址
jmail.From = "Email From for Sender" '發(fā)件人的E-MAIL地址
jmail.MailServerUserName = "UserName of Email" '登錄郵件服務(wù)器所需的用戶名
jmail.MailServerPassword = "Password of Email" '登錄郵件服務(wù)器所需的密碼
jmail.Subject = "Mail Subject" '郵件的標題
jmail.Body = "Mail Body" '郵件的內(nèi)容
jmail.Priority = 1 '郵件的緊急程序,1 為最快,5 為最慢, 3 為默認值
jmail.Send("Server Address") '執(zhí)行郵件發(fā)送(通過郵件服務(wù)器地址)
jmail.Close() '關(guān)閉對象
--------------------------------------------------------------------------------
再接著就是CDONTS的發(fā)郵件的簡單代碼:
Set cdomail = Server.CreateObject("CDONTS.NewMail") '建立郵件對象
cdomail.Subject = "Mail Subject" '郵件標題
cdomail.From = "Sender's Mail" '發(fā)件人的地址
cdomail.To = "Email will from" '收件人的地址
cdomail.Body = "Mail Body" '郵件的內(nèi)容
cdomail.Send '執(zhí)行發(fā)送
這種方法發(fā)送郵件是最簡單的,同時也帶來一定的問題,就是很少有服務(wù)器會開這項服務(wù)!
--------------------------------------------------------------------------------
以上三種列出的是兩個組件的三種發(fā)送方法,其中第二種是有身份驗證的,也就是說有要求驗證的郵箱如果不用這種方法發(fā)送的話,是無法收到信件的。如接收郵箱為 163、yahoo等,沒有用第二種方法發(fā)的話,是收不到郵件的。
接下來是一個簡單的例子,由于頁面的關(guān)系,我在此只做一個例子,另外兩種可以用同樣的方法來處理的。
我們寫程序,一般情況下都是說要代碼模塊化,這樣方便維護,同時也方便移植。因此,我在這里將這個發(fā)郵件的寫成一個子程,在調(diào)用的時候可以直接調(diào)用(當然,如果你高興寫成函數(shù)的話也是可以的,這個主要是看個人興趣)
這里引用第一種
Sub SendAction(subject, mailaddress, email, sender, content, fromer) '在這里,我把標題,發(fā)件服務(wù)器的地址,收件人,郵件內(nèi)容及發(fā)件人都列到這個項目中
'方便以后使用的時候直接修改發(fā)送表單,而不用去修改核心代碼
Set jmail = Server.CreateObject("JMAIL.SMTPMail") '創(chuàng)建一個JMAIL對象
jmail.silent = true 'JMAIL不會拋出例外錯誤,返回的值為FALSE跟TRUE
jmail.logging = true '啟用使用日志
jmail.Charset = "GB2312" '郵件文字的代碼為簡體中文
jmail.ContentType = "text/html" '郵件的格式為HTML的
jmail.ServerAddress = mailaddress '發(fā)送郵件的服務(wù)器
jmail.AddRecipient Email '郵件的收件人
jmail.SenderName = sender '郵件發(fā)送者的姓名
jmail.Sender = fromer '郵件發(fā)送者的郵件地址
jmail.Priority = 1 '郵件的緊急程序,1 為最快,5 為最慢, 3 為默認值
jmail.Subject = subject '郵件的標題
jmail.Body = content '郵件的內(nèi)容
'由于沒有用到密抄跟抄送,這里屏蔽掉這兩句,如果您有需要的話,可以在這里恢復
'jmail.AddRecipientBCC Email '密件收件人的地址
'jmail.AddRecipientCC Email '郵件抄送者的地址
jmail.Execute() '執(zhí)行郵件發(fā)送
jmail.Close '關(guān)閉郵件對象
End Sub
以上我已經(jīng)將代碼做成一個塊,下面是對該塊的具體引用法!
如我收到的值分別如下:
郵件標題(subject):JMAIL發(fā)信
郵件內(nèi)容(content):這是一封測試郵件
發(fā)信人的地址(fromer):runbing@eyou.com
發(fā)信者的姓名(sender):帥青蛙
收信人的地址(email):runbing@eyou.com
郵件服務(wù)器的地址(mailaddress):mail.ubbcn.com
則我調(diào)用發(fā)信塊的時候(SendAction subject,mailaddress,email,sender,content,fromer)最終變成這樣
Set jmail = Server.CreateObject("JMAIL.SMTPMail")
jmail.silent = true
jmail.logging = true
jmail.Charset = "GB2312"
jmail.ContentType = "text/html"
jmail.ServerAddress = [color=blue]"mail.ubbcn.com"[/color]
jmail.AddRecipient [color=blue]"runbing@eyou.com"[/color]
jmail.SenderName = [color=blue]"帥青蛙"[/color]
jmail.Sender = [color=blue]"runbing@eyou.com"[/color]
jmail.Priority = 1
jmail.Subject = [color=blue]"JMAIL發(fā)信" [/color]
jmail.Body = [color=blue]"這是一封測試郵件"[/color]
jmail.Execute()
jmail.Close
--------------------------------------------------------------------------------
下面是一個發(fā)信的例子,以HTML格式發(fā)不出信的關(guān)鍵問題是EMAIL地址要找對,另外一些大網(wǎng)站比如163.com在發(fā)信的時候需要設(shè)置發(fā)信信箱和密碼的,不認證的話,有時候收不到信的
另外發(fā)信服務(wù)器最好是用企業(yè)郵局的,我用了幾個月,基本上都可以發(fā)信的
sendname = Request.Form ("name")
email= Request.Form ("email")
bh=Request.Form ("bh")
SystemEmail= Request.Form ("fmail")
pic = Request.Form ("pic")
mailbody="<html>"
mailbody=mailbody & "<title>&想您推薦一個產(chǎn)品</title>"
mailbody=mailbody & "<body>"
mailbody=mailbody & "<TABLE border=0 width='95%' align=center><TBODY><TR>"
mailbody=mailbody & "<TD valign=middle align=top>"
mailbody=mailbody &"您好:<br><br>"
mailbody=mailbody & "您的朋友("&sendname&")發(fā)送了一張圖片給你<br>"
mailbody=mailbody & "下面是相關(guān)信息:<br>"
mailbody=mailbody & "公司名稱:"&bh&"<br>"
mailbody=mailbody & "產(chǎn)品圖:<img src=http://www.szwin.net/chanpin/"&pic&" > <br>"
mailbody=mailbody & "<br><br>"
mailbody=mailbody & "<center><font color=red>希望您能選擇這個產(chǎn)品!</font>"
mailbody=mailbody & "</TD></TR></TBODY></TABLE><br><hr width=95% size=1>"
mailbody=mailbody & "</body>"
mailbody=mailbody & "</html>"
Set jmail = Server.CreateObject("JMail.Message")
jmail.silent = true
jmail.Logging = true
jmail.Charset = "gb2312"
JMail.ContentType = "text/html"
jmail.MailServerUserName ="123@szwin.net" '輸入smtp服務(wù)器驗證登陸名 (郵局中任何一個用戶的Email地址)
jmail.MailServerPassword = "******" '輸入smtp服務(wù)器驗證密碼 (用戶Email帳號對應(yīng)的密碼)
jmail.From = SystemEmail '發(fā)件人Email
jmail.FromName = sendname '發(fā)件人姓名
jmail.AddRecipient (email) '收件人Email
jmail.Subject = "想您推薦產(chǎn)品" '信件主題
jmail.Body = mailbody '正文
jmail.Send ("mail.szwin.net") 'smtp服務(wù)器地址(企業(yè)郵局地址)
set jmail = nothing
SendMailed = "OK"
如果大家想用文本的格式,可以用這個參考
每一行都會自動換行的
content = content & "用戶帳號:" & uid & vbcrlf
content = content & "用戶密碼:" & pwd & vbcrlf
content = content & "用戶郵件:" & email & vbcrlf
content = content & "用戶QQ:" & oicq & vbcrlf
content = content & "用戶地址:"& address & vbcrlf
content = content & "用戶電話:" & tel & vbcrlf
content = content & "用戶傳真:" & fax & vbcrlf
content = content & "注冊時間:" & cstr(now()) & vbcrlf
content = content &"信息服務(wù)由 "&homename&"提供("&homepage&")"& vbcrlf
下面是反饋表單的
<%
name = Request.Form ("gongsi")
name = Request.Form ("address")
name = Request.Form ("name")
email= Request.Form ("email")
phone= Request.Form ("phone")
mailsubject= Request.Form ("mailsubject")
text1= Request.Form ("text1")
%>
<%
mess = mess & "---------------客戶反饋意見開始-------------------" & vbcrlf
mess = mess & "發(fā)件人公司名稱:" & gongsi & vbcrlf
mess = mess & "發(fā)件人公司地址:" & address & vbcrlf
mess = mess & "發(fā)件人姓名:" & name & vbcrlf
mess = mess & "發(fā)件人郵件:" & email & vbcrlf
mess = mess & "發(fā)件人電話:" & phone & vbcrlf
mess = mess & "發(fā)件人內(nèi)容:" & text1 & vbcrlf
mess = mess & "---------------客戶反饋意見結(jié)束-------------------"
SystemEmail=Request.Form ("email")
sendname=Request.Form ("name")
Set jmail = Server.CreateObject("JMail.Message")
JMail.Priority = 1
JMail.Silent=true
JMail.Charset = "gb2312"
jmail.MailServerUserName ="123@szwin.net" '輸入smtp服務(wù)器驗證登陸名 (郵局中任何一個用戶的Email地址)
jmail.MailServerPassword = "*******" '輸入smtp服務(wù)器驗證密碼 (用戶Email帳號對應(yīng)的密碼)
jmail.From = SystemEmail '發(fā)件人Email
jmail.FromName = sendname '發(fā)件人姓名
jmail.AddRecipient "itpc@itpc.cn" '收件人Email
jmail.Subject = mailsubject '信件主題
jmail.Body = mess '正文
jmail.Send ("mail.szwin.net") 'smtp服務(wù)器地址(企業(yè)郵局地址)
set jmail = nothing
SendMailed = "OK"
%>
<Script language="javascript">
alert("郵件發(fā)送成功");
history.back();
</Script>
--------------------------------------------------------------------------------
下面老補充一個ASPEMAIL的
實例
sub sendMail(a_intID , a_strEmail)
const c_strMailServer = "smtp.163.net"
dim objConn,objRs,strSql
dim strContent,strBody,strSubject,objEmail
strBody = "<html><head>"
strBody = strBody + "<meta HTTP-EQUIV=Content-Type content=text/html; charset=gb2312>"
strBody = strBody + "</head><body bgColor=#d2b48c>"
strBody = strBody + "<a + "<img src=http://www.china-168.net/center/images/banner.gif border=0>" + "</a>"
strBody = strBody + "<h3 style='FONT-SIZE: 15px'>" + "請您確認" + "</h>" + vbcrlf
strBody = strBody + "<p style='FONT-SIZE: 13px'>" + "您好<a href=mailto:"&a_strEmail&">"&a_strEmail&"</a>" + "</td>" + vbcrlf
strBody = strBody + "<p style='FONT-SIZE: 13px'>" + "歡迎您訂閱萬里信息網(wǎng)郵件列表" +"</td>"
strBody = strBody + "<p style='FONT-SIZE: 13px'>" + "為了防止出現(xiàn)訂閱錯誤,請您點擊以下鏈接進行確認。謝謝!" + "</td>"
strBody = strBody + "<p style='FONT-SIZE: 13px'>" + "<a href=""http://www.china-168.net/EmailList/affirm.asp?Mode=MailList&content="&a_strEmail&""">" + "請點擊這里,進行訂閱確認" + "</a>"
strBody = strBody + "<p style='FONT-SIZE: 13px'><a href=http://www.china-168.net target='_blank'>" + vbcrlf
strBody = strBody + "<img src=http://www.china-168.net/images/logo.gif"
strBody = strBody + " alt='更多信息盡在萬里信息網(wǎng)'></a><br>"
strBody = strBody + "<p style='FONT-SIZE: 13px'>" + "歡迎再次光臨萬里信息網(wǎng)!" + "</td>"
strBody = strBody + "</body></html>"
'發(fā)送email
on error resume next
set objEmail = server.CreateObject("Persits.MailSender")
objEmail.Host = c_strMailServer
objEmail.From = "jiabaoxu@163.net"
objEmail.FromName = "china-168.net"
objEmail.AddAddress a_strEmail
objEmail.Subject = "訂閱確認"
objEmail.IsHTML = true
objEmail.CharSet = "gb2312"
objEmail.Body = strBody
objEmail.Send
'檢察錯誤
if Err.number = 0 then '如果成功
Response.Write ("<p align=center class=cn>為確保用戶不被騷擾,我們將發(fā)出確認信,請訪問信中的URL以確認登記</td>")
else '如果失敗則顯示出錯原因
Response.Write ("<p align=center class=cn>郵件發(fā)送出錯,錯誤原因:<br>")
Response.Write ("<font color=red>"&Err.Description&"</font></td>")
end if
'清場
set objEmail = nothing
end sub 使用該過程:
<%
.....RTSEmail GuestMailID,""&email&""
.....%>
--------------------------------------------------------------------------------
1、注意取得EMAIL的方法,這個很重要,另外注意
JMail.SMTPMail和JMAIL.Message一個是不需要用戶名稱
密碼認證一個是需要!
2、最好設(shè)置認證用戶名稱和密碼的,這樣可以保證發(fā)信的效率
我以前用不認證的163.com sohu.com的免費信箱很難收到信的!
3、注意各項參數(shù)正確,比如發(fā)信人姓名,發(fā)信人EMAIL,主題
如果有的參數(shù)取不到正確的值,也會收不到信的
另外綜合一個動網(wǎng)的BBS里面的發(fā)信代碼,讓大家加深一點印象
<%
sub Jmail(email,topic,mailbody)
on error resume next
dim JMail
Set JMail=Server.CreateObject("JMail.SMTPMail")
JMail.Logging=True
JMail.Charset="gb2312"
JMail.ContentType = "text/html"
JMail.ServerAddress=Forum_info(4)
JMail.Sender=Forum_info(5)
JMail.Subject=topic
JMail.Body=mailbody
JMail.AddRecipient email
JMail.Priority=1
'JMail.MailServerUserName = "shatan@dvbbs.net" '您的郵件服務(wù)器登錄名
'JMail.MailServerPassword = "nihaoma" '登錄密碼
JMail.Execute
Set JMail=nothing
if err then
SendMail=err.description
err.clear
else
SendMail="OK"
end if
end sub
sub Cdonts(email,topic,mailbody)
on error resume next
dim objCDOMail
Set objCDOMail = Server.CreateObject("CDONTS.NewMail")
objCDOMail.From =Forum_info(5)
objCDOMail.To =email
objCDOMail.Subject =topic
objCDOMail.BodyFormat = 0
objCDOMail.MailFormat = 0
objCDOMail.Body =mailbody
objCDOMail.Send
Set objCDOMail = Nothing
if err then
SendMail=err.description
err.clear
else
SendMail="OK"
end if
end sub
sub aspemail(email,topic,mailbody)
on error resume next
dim mailer,recipient,sender,subject,message
dim mailserver,result
Set mailer=Server.CreateObject("ASPMAIL.ASPMailCtrl.1")
recipient=email
sender=Forum_info(5)
subject=topic
message=mailbody
mailserver=Forum_info(4)
result=mailer.SendMail(mailserver, recipient, sender, subject, message)
if err then
SendMail=err.description
err.clear
else
SendMail="OK"
end if
end sub
sub Emailbody(body)
Emailbody=Emailbody &"<style>A:visited { TEXT-DECORATION: none }"
Emailbody=Emailbody &"A:active { TEXT-DECORATION: none }"
Emailbody=Emailbody &"A:hover { TEXT-DECORATION: underline overline }"
Emailbody=Emailbody &"A:link { text-decoration: none;}"
Emailbody=Emailbody &"A:visited { text-decoration: none;}"
Emailbody=Emailbody &"A:active { TEXT-DECORATION: none;}"
Emailbody=Emailbody &"A:hover { TEXT-DECORATION: underline overline}"
Emailbody=Emailbody &"BODY { FONT-FAMILY: 宋體; FONT-SIZE: 14px;}"
Emailbody=Emailbody &"TD { FONT-FAMILY: 宋體; FONT-SIZE: 14px }</style>"
Emailbody=Emailbody &"<TABLE border=0 width='95%' align=center><TBODY><TR>"
Emailbody=Emailbody &"<TD valign=middle align=top>"
Emailbody=Emailbody & body
Emailbody=Emailbody &"</TD></TR></TBODY></TABLE><br><hr width=95% size=1>"
Emailbody=Emailbody & Copyright & " " & Version
Emailbody=mailbody
end sub
%>