SoFunction
Updated on 2025-03-08

C# simple implementation of sending emails on web pages

1. The front-end HTML uses Jquery. If you do a demonstration, don't forget to introduce Jquery's library.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">

<html xmlns="http:///1999/xhtml">
<head>
  <title></title>
  <script src="jquery-1.8." type="text/javascript"></script>
  <script type="text/javascript">
    function sendemail() {
      var smtp = $('#txtSmtp').val();
      var content = $('#txtContent').val();
      var title = $('#txtTitle').val();
      var from = $('#txtFrom').val();
      var to = $('#txtTo').val();
      $.post("", { 'smtp': smtp, 'content': content,'title':title, 'from': from, 'to': to },
    function (data) {
      var n = eval('(' + data + ')');
      if () {
        alert();
      }
    });
    
    }
  </script>
</head>
<body>
  <table>
    <tr><td>smtp:</td>
      <td><input type="text" id = "txtSmtp" value="Smtp Server" />
      </td>
    </tr>
    
    <tr><td>from addr:</td>
      <td><input type="text" id = "txtFrom" value="xxx@" />
      </td>
    </tr>

    <tr><td>to addr:</td>
      <td><input type="text" id = "txtTo" value="xxx@" />
      </td>
    </tr>

    <tr><td>title:</td>
      <td><input type="text" id = "txtTitle" value="title" />
      </td>
    </tr>

    <tr><td>content:</td>
      <td><input type="text" id = "txtContent" value="Content" />
      
      </td>
    </tr>
    <tr>
      <td>
        <input type="button"  value="send" onclick="sendemail()"/>
      </td>
    </tr>
  </table>
</body>
</html>

2. The background code is a general processing class ashx for asynchronous call in the foreground

<%@ WebHandler Language="C#" class="Handler" %>

using System;
using ;
using Utility;
public class Handler : IHttpHandler {
  
  public void ProcessRequest (HttpContext context)
  {
     = "text/plain";
    string smtp = ["smtp"].ToString();
    string title = ["title"].ToString();
    string content = ["content"].ToString();
    string from = ["from"].ToString();
    string to = ["to"].ToString();
    
    
    try
    {
      EmailClient emailClient = new EmailClient(smtp);// localhost::25
      (from, to, title, content);
       jss = new ();
      <string, object> d = new <string, object>();
      ("message", "success");
      ("success", true);
      ((d));
    }
    catch (Exception ex)
    {
       jss = new ();
      <string, object> d = new <string, object>();
      ("message", );
      ("success", true);
      ((d));
    }
    
      
  }
 
  public bool IsReusable {
    get {
      return false;
    }
  }

}

3. Finally, the SMTP auxiliary class used

public class EmailClient
  {
    private string smtpServer;
    private string senderAddress;

    
    public EmailClient(string smtpServer)
    {
       = smtpServer;
       = ;
    }

 public void SendEmail(string fromAddress, string toAddress, string subject, string messageBody)
    {
      SmtpClient smtp = new SmtpClient(smtpServer);

      MailMessage email = new MailMessage();

       = new MailAddress(fromAddress);
      (toAddress);
       = subject;
       = messageBody;

      (email);
    }

}