C#实现SMTP服务器,使用TCP命令实现,功能比较完善[1]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 qs1976 的 blog


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)
  {

本文关键:C#实现SMTP服务器,使用TCP命令实现,功能比较完善
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top