我正在寻找一种使用ajax和c#mvc从我的邮件列表中退订客户端的方法。
我试图在我的.js文件中使用$(document).ready(function()...函数)来获取隐藏在我的Index.cshtml文件的输入元素中的查询字符串值,该字符串将首先显示用户点击电子邮件中的“取消订阅”后,加载正确后,这会将用户记录更新为取消订阅。
Index.cshtml文件上的输入元素
<input type="hidden" value='@Request.QueryString["email"]' id="QryEmailAddress" />
.js文件中的Ajax代码
$(document).ready(function () {
var emailAddress = $("#QryEmailAddress").val();
if (emailAddress != null || emailAddress != "") {
$.ajax({
type: "POST",
url: '/Home/UnsubscribeClientEmail',
data: { emailAddress: emailAddress },
beforeSend: function () {
$('#LoadingBoxModal #showProgress').html();
$('#LoadingBoxModal .modal-dialog').css('width', '');
$('#LoadingBoxModal').appendTo('body');
$('#LoadingBoxModal').modal({
backdrop: 'static',
keyboard: false,
show: true
});
},
success: function (data) {
$("#LoadingBoxModal").modal('hide');
},
error: function (error) {
$("#LoadingBoxModal").modal('hide');
},
completed: function () {
$("#LoadingBoxModal").modal('hide');
}
});
}
else {
//If there's no value, then continue going opening a home page as usual...
}
});
如果上面代码中的emailAddress变量中包含一个值,它将进入我的HomeController.cs文件中的代码中。
public string UnsubscribeClientEmail(string email)
{
var _updateClientInfo = new HomeTransactions(new PetaConnection().db()).ClientSubscriptions(email);
return _updateClientInfo;
}
.home控制器中的ClientSubscriptions(email)引用以下代码。
public string ClientSubscriptions(string emailAddress)
{
var _getClientInfo = _connect.FirstOrDefault<ClientInfo>($"select * from ClientInfo where EmailAddress = '{emailAddress}'");
_getClientInfo.EmailSubscription = "No";
_getClientInfo.DateUpdated = DateTime.Now;
_connect.Update(_getClientInfo);
return "OK";
}
这是发送给客户端的电子邮件功能。
public void SendNotification(string emailAddress, string firstName, string referenceNo)
{
string adminName = "Company Name";
string adminAddress = "admin@domain.com";
string password = "Jibbirish";
string serverAddress = "aserv.domain.com";
try
{
SmtpMail smtpMail = new SmtpMail("TryIt");
SmtpClient smtpClient = new SmtpClient();
smtpMail.From = new MailAddress(adminName, adminAddress);
smtpMail.To = emailAddress;
smtpMail.Subject = "Subject";
smtpMail.Priority = MailPriority.High;
SmtpServer smtpServer = new SmtpServer(serverAddress);
smtpServer.User = adminAddress;
smtpServer.Password = password;
Attachment attachment = smtpMail.AddAttachment("C:\\images\\Logo_Full.jpg");
string contentId = "myLogo";
attachment.ContentID = contentId;
smtpMail.HtmlBody = "<html><body><a href='.../Home/Index?email=" + emailAddress + "'>Unsubscribe</a></body></html>";
SmtpClientAsyncResult asyncResult = smtpClient.BeginSendMail(smtpServer, smtpMail, null, null);
//Waiting to send email...
while (!asyncResult.IsCompleted)
{
asyncResult.AsyncWaitHandle.WaitOne(10, false);
}
smtpClient.EndSendMail(asyncResult);
}
catch (Exception ex)
{
throw;
}
}
当用户单击电子邮件中的“取消订阅”时,一旦他们登陆我们的网站,他们就会收到一条消息,告知他们结果...