如何在“经典ASP”中编辑cdosys.asp来发送电子邮件?

时间:2011-04-24 21:02:45

标签: asp-classic

我的网站除了“联系”页面外都是静态内容。我的托管服务让我在IIS服务器上,所以他们给了我通用的cdosys.asp文件。如你所知,如果你使用了cdosys.asp,你可以从联系页面指向表单提交,cdosys.asp会向我发送电子邮件中的表单字段中的网站访问者,然后转发访问者到“感谢写作”页面。

这是我的故障。我学到的唯一服务器端语言是ColdFusion。没有ASP。 cdosys文件非常简单,可以看出我更改了​​一些文本字符串,因此它指向我的邮件服务器,然后指向感谢页面。但是,我的联系人输入表单只需要三个提供的字段中的一个。两个是可选的。但是cdosys对此感到窒息。它认为所有领域都是必需的。它只适用于所有三个都有内容的情况。我用谷歌搜索了一个旧的ASP教程。有些友好的ASP专业人士告诉我如何编辑下面的代码,让我的三个表单字段中的两个没有内容,仍然给我发电子邮件?

<%
    For Field = 1 to Request.Form.Count - 3
        FieldName = Replace(Request.Form.Key(Field),"_"," ")
        FieldValue = Request.Form.Item(Field)
        Body = Body & FieldName & ": " & FieldValue & VbCrLf
    Next

    'Dimension variables
    Dim objCDOSYSCon

    'Create the e-mail server object
    Set objCDOSYSMail = Server.CreateObject("CDO.Message")
    Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")

    'Set and update fields properties
    With objCDOSYSCon
        'Outgoing SMTP server
        .Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.domain.com" 'was "SMTPSERVER"
        .Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'CDO Port
        .Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Timeout
        .Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
        .Fields.Update
    End With

    'Update the CDOSYS Configuration
    Set objCDOSYSMail.Configuration = objCDOSYSCon

    'Set and update email properties
    With objCDOSYSMail
        '0=Low, 1=Normal, 2=High
        .Fields("urn:schemas:httpmail:importance").Value = 1
        'Who the e-mail is from
        .From = Request.Form("email_address")
        'Who the e-mail is sent to
        .To = "myname@domain.com"
        'Who the e-mail is CC'd to
        .Cc = ""
        'The subject of the e-mail
        .Subject = Request.Form("email_subject")
        'Set the e-mail body format (HTMLBody=HTML TextBody=Plain)
        .TextBody = Body
        .Fields.Update
        'Send the e-mail
        .Send
    End With

    'Close the server mail object
    Set objCDOSYSMail = Nothing
    Set objCDOSYSCon = Nothing

    'Rederect after sending email
    Response.Redirect Request.Form("redirect_to")
%>

1 个答案:

答案 0 :(得分:0)

试试这样:

For Field = 1 to Request.Form.Count - 3
FieldName = Replace(Request.Form.Key(Field),"_"," ")
FieldValue = Request.Form.Item(Field)
if isnull(fieldvalue) then fieldvalue = "(null)"
Body = Body & FieldName & ": " & FieldValue & VbCrLf
Next