2013-05-17 33 views
0

我有一個表單驗證表單字段與JavaScript和發送電子郵件與VBScript。如何驗證工作正常,但電子郵件沒有發送到電子郵件帳戶。如何驗證表單和發送電子郵件

VBScript來發送電子郵件:

<% 
posted = request.form ("submit") 
if posted = "Submit" then 

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
'' Customize the following 5 lines with your own information. '' 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
vtoaddress = "___________" ' Change this to the email address you will be receiving your notices. 
vmailhost = "smtp.gmail.com" ' Change this to your actual Domain name. 
vfromaddress = "___________" ' Change this to the email address you will use to send and authenticate with. 
vfrompwd =  "___________" ' Change this to the above email addresses password. 

''''''''''''''''''''''''''''''''''''''''''' 
'' DO NOT CHANGE ANYTHING PAST THIS LINE '' 
''''''''''''''''''''''''''''''''''''''''''' 
vsubject = request.form ("subject") 
vfromname = request.form ("fname") 
vbody = request.form ("message") 
vrplyto = request.form ("email") 
vrcity = request.form ("city") 
vrmono = request.form ("phone") 
vmsgbody = "<b>Name:</b> "& vfromname & "<br><b>Email:</b> "& vrplyto &"<br><b>Mobile No:</b> "& vrmono &"<br><b>City:</b> "& vrcity &"<br><b>Subject:</b> "& vsubject&"<br><b>Message:</b> "& vbody 

Set objEmail = Server.CreateObject("CDO.Message") 

objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = vmailhost 
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465 
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1 
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = vfromaddress 
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = vfrompwd 
objEmail.Configuration.Fields.Update 

objEmail.Subject = vsubject 
objEmail.From = vfromaddress 
objEmail.To=vfromaddress 
objEmail.HTMLBody = vmsgbody 
objEmail.Send 
    vErr = Err.Description 
    if vErr <> "" then 
     response.write vErr & "<br><br>There was an error on this page." 
     'MsgBox("There was an error on this page.") 
    else 
     response.write "Thank you, your message has been sent." 
     'MsgBox("Thank you, your message has been sent.") 
    End If 
Set objEmail = Nothing 
response.write "Thank you, your message has been sent." 
'MsgBox("Thank you, your message has been sent.") 
end if 
%> 

Javascript來驗證形式:

