感谢您抽出时间来寻找/帮助我。
我正在寻找使用-Jquery / asp.net发送邮件而不发回邮件的例子。
情况如下:
“联系我们”表单位于长站点的底部(一页 网站)如果我使用常规方法页面发回到 服务器,然后用户不知道邮件是否已发送..
我的目标是:
发送邮件,然后向用户显示指示是否的手势标志 邮件是否已发送
有人可以指导我这样的例子(我试图找到但没有运气)
这就是我的意思:(它不起作用) 有人可以指导我如何解决它?
我也尝试使用.ashx文件而不是WebMethod方法而没有运气,这种方法有不足之处?(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)
[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;
}
}
答案 0 :(得分:2)
假设你有正常的SendEmail方法,你只需要一个WebMethod:
[WebMethod]
public static Boolean SendEMail()
{
//this is my actual email helper I tested it with
return BLL.MailHelper.sendEmail("youremail@server.com", "Hello, World", "Subject");
}
然后对此页面执行ajax POST:
来自HTML的:
<script src="js/jquery-1.9.1.js"></script>
<script>
$(function () {
$.ajax({
type: "POST",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "SampleMail.aspx/SendEMail",
dataType: "json",
success: function (data) {
alert(data.d);
}
});
});
</script>
如果电子邮件发送成功与否,我的BLL.MailHelper.sendEmail()
方法会返回一个布尔值,并且您将在成功回调函数的jSon中获得相同的结果,您可以使用该函数通知用户该电子邮件是否为发送与否。
请注意,SampleMail.aspx是页面的名称,SendEMail是Method名称。 data参数为空,但你可以在这里向你的方法发送额外的参数,虽然我不推荐它。
答案 1 :(得分:0)
我使用处理程序(而不是Web服务)发送邮件,请参阅讨论here:
<强> JS:强>
//Click on the submit button of the- Contact Form
$('#btn_contactForm').on('click', function (e) {
e.preventDefault();
var settings = {
'data': getData(),
'url': "Handlers/SendMail.ashx",
'contentType': 'application/x-www-form-urlencoded; charset=UTF-8'
};
sendEmail(settings);
});
function getData() {
var data = {
'firstName': $('#txt_fName').val(),
'lastName': $('#txt_lName').val(),
'phone': $('#txt_phone').val(),
'bName': $('#txt_bName').val(),
'fromMail': $('#txt_email').val(),
'Message': $('#txt_message').val()
};
return data;
}
function sendEmail(settings) {
var success = false;
$.ajax({
type: "POST",
contentType: settings.contentType,
data: settings.data,
url: settings.url,
dataType: "json",
async: false,
success: function (data) {
// Return True from handler
//console.log(data);
alert("success")
success = true;
},
error: function (data) {
// Return False from handler
// console.log(data);
alert("fail")
success = false;
}
});
return success;
}
<强> SendMail.ashx:强>
public void ProcessRequest(HttpContext context)
{
// Loads parameters into variables
string firstName = context.Request.Form.Get("firstName");
string lastName = context.Request.Form.Get("lastName");
string phone = context.Request.Form.Get("phone");
string bName = context.Request.Form.Get("bName");
string senderEmail = context.Request.Form.Get("fromMail");
string message = context.Request.Form.Get("message");
//Used a class for sending the mail- in App_Code folder
//The method(SendEmail) in that class returns boolean True- success, False- failure
bool MailSent = Mail.SendEmail(firstName, lastName, bName, phone, senderEmail, message);
context.Response.ContentType = "text/plain";
if(MailSent)
{
context.Response.Write("true");
}else
{
context.Response.Write("false");
}
}
Mail.cs - Mail.SendEmail()
public static bool SendEmail(string firstName, string lastName, string bName, string phone, string senderEmail, string message)
{
// Email sending
string eBody = "<div style='direction:rtl;'><b>sent from </b>" + firstName + " " + lastName + "<br />";
eBody += "<b>bName: </b>" + bName + "<br />";
eBody += "<b>email: </b>" + senderEmail + "<br />";
eBody += "<b>phone: </b>" + phone + "<br />";
eBody += "<div style='width:300px;'><b>message: </b>" + message + "</div></div>";
MailMessage MyMailMessage = new MailMessage("***@gmail.com", "MailMessage("***@gmail.com", "subject", eBody);
MyMailMessage.IsBodyHtml = true;
try
{
SmtpClient SMTPServer = new SmtpClient();
SMTPServer.Send(MyMailMessage);
return true;
}
catch
{
return false;
}
}