C#簡單實(shí)現(xiàn)在網(wǎng)頁上發(fā)郵件的案例
更新時(shí)間:2016年03月26日 14:50:22 作者:攻城獅caitou
本文分享一個(gè)C#利用SMTP發(fā)送郵件的案例,提供了前后臺(tái)代碼,方便大家學(xué)習(xí)。
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.后臺(tái)代碼是一般處理類 ashx,供前臺(tái)異步調(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#利用system.net發(fā)送html格式郵件
- C#.NET采用HTML模板發(fā)送電子郵件完整實(shí)例
- C#實(shí)現(xiàn)SMTP郵件發(fā)送程序?qū)嵗?/a>
- C#實(shí)現(xiàn)發(fā)送郵件的三種方法
- C#使用smtp發(fā)送帶附件的郵件實(shí)現(xiàn)方法
- C#實(shí)現(xiàn)異步發(fā)送郵件的方法
- C#使用CDO發(fā)送郵件的方法
- C#實(shí)現(xiàn)按數(shù)據(jù)庫郵件列表發(fā)送郵件的方法
- C#編程實(shí)現(xiàn)發(fā)送郵件的方法(可添加附件)
- C#實(shí)現(xiàn)的自定義郵件發(fā)送類完整實(shí)例(支持多人多附件)
相關(guān)文章
c# AES字節(jié)數(shù)組加密解密流程及代碼實(shí)現(xiàn)
這篇文章主要介紹了c# AES字節(jié)數(shù)組加密解密流程及代碼實(shí)現(xiàn),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11
C# 實(shí)現(xiàn)顏色漸變窗體控件詳細(xì)講解
這篇文章主要介紹了C# 實(shí)現(xiàn)顏色漸變窗體控件詳細(xì)講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
C#與Java的MD5簡單驗(yàn)證(實(shí)例代碼)
下面小編就為大家?guī)硪黄狢#與Java的MD5簡單驗(yàn)證(實(shí)例代碼)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
C# 表達(dá)式樹Expression Trees的知識(shí)梳理
本篇文章主要介紹了表達(dá)式樹 Expression Trees的基礎(chǔ)知識(shí):Lambda 表達(dá)式創(chuàng)建表達(dá)式樹;API 創(chuàng)建表達(dá)式樹;編譯表達(dá)式樹;執(zhí)行表達(dá)式樹;修改表達(dá)式樹等等,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01

