ASP如何实现发邮件的功能

ASP如何实现发邮件的功能
解决思路
   ASP中没有直接发邮件的方法,需要依靠组件来实现,常用的组件有CDONTS和Jmail两个。
具体步骤
方法一:用 CDONTS 组件发邮件。
代码示例:

<%
Set MyMail = Server.CreateObject(“CDONTS.NewMail”)
MyMail.From = “from@domain.com” ’发信人的信箱地址,必选
MyMail.To = “to@domain.com” ’收信人的信箱地址,必选
MyMail.Subject = “您好!” ’邮件主题,必选
’邮件内容,必选,可以用 Request.Form 方法获取表单提交数据作为邮件内容
MyMail.Body = “希望交流一下”
MyMail.MailFormat = 1 ’邮件的格式,可选,0是MIME格式,1是文本消息
’邮件的正文的格式,可选,0表示HTML格式,1表示普通文本
MyMail.BodyFormat = 0
’邮件的重要性,可选,0表示低重要性,1表示普通,2表示高重要
MyMail.Importance = 1
’附件绝对路径,可选,可以用 Request.Form 方法获取表单提交数据作为附件路径
MyMail.AttachFile (“D:\myphotos\1.jpg”)
MyMail.Send
Set MyMail = Nothing
%>



方法二:通过 Jmail 组件发送邮件。  

<%
’创建 Jmail 对象
Set MyMail = Server.CreateObject(“JMAIL.SMTPMail”)
MyMail.silent = true’JMAIL不会抛出意外错误,返回值为 false 或 true
MyMail.logging = true ’启用使用日志
MyMail.Charset = “GB2312″ ’设置邮件文字编码为简体中文
MyMail.ContentType = “text/html” ’设置邮件的内容类型为 HTML
MyMail.ServerAddress = “mail.domain.com” ’发送邮件的服务器
MyMail.AddRecipient “to@domain.com” ’邮件接收人的 Email 地址
MyMail.SenderName = “Xmercy” ’邮件发送者的姓名
MyMail.Sender = “from@domain.com” ’邮件发送者的 Email 地址
MyMail.Priority = 3 ’邮件的紧急程序,1 最快,5 最慢, 默认为 3
MyMail.Subject = “您好!” ’邮件的主题
MyMail.Body = “希望交流一下。” ’邮件的内容
MyMail.AddRecipientBCC “bcc@domain.com” ’密件收件人的 Email 地址
MyMail.AddRecipientCC “cc@domain.com” ’邮件抄送者的 Email 地址
MyMail.Execute() ’发送邮件
MyMail.Close
%>



注意:使用CDONTS或Jmail组件前必须先确认服务器是否支持该组件。
特别提示
     如果服务器不支持发邮件的组件,将显示Server.CreateObject失败的错误信息,如图4.3.4所示。利用这个特点,也可以用这个方法来判断服务器是否支持该组件。

图4.3.4 在不支持Jmail组件的服务器上发送邮件


特别说明

   关于CDONTS和Jmail组件和方法和属性,可以参考两个方法的代码中的注释。

相关日志

标签Tags:, ,
Leave a comment

0 Comments.

Leave a Reply


[ Ctrl + Enter ]