日期:2011-01-08  浏览次数:20756 次

最后要实现的功能是邮件的编辑和发送。这部分功能由Email_List.asp文件提供,其界面如图6所示。接下来我们就来分析这个文件。

   Email_List.asp的内部工作过程和edit_record.asp很类似。管理员在表单中写作邮件并提交它,系统将选择所有Mail_List字段值为“是”的记录,然后将新邮件的拷贝发送给这些记录中的Guest_Mail地址。

   每一次发送邮件我们都重新创建mailer对象,发送完成后关闭它。这一点非常重要,这是由于我们需要修改邮件的正文,加入取消订阅邮件列表的URL和ID号。

 if Request.ServerVariables("REQUEST_METHOD") = "POST" then
  strSubject = Request.Form("txtSubject")
  strBody = Request.Form("txtBody")
  strFrom = Request.Form("txtFrom")
  ' 从数据库选取收件人记录
  strSQL_SelectEmail = "SELECT Guests.Guest_ID, Guests.Guest_Email " & _
  " FROM Guests WHERE ((Guests.Mail_List)=-1);"
  Set oConn = Server.CreateObject("ADODB.Connection")
  oConn.Open strDSNPath
  Set rsMail = oConn.Execute(strSQL_SelectEmail)
  if rsMail.BOF = True and rsMail.EOF = True then
  ...数据库为空提示,略...
  else
  rsMail.MoveFirst
  Do While Not rsMail.EOF
  ' 创建对象
  Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
  ' 填写其它邮件标题信息
  Mailer.FromName = strFrom
  Mailer.FromAddress = strEmailFrom
  Mailer.RemoteHost = strHost
  Mailer.Subject = strSubject
  Mailer.BodyText = ...设置邮件内容,略...
  strTo = rsMail.Fields("Guest_Email").Value
  If strTo < > "" then
  Mailer.Recipient = strTo
  if Mailer.SendMail then
  ...发送成功提示,略...
  else
  ...发送失败提示,略...
  end if 'Mailer.SendMail
  end if 'strTo < > ""
  rsMail.MoveNext
  Set Mailer = Nothing
  Loop
  end if 'rsMail.BOF = True and rsMail.EOF = True
  rsMail.Close
  Set rsMail = Nothing
  oConn.Close
  Set oConn = Nothing
 end if 'REQUEST_METHOD = "POST"

   这里需要注意的是,我们将变量strHost的值赋给ASPMail的实例对象的RemoteHost属性。因此,必须保证strHost的值是一个合适的邮件服务器名字(如mail.mydomain.com)。