C#簡單實現(xiàn)在網(wǎng)頁上發(fā)郵件的案例
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,供前臺異步調(diào)用
<%@ 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# 利用ICSharpCode.SharpZipLib實現(xiàn)在線壓縮和解壓縮
欄 目:C#教程
下一篇:利用C#實現(xiàn)網(wǎng)絡(luò)爬蟲
本文標題:C#簡單實現(xiàn)在網(wǎng)頁上發(fā)郵件的案例
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6611.html
您可能感興趣的文章
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10C#停止線程的方法
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實現(xiàn)多線程下載文件的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實現(xiàn)遠程關(guān)閉計算機或重啟計算機的方法
- 01-10C#自定義簽名章實現(xiàn)方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery


