asp发送邮件(email)方法集合

时间:2009-02-16 00:31:30  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读: />jmail.Charset = "GB2312" '邮件文字的代码为简体中文 jmail.ContentType = "text/html" '邮件的格式为HTML的 jmail.ServerAddress = "Server Address" '发送邮件的服务器 jmail.AddRecipient Email '邮件的收件人 jmail.SenderName = "SenderName" '邮件发送者的姓名 jmail.Sender = "Email Address&quo...
  正文:首先是jmail.smtpmail的核心代码

Set jmail = Server.CreateObject("JMAIL.SMTPMail") '创建一个JMAIL对象
jmail.silent = true 'JMAIL不会抛出例外错误,返回的值为FALSE跟TRUE
jmail.logging = true '启用使用日志
jmail.Charset = "GB2312" '邮件文字的代码为简体中文
jmail.ContentType = "text/html" '邮件的格式为HTML的
jmail.ServerAddress = "Server Address" '发送邮件的服务器
jmail.AddRecipient Email '邮件的收件人
jmail.SenderName = "SenderName" '邮件发送者的姓名
jmail.Sender = "Email Address" '邮件发送者的邮件地址
jmail.Priority = 1 '邮件的紧急程序,1 为最快,5 为最慢, 3 为默认值
jmail.Subject = "Mail Subject" '邮件的标题
jmail.Body = "Mail Body" '邮件的内容
jmail.AddRecipientBCC Email '密件收件人的地址
jmail.AddRecipientCC Email '邮件抄送者的地址
jmail.Execute() '执行邮件发送
jmail.Close '关闭邮件对象
接下来是jmail.message的核心代码:

Set jmail = Server.CreateObject("JMAIL.Message") '建立发送邮件的对象
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" '发件人的E-MAIL地址
jmail.MailServerUserName = "UserName of Email" '登录邮件服务器所需的用户名
jmail.MailServerPassword = "Password of Email" '登录邮件服务器所需的密码
jmail.Subject = "Mail Subject" '邮件的标题
jmail.Body = "Mail Body" '邮件的内容
jmail.Priority = 1 '邮件的紧急程序,1 为最快,5 为最慢, 3 为默认值
jmail.Send("Server Address") '执行邮件发送(通过邮件服务器地址)
jmail.Close() '关闭对象
再接着就是CDONTS的发邮件的简单代码:

Set cdomail = Server.CreateObject("CDONTS.NewMail") '建立邮件对象
cdomail.Subject = "Mail Subject" '邮件标题
cdomail.From = "Sender's Mail" '发件人的地址
cdomail.To = "Email will from" '收件人的地址
cdomail.Body = "Mail Body" '邮件的内容
cdomail.Send '执行发送

这种方法发送邮件是最简单的,同时也带来一定的问题,就是很少有服务器会开这项服务!
以上三种列出的是两个组件的三种发送方法,其中第二种是有身份验证的,也就是说有要求验证的邮箱如果不用这种方法发送的话,是无法收到信件的。如接收邮箱为 163、yahoo等,没有用第二种方法发的话,是收不到邮件的。

接下来是一个简单的例子,由于页面的关系,我在此只做一个例子,另外两种可以用同样的方法来处理的。

我们写程序,一般情况下都是说要代码模块化,这样方便维护,同时也方便移植。因此,我在这里将这个发邮件的写成一个子程,在调用的时候可以直接调用(当然,如果你高兴写成函数的话也是可以的,这个主要是看个人兴趣)



这里引用第一种

Sub SendAction(subject, mailaddress, email, sender, content, fromer) '在这里,我把标题,发件服务器的地址,收件人,邮件内容及发件人都列到这个项目中
'方便以后使用的时候直接修改发送表单,而不用去修改核心代码
Set jmail = Server.CreateObject("JMAIL.SMTPMail") '创建一个JMAIL对象
jmail.silent = true 'JMAIL不会抛出例外错误,返回的值为FALSE跟TRUE
jmail.logging = true '启用使用日志
jmail.Charset = "GB2312" '邮件文字的代码为简体中文
jmail.ContentType = "text/html" '邮件的格式为HTML的
jmail.ServerAddress = mailaddress '发送邮件的服务器
jmail.AddRecipient Email '邮件的收件人
jmail.SenderName = sender '邮件发送者的姓名
jmail.Sender = fromer '邮件发送者的邮件地址
jmail.Priority = 1 '邮件的紧急程序,1 为最快,5 为最慢, 3 为默认值
jmail.Subject = subject '邮件的标题
jmail.Body = content '邮件的内容
'由于没有用到密抄跟抄送,这里屏蔽掉这两句,如果您有需要的话,可以在这里恢复
'jmail.AddRecipientBCC Email '密件收件人的地址
'jmail.AddRecipientCC Email '邮件抄送者的地址
jmail.Execute() '执行邮件发送
jmail.Close '关闭邮件对象
End Sub

以上我已经将代码做成一个块,下面是对该块的具体引用法!


如我收到的值分别如下:

邮件标题(subject):JMAIL发信
邮件内容(content):这是一封测试邮件
发信人的地址(fromer):runbing@eyou.com
发信者的姓名(sender):帅青蛙
收信人的地址(email):runbing@eyou.com
邮件服务器的地址(mailaddress):mail.ubbcn.com

则我调用发信块的时候(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发信" [/color]
jmail.Body = [color=blue]"这是一封测试邮件"[/color]
jmail.Execute()
jmail.Close



下面是一个发信的例子,以HTML格式发不出信的关键问题是EMAIL地址要找对,另外一些大网站比如163.com在发信的时候需要设置发信信箱和密码的,不认证的话,有时候收不到信的
另外发信服务器最好是用企业邮局的,我用了几个月,基本上都可以发信的
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>&想您推荐一个产品</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&")发送了一张图片给你<br>"
mailbody=mailbody & "下面是相关信息:<br>"
mailbody=mailbody & "公司名称:"&bh&"<br>"
mailbody=mailbody & "产品图:<img src=http://www.szwin.net/chanpin/"&pic&" > <br>"
mailbody=mailbody & "<br><br>"
mailbody=mailbody & "<center><font color=red>希望您能选择这个产品!</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服务器验证登陆名 (邮局中任何一个用户的Email地址)
jmail.MailServerPassword = "******" '输入smtp服务器验证密码 (用户Email帐号对应的密码)
jmail.From = SystemEmail '发件人Email
jmail.FromName = sendname '发件人姓名
jmail.AddRecipient (email) '收件人Email
jmail.Subject = "想您推荐产品" '信件主题
jmail.Body = mailbody '正文
jmail.Send ("mail.szwin.net") 'smtp服务器地址(企业邮局地址)
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 &"信息服务由 "&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 & "发件人公司名称:" & gongsi & vbcrlf
mess = mess & "发件人公司地址:" & address & vbcrlf
mess = mess & "发件人姓名:" & name & vbcrlf
mess = mess & "发件人邮件:" & email & vbcrlf
mess = mess & "发件人电话:" & phone & vbcrlf
mess = mess & "发件人内容:" & text1 & vbcrlf
mess = mess & "---------------客户反馈意见结束-------------------"
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服务器验证登陆名 (邮局中任何一个用户的Email地址)
jmail.MailServerPassword = "*******" '输入smtp服务器验证密码 (用户Email帐号对应的密码)
jmail.From = SystemEmail '发件人Email
jmail.FromName = sendname '发件人姓名
jmail.AddRecipient "itpc@itpc.cn" '收件人Email
jmail.Subject = mailsubject '信件主题
jmail.Body = mess '正文
jmail.Send ("mail.szwin.net") 'smtp服务器地址(企业邮局地址)
set jmail = nothing
SendMailed = "OK"

%>
<Script language="JavaScript">
alert("邮件发送成功");
history.back();
</Script>


下面老补充一个ASP/ target=_blank class=infotextkey>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 href='http://www.china-168.net/center/default.asp>" + "<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'>" + "欢迎您订阅万里信息网邮件列表" +"</td>"
strBody = strBody + "<p style='FONT-SIZE: 13px'>" + "为了防止出现订阅错误,请您点击以下链接进行确认。谢谢!" + "</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='更多信息尽在万里信息网'></a><br>"
strBody = strBody + "<p style='FONT-SIZE: 13px'>" + "欢迎再次光临万里信息网!" + "</td>"
strBody = strBody + "</body></html>"

'发送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>为确保用户不被骚扰,我们将发出确认信,请访问信中的URL以确认登记</td>")
else '如果失败则显示出错原因
Response.Write ("<p align=center class=cn>邮件发送出错,错误原因:<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、最好设置认证用户名称和密码的,这样可以保证发信的效率
我以前用不认证的163.com sohu.com的免费信箱很难收到信的!
3、注意各项参数正确,比如发信人姓名,发信人EMAIL,主题
如果有的参数取不到正确的值,也会收不到信的
另外综合一个动网的BBS里面的发信代码,让大家加深一点印象
<%
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" '您的邮件服务器登录名
'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: 9pt;}"
Emailbody=Emailbody &"TD { FONT-FAMILY: 宋体; FONT-SIZE: 9pt }</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
%> "asp发送邮件(email)方法集合"由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Programming/ASP/2009-02-16/9380.html


关键字:

关于《asp发送邮件(email)方法集合》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td