<script language="JavaScript"> 
<!-- 
    function validate() 
    { 
     var count_bug=0;  
     if(document.form1.fname.value=="") 
     { 
      document.getElementById("alertMsgfname").innerHTML="&nbsp;Enter Your First Name.&nbsp;"; 
      document.getElementById("alertMsgfname").style.visibility="visible"; 
      if(eval(count_bug)==0) 
       document.form1.fname.focus(); 
      count_bug+=1; 
     } 
     if(document.form1.email.value=="") 
     { 
      document.getElementById("alertMsgemail").innerHTML="&nbsp;Enter Your E-mail.&nbsp;"; 
      document.getElementById("alertMsgemail").style.visibility="visible"; 
      if(eval(count_bug)==0) 
       document.form1.email.focus(); 
      count_bug+=1; 
     } 
     else if(!isEmail(document.form1.email.value)) 
      {  
      document.getElementById("alertMsgemail").innerHTML="&nbsp;Enter Valid E-mail!&nbsp;"; 
      document.getElementById("alertMsgemail").style.visibility="visible"; 
      if(eval(count_bug)==0) 
       document.form1.email.focus(); 
      count_bug+=1; 
      } 
     if(document.form1.phone.value=="") 
     { 
      document.getElementById("alertMsgphone").innerHTML="&nbsp;Your Phone No.&nbsp;"; 
      document.getElementById("alertMsgphone").style.visibility="visible"; 
      if(eval(count_bug)==0) 
       document.form1.phone.focus(); 
      count_bug+=1; 
     } 

     else if(!ValidateNo(document.form1.phone.value," 1234567890,-/+")) 
     { 
      document.getElementById("alertMsgphone").innerHTML="&nbsp;Invalid Phone No.&nbsp;"; 
      document.getElementById("alertMsgphone").style.visibility="visible"; 
      if(eval(count_bug)==0) 
       document.form1.phone.focus(); 
      count_bug+=1; 
     } 
     else if(document.form1.phone.value.length < 5) 
     { 
      document.getElementById("alertMsgphone").innerHTML="&nbsp;Invalid Phone No.&nbsp;"; 
      document.getElementById("alertMsgphone").style.visibility="visible"; 
      if(eval(count_bug)==0) 
       document.form1.phone.focus(); 
      count_bug+=1; 
     } 
     if(document.form1.city.value=="") 
     { 
      document.getElementById("alertMsgCity").innerHTML="&nbsp;Enter Your City Name.&nbsp;"; 
      document.getElementById("alertMsgCity").style.visibility="visible"; 
      if(eval(count_bug)==0) 
       document.form1.city.focus(); 
      count_bug+=1; 
     } 
     if(document.form1.subject.value=="") 
     { 
      document.getElementById("alertMsgSubject").innerHTML="&nbsp;Enter Your Subject.&nbsp;"; 
      document.getElementById("alertMsgSubject").style.visibility="visible"; 
      if(eval(count_bug)==0) 
       document.form1.subject.focus(); 
      count_bug+=1; 
     } 
     if(document.form1.message.value=="") 
     { 
      document.getElementById("alertMsgMessage").innerHTML="&nbsp;Enter Your Message.&nbsp;"; 
      document.getElementById("alertMsgMessage").style.visibility="visible"; 
      if(eval(count_bug)==0) 
       document.form1.message.focus(); 
      count_bug+=1; 
     } 
    if(count_bug>0) 
     return false; 
    else 
     return true; 
    } 
function isEmail (emailIn){ 
    var isEmailOk = false; 
    var filter = /^[a-zA-Z0-9][a-zA-Z0-9._-]*\@[a-zA-Z0-9-]+(\.[a-zA-Z][a-zA-Z-]+)+$/ 
    // var filter = /^(([^<>()[\]\\.,;:\[email protected]\」]+(\.[^<>()[\]\\.,;:\[email protected]\」]+)*)|(\」.+\」))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 

    if(emailIn.search(filter) != -1) 
     isEmailOk = true; 
    if(emailIn.indexOf("..") != -1) 
     isEmailOk = false; 
    if(emailIn.indexOf("[email protected]") != -1) 
     isEmailOk = false; 

    return isEmailOk; 
} 
function ValidateNo(NumStr, String) 
{ 
    for(var Idx = 0; Idx < NumStr.length; Idx ++) 
    { 
     var Char = NumStr.charAt(Idx); 
     var Match = false; 
     for(var Idx1 = 0; Idx1 < String.length; Idx1 ++) 
     { 
      if(Char == String.charAt(Idx1)) 
      Match = true; 
     } 
     if (!Match) 
     return false; 
    } 
    return true; 
} 
    </script> 

HTML形式的代碼:

