用c#写的smtp邮件发送类
//**********************Created by Chen**************************
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Collections.Specialized;
using KSN.Exceptions;
using KSN.Validate;
namespace KSN.Web.Mail
{
/// <summary>
/// 邮件内容
/// </summary>
public class MailMessage
{
private string sender=null;
private StringCollection receivers=new StringCollection();
private string subject="";
private string xMailer="";
private StringCollection attachments=new StringCollection();
private MailEncodings mailEncoding=MailEncodings.GB2312;
private MailTypes mailType=MailTypes.Html;
private byte[] mailBody=null;
/// <summary>
/// 获取或设置发件人
/// </summary>
public string Sender
{
get{return this.sender;}
set{this.sender=value;}
}
/// <summary>
/// 获取收件人地址集合
/// </summary>
public StringCollection Receivers
{
get{return this.receivers;}
}
/// <summary>
/// 获取或设置邮件主题
/// </summary>
public string Subject
{
get{return this.subject;}
set{this.subject=value;}
}
/// <summary>
/// 获取或设置邮件传送者
/// </summary>
public string XMailer
{
get{return this.xMailer;}
set{this.xMailer=value;}
}
/// <summary>
/// 获取附件列表
/// </summary>
public StringCollection Attachments
{
get{return this.attachments;}
}
/// <summary>
/// 获取或设置邮件的编码方式
/// </summary>
public MailEncodings MailEncoding
{
get{return this.mailEncoding;}
set{this.mailEncoding=value;}
}
/// <summary>
/// 获取或设置邮件格式
/// </summary>
public MailTypes MailType
{
get{return this.mailType;}
set{this.mailType=value;}
}
/// <summary>
/// 获取或设置邮件正文
/// </summary>
public byte[] MailBody
{
get{return this.mailBody;}
set{this.mailBody=value;}
}
}
/// <summary>
/// 邮件编码
/// </summary>
public enum MailEncodings
{
GB2312,
ASCII,
Unicode,
UTF8
}
/// <summary>
/// 邮件格式
/// </summary>
public enum MailTypes
{
Html,
Text
}
/// <summary>
/// smtp服务器的验证方式
/// </summary>
public enum SmtpValidateTypes
{
/// <summary>
/// 不需要验证
/// </summary>
None,
/// <summary>
/// 通用的auth login验证
/// </summary>
Login,
/// <summary>
/// 通用的auth plain验证
/// </summary>
Plain,
/// <summary>
/// CRAM-MD5验证
/// </summary>
CRAMMD5
}
/// <summary>
/// 邮件发送类
/// </summary>
public class KSN_Smtp
{
#region "member fields"
/// <summary>
/// 连接对象
/// </summary>
private TcpClient tc;
/// <summary>
/// 网络流
/// </summary>
private NetworkStream ns;
/// <summary>
/// 错误的代码字典
/// </summary>
private StringDictionary errorCodes=new StringDictionary();
/// <summary>
/// 操作执行成功后的响应代码字典
/// </summary>
private StringDictionary rightCodes=new StringDictionary();
/// <summary>
/// 执行过程中错误的消息
/// </summary>
private string errorMessage="";
/// <summary>
/// 记录操作日志
/// </summary>
private string logs="";
/// <summary>
/// 主机登陆的验证方式
/// </summary>
private StringCollection validateTypes=new StringCollection();
/// <summary>
/// 换行常数
/// </summary>
private const string CRLF=" ";
private string serverName="smtp";
private string logPath=null;
private string userid=null;
private string password=null;
private string mailEncodingName="GB2312";
private bool sendIsComplete=false;
private SmtpValidateTypes smtpValidateType=SmtpValidateTypes.Login;
#endregion
#region "propertys"
/// <summary>
/// 获取最后一此程序执行中的错误消息
/// </summary>
public string ErrorMessage
{
get{return this.errorMessage;}
}
/// <summary>
/// 获取或设置日志输出路径
/// </summary>
public string LogPath
{
get
{
return this.logPath;
}
set{this.logPath=value;}
}
/// <summary>
/// 获取或设置登陆smtp服务器的帐号
/// </summary>
public string UserID
{
get{return this.userid;}
set{this.userid=value;}
}
/// <summary>
/// 获取或设置登陆smtp服务器的密码
/// </summary>
public string Password
{
get{return this.password;}
set{this.password=value;}
}
/// <summary>
/// 获取或设置要使用登陆Smtp服务器的验证方式
/// </summary>
public SmtpValidateTypes SmtpValidateType
{
get{return this.smtpValidateType;}
set{this.smtpValidateType=value;}
}
#endregion
#region "construct functions"
/// <summary>
/// 构造函数
/// </summary>
/// <param name="server">主机名</param>
/// <param name="port">端口</param>
public KSN_Smtp(string server,int port)
{
tc=new TcpClient(server,port);
ns=tc.GetStream();
this.serverName=server;
this.initialFields();
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="ip">主机ip</param>
/// <param name="port">端口</param>
public KSN_Smtp(IPAddress ip,int port)
{
IPEndPoint endPoint=new IPEndPoint(ip,port);
tc=new TcpClient(endPoint);
ns=tc.GetStream();
this.serverName=ip.ToString();
this.initialFields();
}
#endregion
#region "methods"
private void initialFields() //初始化连接
{
logs="================"+DateTime.Now.ToLongDateString()+" "+DateTime.Now.ToLongTimeString()+"==============="+CRLF;
//*****************************************************************
&
补充:Web开发 , ASP.Net ,