Description

Before going through this article ensure that you have a basic understanding of the MVC Architecture, ASP.NET Web API, and jQuery.

Create one WEB API application as in the following figure 1 and 2,


Figure 1

Figure 2

Create a Model class:

In my case I named it in email.

Code in Email.cs as shown below:

  1. public class Email
  2. {
  3. public string cc { get; set; }
  4. public string Bcc { get; set; }
  5. public string To { get; set; }
  6. public string Body { get; set; }
  7. public string Subject { get; set; }
  8. }

Create Controller

Right click on the controller folder and create an empty controller as in the following figure three; in my case I named it EmailController.

Figure 3

Code in EmailController.cs as shown below:

  1. [RoutePrefix("api/Email")]
  2. public class EmailController : ApiController
  3. {
  4. [Route("Send")]
  5. [HttpPost]
  6. public HttpResponseMessage SendEmail(Email e)
  7. {
  8. try
  9. {
  10. using (MailMessage mail = new MailMessage())
  11. {
  12. string[] Tolist = e.To.Split(',');
  13. foreach (string Toemail in Tolist)
  14. {
  15. mail.To.Add(new MailAddress(Toemail));
  16. }
  17. string[] Tocc = e.cc.Split(',');
  18. foreach (string cc in Tocc)
  19. {
  20. mail.CC.Add(cc);
  21. }
  22. string[] ToBcc = e.Bcc.Split(',');
  23. foreach (string Bcc in ToBcc)
  24. {
  25. mail.Bcc.Add(Bcc);
  26. }
  27. mail.Subject = e.Subject;
  28. mail.Body = HttpUtility.HtmlDecode(e.Body);
  29. mail.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
  30. mail.IsBodyHtml = true;
  31. SmtpClient smtp = new SmtpClient();
  32. smtp.Host = ConfigurationManager.AppSettings["Host"];
  33. smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
  34. System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
  35. NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
  36. NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
  37. smtp.UseDefaultCredentials = true;
  38. smtp.Credentials = NetworkCred;
  39. smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
  40. smtp.Send(mail);
  41. }
  42. return Request.CreateResponse(HttpStatusCode.OK, "Email Send", Configuration.Formatters.JsonFormatter);
  43. }
  44. catch (Exception ex)
  45. {
  46. return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);
  47. }
  48. }
  49. }

The above controller will return the simple ”Email Send” as a response after successful execution of the Send action.

The web.config settings.

  1. <appSettings>
  2. <addkeyaddkey="Host"value="smtp.gmail.com"/>
  3. <addkeyaddkey="EnableSsl"value="true"/>
  4. <addkeyaddkey="UserName"value="[email protected]"/>
  5. <addkeyaddkey="Password"value="xxxxx"/>
  6. <addkeyaddkey="Port"value="587"/>
  7. </appSettings>

Create a new html page:

In my case I named it KendoEditorEmail.html.

Please click here to get more detail about Kendo Editor.

Code in KendoEditorEmail.html.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Untitled</title>
  6. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.common.min.css">
  7. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.default.min.css">
  8. <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  9. <script src="http://kendo.cdn.telerik.com/2016.1.112/js/angular.min.js"></script>
  10. <script src="http://kendo.cdn.telerik.com/2016.1.112/js/jszip.min.js"></script>
  11. <script src="http://kendo.cdn.telerik.com/2016.1.112/js/kendo.all.min.js"></script>
  12. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
  13. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  14. </head>
  15. <body>
  16. <div class="container">
  17. <div class="row" id="example">
  18. <div class="form-group">
  19. <label for="email">Email address:</label>
  20. <input type="email" class="form-control" id="email" data-bind="value:To">
  21. </div>
  22. <div class="form-group">
  23. <label for="cc">CC:</label>
  24. <input type="email" class="form-control" id="cc" data-bind="value:cc">
  25. </div>
  26. <div class="form-group">
  27. <label for="cc">Bcc:</label>
  28. <input type="email" class="form-control" data-bind="value:Bcc">
  29. </div>
  30. <div class="form-group">
  31. <label for="cc">Subject:</label>
  32. <input type="text" class="form-control" data-bind="value:Subject">
  33. </div>
  34. <div class="demo-section k-content">
  35. <div>
  36. <h4>Enter some Message</h4>
  37. <textarea data-role="editor"
  38. data-tools="['bold',
  39. 'italic',
  40. 'underline',
  41. 'strikethrough',
  42. 'justifyLeft',
  43. 'justifyCenter',
  44. 'justifyRight',
  45. 'justifyFull']"
  46. data-bind="visible: isVisible,
  47. value: body"
  48. style="height: 200px;"></textarea>
  49. </div>
  50. </div>
  51. <br />
  52. <div>
  53. <button data-bind="click:sendmail" class="k-button">Send Message</button>
  54. </div>
  55. <script>
  56. $(document).ready(function () {
  57. var viewModel = kendo.observable({
  58. To: '',
  59. cc: '',
  60. Bcc: '',
  61. body: '',
  62. isVisible: true,
  63. Subject: '',
  64. sendmail: function (e) {
  65. // e.preventDefault();
  66. console.log('hello')
  67. console.log();
  68. $.ajax({
  69. type: "POST",
  70. async: false,
  71. url: "api/Email/Send",
  72. data: {
  73. To: viewModel.get("To"),
  74. cc: viewModel.get("cc"),
  75. Bcc: viewModel.get("Bcc"),
  76. Body: viewModel.get("body"),
  77. Subject: viewModel.get("Subject"),
  78. },
  79. success: function (response) {
  80. alert(response);
  81. },
  82. error: function () {
  83. alert("Inside ajax call error");
  84. },
  85. })
  86. }
  87. });
  88. kendo.bind($("#example"), viewModel);
  89. })
  90. </script>
  91. </div>
  92. </div>
  93. </body>
  94. </html>
Output

Figure 4

Figure 5
Clicking on Send Message will fire the Send Action.

Figure 6
The Message which is received as in the following figure seven.

Run
Figure 7
I hope you enjoyed this article. Your valuable feedback, question, or comments about this article are always welcomed.

Read more articles on ASP.NET Programming: