邮件发送类,支持HTML格式,支持优先级设置。通过SOCKET类实现的
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Blood.Com.ClassLib
{
///
/// TcpClient派生类,用来进行SMTP服务器的连接工作
///
public class SMTPClient : TcpClient
{
public bool isConnected()
{
return Active;
}
public void SendCommandToServer(string Command)
{
NetworkStream ns = this.GetStream() ;
byte[] WriteBuffer ;
WriteBuffer = new byte[1024] ;
WriteBuffer = Encoding.Default.GetBytes(Command) ;
ns.Write(WriteBuffer,0,WriteBuffer.Length);
return ;
}
public string GetServerResponse()
{
int StreamSize ;
string ReturnValue = "" ;
byte[] ReadBuffer ;
NetworkStream ns = this.GetStream() ;
ReadBuffer = new byte[1024] ;
StreamSize = ns.Read(ReadBuffer,0,ReadBuffer.Length);
if (StreamSize==0)
{
return ReturnValue ;
}
else
{
ReturnValue = Encoding.Default.GetString(ReadBuffer);
return ReturnValue;
}
}
public bool DoesStringContainSMTPCode(string s,string SMTPCode)
{
return(s.IndexOf(SMTPCode,0,10)==-1)?false:true;
}
} //结束类
///
/// 发送邮件类
///
public class SMTPMail
{
///
/// 错误反馈信息
///
private string strErrMessage = null;
///
/// SMTP服务器反馈的信息
///
private string strResponse;
///
/// 构造函数
///
public SMTPMail()
{
strErrMessage = "";
strResponse = "";
}
///
/// 取得错误反馈信息
///
public string ErrorMessage
{
get
{
return strErrMessage ;
}
}
///
/// 取得SMTP服务器反馈的信息
///
public string ServerResponse
{
get
{
return strResponse;
}
}
///
/// 邮件发送优先级
///
public enum Prioritys
{
///
/// 最高级别
///
HIGH = 1,
///
/// 默认级别
///
NORMAL = 3,
///
/// 最低级别
///
LOW = 5
}
public void SendMail(string SmtpHost,int Port,string From,string DisplayFromName,string To,string DisplayToName,Prioritys Priority,bool Html,string Base,string Subject,string Message)
{
try
{
string strResponseNumber;
SMTPClient smtpcMail = new SMTPClient();
smtpcMail.Connect(SmtpHost,Port);
bool bolConnect = smtpcMail.isConnected();
//判断是否进行了连接
if (!bolConnect)
{
strErrMessage = "Smtp服务器连接失败...";
return;
}
//读取反馈信息
strResponseNumber = smtpcMail.GetServerResponse();
if (smtpcMail.DoesStringContainSMTPCode(strResponseNumber,"220"))
{
this.strResponse += strResponseNumber;
}
else
{
this.strErrMessage = "连接失败" + strResponseNumber;
return ;
}
string[] strSendBuffer = new string[6];
string[] strResponseCode = {"220","250","251","354","221"}; // success codes from SMTP server
string strData = "";
strData = string.Concat("HELO ",SmtpHost);
strData = string.Concat(strData,"\r\n");
strSendBuffer[0] = strData ;
strData = "";
strData = string.Concat("MAIL FROM: ","");
strData = string.Concat(strData,"\r\n");
strSendBuffer[1] = strData;
strData = "";
strData = string.Concat("RCPT TO: ","");
strData = string.Concat(strData,"\r\n");
strSendBuffer[2] = strData;
strData = "" ;
strData = string.Concat("DATA","\r\n");
strSendBuffer[3] = strData ;
strData = "" ;
strData = string.Concat("From: ",DisplayFromName + "");
strData = string.Concat(strData,"\r\n" );
strData = string.Concat(strData,"To: " );
strData = string.Concat(strData,DisplayToName + "");
strData = string.Concat(strData,"\r\n" );
strData = string.Concat(strData,"Subject: " );
strData = string.Concat(strData,Subject);
strData = string.Concat(strData,"\r\n");
strData = string.Concat(strData,"MIME-Version: 1.0" );
strData = string.Concat(strData,"\r\n");
strData = string.Concat(strData,"X-Priority: " + Priority);
strData = string.Concat(strData,"\r\n");
strData = string.Concat(strData,"X-MSMail-Priority: " + Priority);
strData = string.Concat(strData,"\r\n");
if(Html == true)
{
strData = string.Concat(strData,"Content-Type: text/html;" );
}
else
{
strData = string.Concat(strData,"Content-Type: text/plain;" );
}
strData = string.Concat(strData,"\r\n");
strData = string.Concat(strData,"charset=\"iso-8859-1\"" );
strData = string.Concat(strData,"\r\n");
if(Html == true)
{
strData = string.Concat(strData,"Content-Transfer-Encoding: text/html;" );
}
else
{
strData = string.Concat(strData,"Content-Transfer-Encoding: text/plain;" );
}
strData = string.Concat(strData,"\r\n");
strData = string.Concat(strData,"Content-Base: \"" + Base + "\"" );
strData = string.Concat(strData,"\r\n" + "\r\n");
strData = string.Concat(strData,Message);
strData = string.Concat(strData,"\r\n.\r\n");
strSendBuffer[4] = strData;
strData = "" ;
strData = string.Concat(strData,"QUIT\r\n");
strSendBuffer[5] = strData;
int i = 0 ;
while(i < strSendBuffer.Length)
{
smtpcMail.SendCommandToServer(strSendBuffer[i]);
strResponseNumber = smtpcMail.GetServerResponse();
for(int j=0;j
{
if (smtpcMail.DoesStringContainSMTPCode(strResponseNumber,strResponseCode[j]))
{
this.strResponse += strResponseNumber;
this.strResponse += "
";
break;
}
else
{
if(j==strResponseCode.Length-1)
{
this.strErrMessage += strResponseNumber;
this.strErrMessage += strSendBuffer[i];
return;
}
}
}
i++ ;
} // 结束循环
}
catch(SocketException err)
{
this.strErrMessage += err.Message + " " + err.StackTrace;
}
catch(Exception e)
{
this.strErrMessage += e.Message + " " + e.StackTrace;
}
} //结束邮件发送方法
} // 结束类
} // 结束Namespace
邮件发送前台页面
邮件发送例子FONT-SIZE: 9pt; FONT-FAMILY: "宋体"
}
.TextBox {
BORDER-RIGHT: #000000 thin dashed; BORDER-TOP: #000000 thin dashed; BORDER-LEFT: #000000 thin dashed; BORDER-BOTTOM: #000000 thin dashed
}
邮件发送例子(SOCKET类) | |
SMTP服务器: | |
SMTP服务器端口: | |
邮件发送者: | |
发送者显示名称: | |
邮件接收者: | |
接收者显示名称: | |
邮件发送优先级: | 高 默认 低 |
发送HTML格式的邮件: | 是 否 |
邮件主题: | |
邮件内容: | |
邮件发送Codebehind
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Blood.Com.ClassLib;
namespace Test
{
///
/// 邮件发送例子
///
public class SendMailSample : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DropDownList ddlPriority;
protected System.Web.UI.WebControls.DropDownList ddlHtml;
protected System.Web.UI.WebControls.TextBox txtSmtpHost;
protected System.Web.UI.WebControls.TextBox txtPort;
protected System.Web.UI.WebControls.TextBox txtFrom;
protected System.Web.UI.WebControls.TextBox txtFromName;
protected System.Web.UI.WebControls.TextBox txtTo;
protected System.Web.UI.WebControls.TextBox txtToName;
protected System.Web.UI.WebControls.TextBox txtSubject;
protected System.Web.UI.WebControls.TextBox txtMessage;
protected System.Web.UI.WebControls.RegularExpressionValidator revPort;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvSmtpHost;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvPort;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvFrom;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvTo;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvSubject;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvMessage;
protected System.Web.UI.WebControls.Button btnSendMail;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.RegularExpressionValidator revFrom;
protected System.Web.UI.WebControls.RegularExpressionValidator revTo;
protected System.Web.UI.WebControls.Panel pelMessage;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.btnSendMail.Click += new System.EventHandler(this.btnSendMail_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnSendMail_Click(object sender, System.EventArgs e)
{
if(IsPostBack)
{
SMTPMail clsMail = new SMTPMail();
string strSmtpHost = txtSmtpHost.Text;
int intPort = Int32.Parse(txtPort.Text);
string strFrom = txtFrom.Text;
string strFromName = txtFromName.Text;
string strTo = txtTo.Text;
string strToName = txtToName.Text;
SMTPMail.Prioritys Priority = SMTPMail.Prioritys.NORMAL;
bool bolHtml;
string strSubject = txtSubject.Text;
string strMessage = txtMessage.Text;
if(ddlPriority.SelectedItem.Text == "默认")
{
Priority = SMTPMail.Prioritys.NORMAL;
}
else if(ddlPriority.SelectedItem.Text == "高")
{
Priority = SMTPMail.Prioritys.HIGH;
}
else if(ddlPriority.SelectedItem.Text == "低")
{
Priority = SMTPMail.Prioritys.LOW;
}
if(ddlHtml.SelectedItem.Text == "是")
{
bolHtml = true;
}
else
{
bolHtml = false;
}
clsMail.SendMail(strSmtpHost,intPort,strFrom,strFromName,strTo,strToName,Priority,bolHtml,"",strSubject,strMessage);
if(clsMail.ErrorMessage !="")
{
pelMessage.Visible = true;
lblMessage.Text = clsMail.ErrorMessage;
lblMessage.ForeColor = System.Drawing.Color.Red;
}
else
{
pelMessage.Visible = true;
lblMessage.Text = "邮件发送成功";
lblMessage.ForeColor = System.Drawing.Color.Blue;
}
}
}
}
}