Search code examples
javascriptmailtowindow.locationactionmethod

How to fire mailto urls from action method


I am a beginner in MVC. I want to develop an action method in MVC which fires Mailto:?body=body goes here.&subject=test subject and so the default mail client will automatically populate the email for the user. Right now I have List<String> which contain mailto: urls.

Please, if you have any experience or demo code it will be helpful to me. Thanks in advance.


Solution

  • My ActionMethod

    [HttpPost]
    public JsonResult emailTemplate()
    {
        List<String> str = new List<String>();
        str.Add("Mailto:?body=Hello1&subject=test subject1");
        str.Add("Mailto:?body=Hello2&subject=test subject2");
        return Json(str);
    }
    

    JavaScript Function in view

    function SendMailClicked() {
    
            $.ajax({
                type: "POST",
                url: "/Home/emailTemplate",
                //data: "{'ReviewComponentIds':'1,2,3'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    jQuery.each(response, function () {
    
                        window.location.href = this + '\n';
                    });
                },
                failure: function (errMsg) {
                    alert('failure');
                }
            });
    
        }