C#简单实现在网页上发邮件的案例

 更新时间:2016年03月26日 14:50:22   作者:攻城狮caitou  
本文分享一个C#利用SMTP发送邮件的案例,提供了前后台代码,方便大家学习。

1.前端HTML使用了Jquery,大家如果做演示不要忘记引入Jquery的库

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
  <script src="jquery-1.8.0.min.js" 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("Handler.ashx", { 'smtp': smtp, 'content': content,'title':title, 'from': from, 'to': to },
    function (data) {
      var n = eval('(' + data + ')');
      if (n.success) {
        alert(n.message);
      }
    });
    
    }
  </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@xxx.com" />
      </td>
    </tr>

    <tr><td>to addr:</td>
      <td><input type="text" id = "txtTo" value="xxx@xxx.com" />
      </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" id="btnSend" value="send" onclick="sendemail()"/>
      </td>
    </tr>
  </table>
</body>
</html>

2.后台代码是一般处理类 ashx,供前台异步调用

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

using System;
using System.Web;
using Utility;
public class Handler : IHttpHandler {
  
  public void ProcessRequest (HttpContext context)
  {
    context.Response.ContentType = "text/plain";
    string smtp = HttpContext.Current.Request.Form["smtp"].ToString();
    string title = HttpContext.Current.Request.Form["title"].ToString();
    string content = HttpContext.Current.Request.Form["content"].ToString();
    string from = HttpContext.Current.Request.Form["from"].ToString();
    string to = HttpContext.Current.Request.Form["to"].ToString();
    
    
    try
    {
      EmailClient emailClient = new EmailClient(smtp);// localhost::25
      emailClient.SendEmail(from, to, title, content);
      System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
      System.Collections.Generic.Dictionary<string, object> d = new System.Collections.Generic.Dictionary<string, object>();
      d.Add("message", "success");
      d.Add("success", true);
      context.Response.Write(jss.Serialize(d));
    }
    catch (Exception ex)
    {
      System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
      System.Collections.Generic.Dictionary<string, object> d = new System.Collections.Generic.Dictionary<string, object>();
      d.Add("message", ex.Message);
      d.Add("success", true);
      context.Response.Write(jss.Serialize(d));
    }
    
      
  }
 
  public bool IsReusable {
    get {
      return false;
    }
  }

}

3.最后是用到的SMTP辅助类

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

    
    public EmailClient(string smtpServer)
    {
      this.smtpServer = smtpServer;
      this.senderAddress = string.Empty;
    }

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

      MailMessage email = new MailMessage();

      email.From = new MailAddress(fromAddress);
      email.To.Add(toAddress);
      email.Subject = subject;
      email.Body = messageBody;

      smtp.Send(email);
    }

}

相关文章

  • C#异步编程由浅入深(二)之Async/Await的使用

    C#异步编程由浅入深(二)之Async/Await的使用

    这篇文章主要介绍了C#异步编程由浅入深(二)之Async/Await的作用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • c# AES字节数组加密解密流程及代码实现

    c# AES字节数组加密解密流程及代码实现

    这篇文章主要介绍了c# AES字节数组加密解密流程及代码实现,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-11-11
  • 杂谈try-catch-finally异常处理

    杂谈try-catch-finally异常处理

    这篇文章主要介绍了杂谈try-catch-finally异常处理的相关资料,需要的朋友可以参考下
    2016-01-01
  • C#装箱和拆箱操作实例分析

    C#装箱和拆箱操作实例分析

    这篇文章主要介绍了C#装箱和拆箱操作,结合实例形式分析了C#中装箱与拆箱的概念、用法及相关注意事项,需要的朋友可以参考下
    2016-08-08
  • C# 实现颜色渐变窗体控件详细讲解

    C# 实现颜色渐变窗体控件详细讲解

    这篇文章主要介绍了C# 实现颜色渐变窗体控件详细讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • c#基础学习之封装

    c#基础学习之封装

    说到封装,其实是比较基础类的问题,它为程序设计提供了系统与系统,模块与模块,类与类之间交互的实现手段
    2013-09-09
  • C#线程定义和使用方法详解

    C#线程定义和使用方法详解

    这篇文章主要介绍了C#Thread类的基本用法,如何定义一个线程类,为线程传递参数的方法,详解看下文
    2013-11-11
  • C#与Java的MD5简单验证(实例代码)

    C#与Java的MD5简单验证(实例代码)

    下面小编就为大家带来一篇C#与Java的MD5简单验证(实例代码)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • C# 表达式树Expression Trees的知识梳理

    C# 表达式树Expression Trees的知识梳理

    本篇文章主要介绍了表达式树 Expression Trees的基础知识:Lambda 表达式创建表达式树;API 创建表达式树;编译表达式树;执行表达式树;修改表达式树等等,具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • C#键值对容器的介绍

    C#键值对容器的介绍

    C#键值对容器的介绍,需要的朋友可以参考一下
    2013-05-05

最新评论