如何在控制器中发送消息后弹出成功的消息,使用 C#、MVC

本文关键字:消息 成功 使用 MVC 控制器 | 更新日期: 2023-09-27 18:34:51

我正在使用smpt发送电子邮件。我可以成功发送电子邮件。我已经测试并收到了。现在我的问题是在控制器中完成电子邮件发送过程(到目前为止,只有成功也足够(后如何显示成功或错误消息。我在这里附加了我的代码,它在我的控制器中。

    [HttpPost]
    public async Task<ActionResult> GetEnquiryData(string FirstName, string LastName, string Email, string PhoneNumber, string TravelCalendar, string TravelNights, string TravelMonth, string TNoAdults, string TNoChildren, string SpecialOc, string GettoKnow, string TCUKMember, string Tdate, string Tprice, string byemail, string dealRef, string ConTime)
    {
        var firstName = FirstName;
        var lastName = LastName;
        var email = Email;
        var contactNumber = PhoneNumber;
        var travelCalendar = TravelCalendar;
        var travelNights = TravelNights;
        var travelMonth = TravelMonth;
        var TAdults = TNoAdults;
        var TChildren = TNoChildren;
        var SpecialOcc = SpecialOc;
        var GetToKnow = GettoKnow;
        var TcukMember = TCUKMember;
        var Date = Tdate;
        var Price = Tprice;
        var Mail_Call = byemail;
        var DealRef = dealRef;
        var CTime = ConTime;
        string subject = "";
        string body = "";

        //TCNewDBEntities EnquiryEntities = new TCNewDBEntities();
        //Enquiry enQuiryDetails = new Enquiry();
        //enQuiryDetails.FirstName = firstName;
        //enQuiryDetails.LastName = lastName;
        //enQuiryDetails.EmailAddress = email;

        //EnquiryEntities.Enquiries.Add(enQuiryDetails);
        //EnquiryEntities.SaveChanges();

        var fromAddress = "inone@Ubr.co.uk";
        subject = "Enquiry Form Details";
        body += "<br /><br /><b>" + Mail_Call + "</b>";
        body += "<br /><br />Deal Reference No." + DealRef;
        body += "<br /><br />First Name : " + firstName;
        body += "<br /><br />Last Name : " + lastName;
        body += "<br /><br />Email Address : " + email;
        body += "<br /><br />Contact Number : " + contactNumber;
        body += "<br /><br />Traveling From : " + travelCalendar;
        body += "<br /><br />Nights : " + travelNights;
        body += "<br /><br />Date : " + Date + " " + travelMonth;
        body += "<br /><br />Price : " + Price;
        body += "<br /><br />No. Adults : " + TAdults;
        body += "<br /><br />No. Children : " + TChildren;
        body += "<br /><br />Special Occasion : " + SpecialOc;
        body += "<br /><br />Know About Deals: " + GetToKnow;
        body += "<br /><br />Convenient Time:" + CTime;
        body += "<br /><br />Voucher Code: " + TcukMember;

        MailMessage mailP = new MailMessage();
        mailP.From = new MailAddress(fromAddress);
        mailP.To.Add(new MailAddress("abcd@gmail.com"));
        mailP.Bcc.Add(new MailAddress(email));
        mailP.Subject = "'" + subject + "'";
        mailP.Body = body;
        mailP.IsBodyHtml = true;
        using (var smtpClient = new SmtpClient())
        {
            var credential = new NetworkCredential
            {
                UserName = "smtpubr",
                Password = "12ubr34"
            };
            smtpClient.Credentials = credential;
            smtpClient.Host = "smtpttcorpeo.com";
            smtpClient.Port = 2322;
            smtpClient.EnableSsl = true;
            await smtpClient.SendMailAsync(mailP);
        }
        return Json(new { });
    }

希望您对此有所帮助。

如何在控制器中发送消息后弹出成功的消息,使用 C#、MVC

让你像这样返回对象示例

public class ServerResponse
{
public string ErrorMessage {get; set;}
public string SuccessMessage {get; set;}
}

然后

 public async Task<ActionResult> GetEnquiredData(string FirstName, string LastName, string Email, string PhoneNumber, string TravelCalendar, string TravelNights, string TravelMonth, string TNoAdults, string TNoChildren, string SpecialOc, string GettoKnow, string TCUKMember, string Tdate, string Tprice, string byemail, string dealRef, string ConTime)
 {
        var serverResponse = new ServerResponse();
        try { //Send Email here
        serverResponse.SuccessMessage = "Success!";
        }
        catch (Exception ex)
        {
        serverResponse.ErrorMessage= $"Error! + {ex.Message}";
        }
       return Json(serverResponse, JsonRequestBehaviour.AllowGet);
 }

之后通过 ajax 检查服务器响应的内容。并显示消息!希望对你有帮助

.js

$.ajax({
url : /YourURL/GetEnquiredData,
data : {your data}
}).success(function(response)
{ if (response.ErrorMessage!=null)
{ 
showError(response.ErrorMessage):
}
else {
showSucess(response.SuccessMessage);
}
});