<form name="form1" method="post" onsubmit="return validate();"> 
      <table border="0" width="100%" cellspacing="0" cellpadding="0" class="table-format"> 
      <tr valign="middle"> 
      <td align="left" class="text-fm" width="23%" style="padding-top:8px;"> 
      <b>Name<font color="#C70017">*</font></b></td> 
      <td align="left" class="text-fm" width="5%"> 
      <b>:</b></td> 
      <td class="text-fm" align="left"> 
       <input type="text" name="fname" id="fname" size="43" maxlength="40" class="inp"> 
       <span id="alertMsgfname" class="valfrm" style='line-height:8px;'></span></td> 
      </tr> 
     <tr valign="middle"> 
      <td align="left" class="text-fm" style="padding-top:8px;"> 
      <b>Email Id<font color="#C70017">*</font></b></td> 
      <td align="left" class="text-fm"> 
      <b>:</b></td> 
      <td class="text-fm" align="left"> 
      <input type="text" name="email" id="email" size="43" maxlength="76" class="inp"> 
      <span id="alertMsgemail" class="valfrm" style='line-height:8px;'></span></td> 
      </tr> 
     <tr valign="middle"> 
       <td align="left" class="text-fm" style="padding-top:8px;"> 
       <b>Mobile No.<font color="#C70017">*</font></b></td> 
       <td align="left" class="text-fm"> 
       <b>:</b></td> 
       <td class="text-fm" align="left"> 
        <input type="text" name="phone" id="phone" size="43" maxlength="16" class="inp"> 
        <span id="alertMsgphone" class="valfrm" style='line-height:8px;'></span></td> 
       </tr> 
       <tr valign="middle"> 
       <td align="left" class="text-fm" style="padding-top:8px;"> 
       <b>City<font color="#C70017">*</font></b></td> 
       <td align="left" class="text-fm"> 
       <b>:</b></td> 
       <td class="text-fm" align="left"> 
       <input type="text" name="city" id="city" size="43" maxlength="76" class="inp"> 
       <span id="alertMsgCity" class="valfrm" style='line-height:8px;'></span></td> 
       </tr> 
       <tr valign="middle"> 
       <td align="left" class="text-fm" style="padding-top:8px;"> 
       <b>Subject<font color="#C70017">*</font></b></td> 
       <td align="left" class="text-fm"> 
       <b>:</b></td> 
       <td class="text-fm" align="left"> 
        <input type="text" name="subject" id="subject" size="43" maxlength="76" class="inp"> 
        <span id="alertMsgSubject" class="valfrm" style='line-height:8px;'></span></td> 
       </tr> 
       <tr valign="middle"> 
       <td align="left" class="text-fm" style="padding-top:8px;"> 
       <b>Message<font color="#C70017">*</font></b></td> 
       <td align="left" class="text-fm"> 
       <b>:</b></td> 
       <td class="text-fm" align="left"> 
       <textarea name="message" id="message" size="43" maxlength="16" class="inp" rows="6" cols="30"></textarea> 
       <span id="alertMsgMessage" class="valfrm" style='line-height:8px;'></span></td> 
       </tr> 
       <tr> 
      <td align="left" colspan="3"> 
       <div align="center"><br> 
       <input type="submit" value="" name="Submit" class="imgClass"/> 
       <br> 
       </td> 
      </tr> 
       </table> 
      </form> 
+0

所以你配置了你的smtp細節?請修改您的帖子以僅顯示相關代碼 – 2013-05-17 10:42:13

+0

我無法輸入電子郵件地址和密碼。 –

回答

0

缺失錯誤描述並不意味着不存在的錯誤。你的錯誤處理代碼改成這樣:

If Err Then 
    response.write Err.Number & "<br><br>There was an error on this page." 
Else 
    response.write "Thank you, your message has been sent." 
End If 

您可以從您的網絡服務器連接到郵件服務器上的端口465?

telnet smtp.gmail.com 465 

您使用的憑據是否正確?

你有權訪問郵件服務器日誌嗎?他們對這個網頁的連接有什麼評論?如果您無法訪問實際的郵件服務器,是否可以設置虛擬服務器並將該頁面臨時指向該服務器,以便您可以檢查代碼是否在原則上工作?

+0

雅我可以連接到這個端口沒有 –

+0

我使用正確的憑據。與安全性,我沒有把代碼。當我使用簡單的表單而不使用onsubmit =「return validate();」時,電子郵件已成功發送到發件人地址和JavaScript。 –

+0

然後我試着簡化JavaScript代碼。當你改變驗證函數爲'function validate(){return true; }'? –