2012-08-06 28 views
1

首先,我對asp.net,jquery和所有網絡端的東西都是全新的。我對此沒有很好的理解。我試圖做一些研究,但我無法弄清楚我的問題。試圖用ASP.net發送電子郵件,不能將變量傳遞給ashx文件

我買了一個模板,現在正試圖通過這個破碎的代碼,使其功能。

我有一個接觸的形式,字段是這樣的:

<label class="name"> 
    <input type="text" value="Name:"> 
    <span class="error">*This is not a valid name.</span> 
     <span class="empty">*This field is required.</span> 
    <span class="clear"></span> 
</label> 

有3個或4個字段都是相同的,除了一個是一個textarea。

現在我想我的問題是,我沒有從文本字段中獲取值。無論是或我沒有正確發送值。

這是我的提交功能:

submitFu: function() { 
    _.validateFu(_.labels) 
    if (!_.form.has('.' + _.invalidCl).length) 
     $.ajax({ 
       type: "POST", 
       url: _.mailHandlerURL, 
       dataType: 'json', 
       data: { 
        'name': +_.getValFromLabel($('.name', _.form)), 
        'email': +_.getValFromLabel($('.email', _.form)), 
        'phone': _.getValFromLabel($('.phone', _.form)), 
        'message': +_.getValFromLabel($('.message', _.form)), 
        'from': '[email protected]', 
        'smtpMailServer': + _.smtpMailServer, 
        'stripHTML': 'false' 
        }, 
       success: function() { _.showFu() }, 
       error: function (data) { alert(data.responseText); } 
}) 

這是我的「getValFromLabel」功能,我真不明白:

getValFromLabel: function (label) { 
     var val = $('input,textarea', label).val() 
     , defVal = label.data('defVal') 
     return label.length ? val == defVal ? 'nope' : val : 'nope' 
} 

我把alert(+_.getValFromLabel($('.message', _.form)))我提交功能,它顯示「NaN」,這對我毫無意義。這就是爲什麼我認爲我沒有正確地獲得價值。聯繫表單返回它沒有發件人地址的錯誤。

任何想法???

+1

由於您使用的是ASP .NET,因此您始終可以使用C#或VB .NET而不是PHP發送電子郵件。 – 2012-08-06 12:50:05

+0

即時通訊在我的郵件處理程序文件中使用c#。雖然我不相信這個問題。參數從來沒有那麼遠。我需要知道我是否正確地從文本字段中獲取數據。 – CSharpDev 2012-08-06 12:54:26

+0

或者我不應該使用c#時做一個POST?我如何調用郵件處理程序文件? – CSharpDev 2012-08-06 12:56:09

回答

2

這裏是使用http://bassistance.de/jquery-plugins/jquery-plugin-validation/

<form method="post" class="cmxform" id="form" action="form.php"> 
    <fieldset> 
     <legend>Login Form (Enter "foobar" as password)</legend> 
     <p> 
      <label for="user">Name</label> 
      <input id="name" name="name" title="Please enter your name (at least 3 characters)" class="required" minlength="3" /> 
     </p> 
     <p> 
      <label for="email">Email</label> 
      <input id="email" name="email" title="Please enter a valid email address" class="required email"/> 
     </p> 
     <p> 
      <label for="phone">Phone</label> 
      <input id="phone" name="phone" title="Please enter a valid phone number" class="required digits" minlength="10" maxlength="15" /> 
     </p> 
     <p> 
      <label for="phone">comment</label> 
      <textarea id="comment" name="comment" title="Please enter a comment" class="required" rows="10" cols="50" ></textarea> 
     </p> 
     <p> 
      <input class="submit" type="submit" value="Submit"/> 
     </p> 
    </fieldset> 
</form> 

這裏基本代碼的JavaScript/jQuery代碼

jQuery("#form").validate({ 
    submitHandler: function() { 
     $.ajax({ 
      type: "POST", 
      url: "EMAIL.ashx",//or you can code behind web method/webservice 
      dataType: 'json', 
      data: { 
       'name': + $("#name").val(), 
       'email': + $("#email").val(), 
       'phone': $("#phone").val(), 
       'message': + $("#comment").val(), 
       'from': '[email protected]', 
       'smtpMailServer': + $("#smtpserver").val(), 
       'stripHTML': 'false' 
       }, 
      success: function() { alert("email sent successfully") }, 
      error: function (data) { alert(data.responseText); } 
     })    
    } 
}); 

你應該,因爲你已經在代碼/樣品上面貼的鏈接指向你的ashx代碼我不包括它,也不推薦smtp的細節,你可以從網上獲得你的csharp代碼的細節。config

1

嘗試使用ASP .NET Controls和C#發送電子郵件,如果您對Web語言不太滿意。

您的標記看起來是這樣的:

<asp:TextBox ID="Name" runat="server"></asp:TextBox> 
<asp:Button ID="SendButton" runat="server" Text="Send" /> 

顯然,這只是一個單一的文本框來容納一個「名稱」值,一鍵發送電子郵件。根據需要添加更多文本框。

然後,在SendButton_Click事件,請使用如下代碼:

string MailBody; 
MailMessage myMessage = new MailMessage(); 
SmtpClient smtpC = new SmtpClient(); 

myMessage.Subject = "Subject for Email"; 
myMessage.From = new MailAddress("[email protected]", "Your Name"); 
myMessage.To.Add(new MailAddress("[email protected]", "Receiver Name"); 

mailBody = Name.Text; //Get information from webpage 

smtpC.send(myMessage); //Send the email via smtp 

確保你把下面引用的C#文件的頂部:

using System.IO; 
using System.Net.Mail; 

此外,使用SMTP客戶端,你將不得不在你的web.config文件中添加一些配置,如下所示:

<system.net> 
    <mailSettings> 
    <smtp deliveryMethod="Network" from="[email protected]"> 
     <network enableSsl="true" userName="[email protected]" password="yourpassword" host="smtp.gmail.com"/> 
    </smtp> 
</mailSettings>