c#实现smtp服务器,使用tcp命令实现,功能比较…

2008-02-23 05:44:40来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections;

namespace SkyDev.Web.Mail
{
public enum MailFormat{Text,HTML};
public enum MailPriority{Low=1,Normal=3,High=5};

#region Class mailAttachments
public class MailAttachments
{
private const int MaxAttachmentNum=10;
private IList _Attachments;

public MailAttachments()
{
_Attachments=new ArrayList();
}

public string this[int index]
{
get { return (string)_Attachments[index];}
}
/// <summary>
/// 添加邮件附件
/// </summary>
/// <param name="FilePath">附件的绝对路径</param>
public void Add(params string[] filePath)
{
if(filePath==null)
{
throw(new ArgumentNullException("非法的附件"));
}
else
{
for(int i=0;i<filePath.Length;i )
{
Add(filePath[i]);
}
}
}

/// <summary>
/// 添加一个附件,当指定的附件不存在时,忽略该附件,不产生异常。
/// </summary>
/// <param name="filePath">附件的绝对路径</param>
public void Add(string filePath)
{
//当附件存在时才加入,否则忽略
if (System.IO.File.Exists(filePath))
{
if (_Attachments.Count<MaxAttachmentNum)
{
_Attachments.Add(filePath);
}
}
}

public void Clear()//清除任何附件
{
_Attachments.Clear();
}

public int Count//获取附件个数
{
get { return _Attachments.Count;}
}

}
#endregion//end Class mailAttachments

#region Class MailMessage
/// <summary>
/// MailMessage 表示SMTP要发送的一封邮件的消息。
/// </summary>
public class MailMessage
{
private const int MaxRecipientNum=10;
public MailMessage()
{
_Recipients=new ArrayList();//收件人列表
_Attachments=new MailAttachments();//附件
_BodyFormat=MailFormat.Text;//缺省的邮件格式为Text
_Priority=MailPriority.Normal;
_Charset="GB2312";
}

/// <summary>
/// 设定语言代码,默认设定为GB2312,如无需可配置为""
/// </summary>
public string Charset
{
get { return _Charset;}
set { _Charset=value;}
}

public string From
{
get{ return _From;}
set { _From=value;}
}

public string FromName
{
get { return _FromName;}
set { _FromName=value;}
}
public string Body
{
get { return _Body;}
set { _Body=value;}
}

public string Subject
{
get { return _Subject;}
set { _Subject=value;}
}

public MailAttachments Attachments
{
get {return _Attachments;}
set { _Attachments=value;}
}

public MailPriority Priority
{
get { return _Priority;}
set { _Priority=value;}
}

public IList Recipients
{
get { return _Recipients;}
}
/// <summary>
/// 增加一个收件人地址
/// </summary>
/// <param name="recipient">收件人的Email地址</param>
public void AddRecipients(string recipient)
{
//先检查邮件地址是否符合规范
if (_Recipients.Count<MaxRecipientNum)
{
_Recipients.Add(recipient);//增加到收件人列表
}
}

public void AddRecipients(params string[] recipient)
{
if (recipient==null)
{
throw (new ArgumentException("收件人不能为空."));
}
else
{
for (int i=0;i<recipient.Length;i )
{
AddRecipients(recipient[i]);
}
}
}

public MailFormat BodyFormat
{
set { _BodyFormat=value;}
get { return _BodyFormat;}
}

private string _From;//发件人地址
private string _FromName;//发件人姓名
private IList _Recipients;//收件人
private MailAttachments _Attachments;//附件
private string _Body;//内容
private string _Subject;//主题
private MailFormat _BodyFormat;//邮件格式
private string _Charset="GB2312";//字符编码格式
private MailPriority _Priority;//邮件优先级
}
#endregion


#region Class SmtpMail
public class SmtpServerHelper
{
private string CRLF="\r\n";//回车换行

/// <summary>
/// 错误消息反馈
/// </summary>
private string errmsg;

/// <summary>
/// TcpClient对象,用于连接服务器
/// </summary>
private TcpClient tcpClient;

/// <summary>
/// NetworkStream对象
/// </summary>
private NetworkStream networkStream;

/// <summary>
/// 服务器交互记录
/// </summary>
private string logs="";

/// <summary>
/// SMTP错误代码哈希表
/// </summary>
private Hashtable ErrCodeHT = new Hashtable();

/// <summary>
/// SMTP正确代码哈希表

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: 在c#中编写多线程应用程式,简单!_c#应用

下一篇: 以编程方式使用批语句节省 数据库io来提高性能 _c#应用