-1

I have an asp page which should send a mail . I am using the below code, but it returns an error in sending

<form action="contactus.asp" method="post">
            <div style="float:left; padding-left:10px; font-family:Tahoma; font-size:14px; color:green; width:150px;">Name:</div>
            <input type="text" style="width:250px; height:20px;" />
            <div style="float:left; padding-left:10px; font-family:Tahoma; font-size:14px; color:green; width:150px;">E-mail Address:</div>
            <input type="text" name="from_mail" style="width:250px; height:20px;" />
            <div style="float:left; padding-left:10px; font-family:Tahoma; font-size:14px; color:green; width:150px;">Phone Number:</div>
            <input type="text" style="width:250px; height:20px;" />
            <div style="float:left; padding-left:10px; font-family:Tahoma; font-size:14px; color:green; width:150px;">Message:</div>
            <input type="text" name="message" style="width:250px; height:20px;" /> 
            <div style="float:left; padding-left:10px; font-family:Tahoma; font-size:14px; color:green; width:150px;">
                <input type="submit" value="Send" style="font-family:Tahoma; font-size:14px; color:green; width:80px; height:20px; font-weight:bold;" />
    </form>
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.TextBody="This is a message."
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2'Name or IP of remote SMTP server
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.server.com"'Server port
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 
myMail.Configuration.Fields.Update
myMail.Send
set myMail=nothing
%>
user3261588
  • 51
  • 1
  • 9
  • What error message are you getting? – John Mar 31 '14 at 19:29
  • error '8004020e'/contactus.asp, line 164 which is the line os sending mail – user3261588 Mar 31 '14 at 21:34
  • When i load the page , i see this error in the bottom of the page CDO.Message.1 error '8004020d'At least one of the From or Sender fields is required, and neither was found./contactus.asp, line 164 – user3261588 Mar 31 '14 at 21:35
  • Hmm, you certainly do have a from field, and your code looks like it cones from the W3Schools tutorial, so it's certainly tried and tested. Is it possible you have a typo in your MyMail.From line? – John Mar 31 '14 at 22:38
  • I'm assuming you've changed the `smtp.server.com` to a real smtp server that you have access too? Also, how are you authenticating to that server? – safetyOtter Apr 01 '14 at 03:20
  • Ok, how can i put this code in a function and call it on submit of form as i have made a form element – user3261588 Apr 01 '14 at 07:45
  • i have updated my code to show you my form. but when i load the error appear before posting the form and when i enter my data the error appears again. any help – user3261588 Apr 01 '14 at 11:01
  • Use form variables for your fields, eg `myMail.From=Request.Form("from_mail")` to link it to your form. No need for a function – John Apr 01 '14 at 12:15

1 Answers1

0

This should show you how to use form variables in your script. The asp goes before your form, and I've added a conditional statement so that the page doesn't try to execute the code unless the Send button has been pressed. Note that I've given the send button a name attribute

<%  If request.form("Submitbutton") <>"" Then
    Set myMail=CreateObject("CDO.Message")
    myMail.Subject="Sending email with CDO"
    myMail.From=Request.Form("from_mail")
    myMail.To="someone@somedomain.com"
    myMail.TextBody=Request.Form("message")
    myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
    myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.server.com"
    myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 
    myMail.Configuration.Fields.Update
    myMail.Send
    set myMail=nothing
    End If
    %>    

<form action="contactus.asp" method="post">
                <div style="float:left; padding-left:10px; font-family:Tahoma; font-size:14px; color:green; width:150px;">Name:</div>
                <input type="text" style="width:250px; height:20px;" />
                <div style="float:left; padding-left:10px; font-family:Tahoma; font-size:14px; color:green; width:150px;">E-mail Address:</div>
                <input type="text" name="from_mail" style="width:250px; height:20px;" />
                <div style="float:left; padding-left:10px; font-family:Tahoma; font-size:14px; color:green; width:150px;">Phone Number:</div>
                <input type="text" style="width:250px; height:20px;" />
                <div style="float:left; padding-left:10px; font-family:Tahoma; font-size:14px; color:green; width:150px;">Message:</div>
                <input type="text" name="message" style="width:250px; height:20px;" /> 
                <div style="float:left; padding-left:10px; font-family:Tahoma; font-size:14px; color:green; width:150px;">
                    <input type="submit" name="Submitbutton" value="Send" style="font-family:Tahoma; font-size:14px; color:green; width:80px; height:20px; font-weight:bold;" />
        </form>
John
  • 4,658
  • 2
  • 14
  • 23
  • i updated my code according what you say, but after entering my data. this error appears "CDO.Message.1 error '80040213' The transport failed to connect to the server." – user3261588 Apr 01 '14 at 16:48
  • I refer you to safetyOtter's comment to your original question re authentication, and take a look at this - http://www.paulsadowski.com/wsh/cdo.htm – John Apr 01 '14 at 18:40
  • No ajax. Classic asp is much older than ajax (in web terms). "The transport failed to connect to the server" usually means that CDO looked for an SMTP server and didn't find one – John Apr 03 '14 at 09:25