跟据前面几章的基础,我们现在可以把发送Email的类提炼出来了,以后可以直接使用。
为了使用方便,先建立一个smallfoolsEmail.xml,把一般设置都放在里面,这样的话,程序编好后,如果要修改smtp服务器的设置,只要把smallfoolsEmail.xml文件改改就行,不用重新编译了。
smallfoolsEmail.xml的内容如下:
smtp.sina.com.cn
smallfools@hotmail.com
true
smallfools
smallfools
再建一个smallfoolsEmail.cs文件,把发送邮件所要的操作都放在里面。内容如下:
using System;
using System.Text;
using System.Xml;
using System.IO;
using System.Web.Mail;
namespace email
{
///
/// 小笨笨的发送邮件类
///
public class smallfoolsEmail
{///
/// smtp服务器地址
///
public string SmtpAddress = "";
///
/// smtp服务器是否需要验证(需要为true,不需要为false)
///
public bool validate = false;
///
/// 如果smtp服务器要验证的话,验证的用户名
///
public string SendUserName = "";
///
/// 如果smtp服务器要验证的话,验证的密码
///
public string SendPassword = "";
///
/// 邮件回复到哪个信箱里
///
public string ReplyTo = "";
///
/// 初始化smallfoolsEmail类
///
public smallfoolsEmail()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath("smallfoolsEmail.xml"));
XmlNode myNode = xmlDoc.DocumentElement.FirstChild;
if (myNode!=null)
{
SmtpAddress = myNode["SmtpAddress"].InnerText;
validate = bool.Parse(myNode["validate"].InnerText);
SendUserName = myNode["SendUserName"].InnerText;
SendPassword = myNode["SendPassword"].InnerText;
ReplyTo = myNode["ReplyTo"].InnerText;
}
}
///
/// 发送Email
///
/// 收件人email地址,可用用“;”分开的多个地址
/// 抄送地址
/// 暗送地址
/// 邮件主题
/// 邮件正文
/// 优先级
/// 是否HTML邮件
/// 附件列表
public bool SendEmail(string To,string Cc,string Bcc,string Subject,string Body,string Priority,bool IsHtml,string[] Attachments)
{
bool bFlag = false;
if (!IsHtml)
{
Body = System.Web.HttpContext.Current.Server.HtmlDecode(Body);
}
MailMessage myMail = new MailMessage();
myMail.To = To;
myMail.From = ReplyTo;
myMail.Cc = Cc;
myMail.Bcc = Bcc;
myMail.Subject = Subject;
myMail.Body = Body;
myMail.Priority = mailPriority(Priority);
for (int i=0;i
{
MailAttachment myAttachment = new System.Web.Mail.MailAttachment(Attachments[i].ToString());
myMail.Attachments.Add(myAttachment);
}
SmtpMail.SmtpServer = SmtpAddress;
if (validate)
{
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", SendUserName);
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", SendPassword);
}
try
{
SmtpMail.Send(myMail);
bFlag = true;
}
catch(Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.Message);
}
if (bFlag)
{
for (int i=0;i
{
DelFile(Attachments[i].ToString());
}
}
return bFlag;
}
///
/// 返回邮件发送优先级
///
private System.Web.Mail.MailPriority mailPriority(string Priority)
{
Priority = Priority.ToLower();
MailPriority rePriority = MailPriority.Normal;
switch (Priority)
{
case "high" :
rePriority = MailPriority.High;
break;
case "low" :
rePriority = MailPriority.Low;
break;
case "normal" :
rePriority = MailPriority.Normal;
break;
}
return rePriority;
}
///
/// 上传附件
///
/// 客户端文件
public bool UpFile(System.Web.HttpPostedFile PostedFile)
{
bool bFlag = false;
string clientFileName = PostedFile.FileName;
string[] strTemp = clientFileName.Split('\\');
string ServerFileName = System.Web.HttpContext.Current.Server.MapPath("Attachments\\"+strTemp[strTemp.Length-1].ToString());
try
{
PostedFile.SaveAs(ServerFileName);
bFlag = true;
}
catch
{}
return bFlag;
}
///
/// 删除上传附件
///
/// 服务器端附件名
public bool DelFile(string ServerFileName)
{
bool bFlag = false;
try
{
FileInfo myFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath("Attachments\\"+ServerFileName));
myFile.Delete();
bFlag = true;
}
catch
{}
return bFlag;
}
}
}
现在可以动手编写页面了,如index.aspx的代码如下:
<%@ Page language="c#" Codebehind="index.aspx.cs" AutoEventWireup="false" Inherits="email.index" %>
小笨笨的Email发送系统
http://schemas.microsoft.com/intellisense/ie5">
而index.aspx.cs的代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace email
{
///
/// index 的摘要说明。
///
public class index : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox tbTo;
protected System.Web.UI.WebControls.TextBox tbCc;
protected System.Web.UI.WebControls.TextBox tbBcc;
protected System.Web.UI.WebControls.TextBox tbSubject;
protected System.Web.UI.WebControls.DropDownList ddlPriority;
protected System.Web.UI.WebControls.DropDownList ddlIfHtml;
protected System.Web.UI.WebControls.TextBox tbBody;
protected System.Web.UI.WebControls.DropDownList ddlUpFile;
protected System.Web.UI.WebControls.Button btUpFile;
protected System.Web.UI.WebControls.Button btSend;
protected System.Web.UI.WebControls.Button btDelFile;
protected System.Web.UI.HtmlControls.HtmlInputFile upfile;
protected smallfoolsEmail myemail = new smallfoolsEmail();
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.btUpFile.Click += new System.EventHandler(this.btUpFile_Click);
this.btSend.Click += new System.EventHandler(this.btSend_Click);
this.btDelFile.Click += new System.EventHandler(this.btDelFile_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btUpFile_Click(object sender, System.EventArgs e)
{
if (this.upfile.PostedFile.ContentLength != 0)
{
if (myemail.UpFile(this.upfile.PostedFile))
{
string clientFileName = this.upfile.PostedFile.FileName;
string[] strTemp = clientFileName.Split('\\');
string ServerFileName = strTemp[strTemp.Length-1].ToString();
this.ddlUpFile.Items.Add(new ListItem(this.upfile.PostedFile.FileName,ServerFileName));
if (this.ddlUpFile.Items.Count>0)
{
this.btDelFile.Enabled = true;
}
else
{
this.btDelFile.Enabled = false;
}
}
else
{
Page.RegisterClientScriptBlock("err","");
}
}
}
private void btDelFile_Click(object sender, System.EventArgs e)
{
if (myemail.DelFile(this.ddlUpFile.SelectedValue))
{
this.ddlUpFile.Items.Remove(this.ddlUpFile.Items[this.ddlUpFile.SelectedIndex]);
if (this.ddlUpFile.Items.Count>0)
{
this.btDelFile.Enabled = true;
}
else
{
this.btDelFile.Enabled = false;
}
Page.RegisterClientScriptBlock("OK","");
}
else
{
Page.RegisterClientScriptBlock("err","");
}
}
private void btSend_Click(object sender, System.EventArgs e)
{
string To = this.tbTo.Text.Trim();
string Cc = this.tbCc.Text.Trim();
string Bcc = this.tbBcc.Text.Trim();
string Subject = this.tbSubject.Text.Trim();
string Body = this.tbBody.Text;
string Priority = this.ddlPriority.SelectedValue;
bool IsHtml = bool.Parse(this.ddlIfHtml.SelectedValue);
string[] Attachments = new string[this.ddlUpFile.Items.Count];
for (int i=0;i
{
Attachments[i] = this.ddlUpFile.Items[i].Value;
}
if (myemail.SendEmail(To,Cc,Bcc,Subject,Body,Priority,IsHtml,Attachments))
{
Page.RegisterClientScriptBlock("ok","");
}
else
{
Page.RegisterClientScriptBlock("ok","");
}
}
}
}
本文作者:佚名 来源:http://smallfools.blog.hexun.com/
CIO之家 www.ciozj.com 微信公众号:imciow