发送邮件而不回发 [英] sending mail without postback
问题描述
我正在寻找使用-Jquery / asp.net发送邮件而不发回邮件的例子。
情况如下:
如果我使用常规方法页面发回服务器,联系我们表单位于长站点(一个页面站点)的底部然后用户不知道邮件是否被发送..
我的目标是:
发送邮件,然后向用户显示一个手势标志,指示邮件是否已发送
这就是我带来的:(它不工作)有人可以指导我如何解决它吗?
我也尝试用.ashx文件而不是WebMethod方法没有运气,有下降对于那种方法?(ashx)
JS:
I am looking for example to send mail without post back using- Jquery/asp.net.
The scenario is this:
The "contact us" form is located at the bottom of a long site(one page site) if I use the regular approach the page is posting back to the server and then the user is not aware if the mail was sent or not..
My goal is:
sending the mail and then show the user a gesture sign indicating if the mail was sent or not
this is what I came with:(its not working) can someone please direct me how to solve it?
I also tried with .ashx file instead of WebMethod approach with no luck, there are downs for that approach?(ashx)
JS:
$('#btn_contactForm').on('click', function (e) {
e.preventDefault();
var formData = $('#form_contact').serialize();
$.ajax({
type: "POST",
data:formData,
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "<%=siteUrl %>Index.aspx/SendMail",
success: function (data) {
alert(data.d);
},
error: function (data) {
alert(data.respnseText);
}
});
});
服务器端:(index.aspx.cs )
- 我不知道从ajax帖子中检索参数数据
Server side:(index.aspx.cs)
- I don't know hot to retrieve the parameter data from the ajax post
[WebMethod]
public Boolean SendMail()
{
string firstName = ....;
string lastName = ....;
string bName = ....;
string phone =....;
string senderEmail = ....;
string message = ....;
string eBody = "<div style='direction:rtl;'><b>mail sent from: </b>" + firstName + " " + lastName + "<br />";
eBody += "<b>bName: </b>" + bName + "<br />";
eBody += "<b>mail: </b>" + senderEmail + "<br />";
eBody += "<b>phone number: </b>" + phone + "<br />";
eBody += "<div style='width:300px;'><b>message: </b>" + message + "</div></div>";
MailMessage MyMailMessage = new MailMessage("***@gmail.com", "***@gmail.com", "message", eBody);
MyMailMessage.IsBodyHtml = true;
try
{
SmtpClient SMTPServer = new SmtpClient();
SMTPServer.Send(MyMailMessage);
return true;
//Response.Redirect("Thankyou.aspx");
}
catch (Exception ex)
{
return false;
}
}
Rechousa-添加了一个示例(html-文件)在他的解决方案的评论中。
推荐答案
(' #btn_contactForm')。on(' click', function (e){
e.preventDefault();
var formData =
('#btn_contactForm').on('click', function (e) { e.preventDefault(); var formData =
(' #form_contact')。serialize() ;
('#form_contact').serialize();
.ajax({
type: POST,
data:formData,
contentType: application / json; charset = utf -8,
dataType: json,
url: <%= siteUrl%> Index.aspx / SendMail,
成功:功能(数据){
alert(data.d);
},
错误: function (data){
alert(data.respnseText);
}
});
});
.ajax({ type: "POST", data:formData, contentType: "application/json; charset=utf-8", dataType: "json", url: "<%=siteUrl %>Index.aspx/SendMail", success: function (data) { alert(data.d); }, error: function (data) { alert(data.respnseText); } }); });
服务器端:(index.aspx.cs )
- 我不知道从ajax帖子中检索参数数据
Server side:(index.aspx.cs)
- I don't know hot to retrieve the parameter data from the ajax post
[WebMethod]
public Boolean SendMail()
{
string firstName = ....;
string lastName = ....;
string bName = ....;
string phone =....;
string senderEmail = ....;
string message = ....;
string eBody = "<div style='direction:rtl;'><b>mail sent from: </b>" + firstName + " " + lastName + "<br />";
eBody += "<b>bName: </b>" + bName + "<br />";
eBody += "<b>mail: </b>" + senderEmail + "<br />";
eBody += "<b>phone number: </b>" + phone + "<br />";
eBody += "<div style='width:300px;'><b>message: </b>" + message + "</div></div>";
MailMessage MyMailMessage = new MailMessage("***@gmail.com", "***@gmail.com", "message", eBody);
MyMailMessage.IsBodyHtml = true;
try
{
SmtpClient SMTPServer = new SmtpClient();
SMTPServer.Send(MyMailMessage);
return true;
//Response.Redirect("Thankyou.aspx");
}
catch (Exception ex)
{
return false;
}
}
Rechousa-添加了一个示例(html-文件)在他的解决方案的评论中。
这篇关于发送邮件而不回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!