如何使用我的后端

时间:2017-09-19 22:45:18

标签: javascript c# ajax wcf

我有发送电子邮件的课程方法

public static bool SendEmail(string msg)
    {
        var sent = false;
        try
        {
            var fromEmail = ConfigurationManager.AppSettings["FromEmail"];
            var toEmail = ConfigurationManager.AppSettings["ToEmail"];
            var emailsubject=ConfigurationManager.AppSettings["ErrorTitle"];

            var emailService = new EmailServiceClient();
            sent = emailService.SendEmail(fromEmail, toEmail, emailsubject, msg);
        }
        catch (Exception ex)
        {
            LogException(ex);
        }
        return sent;
    }

这是我的Javascript函数on on button Click()event

             function sendMail() {
       var e= "Hi";

        $.ajax({
            url: '/Home/SendEmail',
            method: "POST",  

        success:function (result) {
            alert('Email Sent');
        },
        error: function (result) {
            alert('Email Does Not Sent');
        }
        });

    }

此JS代码正在打开我系统的Outlook电子邮件(打开我的邮件客户端)。 我想直接通过EmailService从我的网页点击按钮发送电子邮件(不是通过SMTP,因为我为EmailService创建了WCF),我不知道如何实现它的流程。

需要了解JS代码中的更改,任何文档或示例以供参考都会有所帮助。 谢谢

2 个答案:

答案 0 :(得分:1)

它适用于此:

 public ActionResult SendEmail(string msg)
    {
        var sent = false;
        try
        {

            var emailClient = new EmailServiceReference.EmailServiceClient();
            sent = emailClient.SendEmail(fromEmail, toEmail, emailsubject, msg);  /// All parameters of SendEmail method defined

        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception occured while sending Email " + ex.Message);
        }
        return Json(sent, JsonRequestBehavior.AllowGet);
    }

答案 1 :(得分:0)

我建议你阅读jquery以及如何使用jquery ajax,下面的代码是一个很好的开始,你没有提到你是否使用MVC或API或WebForms,所以下面的代码有点通用。 (this is a complete guide that you can follow

function SendMail(){
var msg = 'your msg';

$.ajax({
                url: 'YOURURLHERE/SendEmail?msg=' + msg,
                method: "GET",               
            }).done(function (result) {
                alert('Email Sent');
                });
            });
}