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

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

本文简介:选择自 qs1976 的 blog

   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正确代码哈希表
  /// </summary>
  private hashtable rightcodeht = new hashtable();

  public smtpserverhelper()
  {
   smtpcodeadd();//初始化smtpcode
  }

  ~smtpserverhelper()
  {
   networkstream.close();
   tcpclient.close();
  }

  /// <summary>
  /// 将字符串编码为base64字符串
  /// </summary>
  /// <param name="str">要编码的字符串</param>
  private string base64encode(string str)
  {
   byte[] barray;
   barray=encoding.default.getbytes(str);
   return convert.tobase64string(barray);
  }

  /// <summary>
  /// 将base64字符串解码为普通字符串
  /// </summary>
  /// <param name="str">要解码的字符串</param>
  private string base64decode(string str)
  {
   byte[] barray;
   barray=convert.frombase64string(str);
   return encoding.default.getstring(barray);
  }

  /// <summary>
  /// 得到上传附件的文件流
  /// </summary>
  /// <param name="filepath">附件的绝对路径</param>
  private string getstream(string filepath)
  {
   //建立文件流对象
   system.io.filestream filestr=new system.io.filestream(filepath,system.io.filemode.open);
   byte[] by=new byte[system.convert.toint32(filestr.length)];
   filestr.read(by,0,by.length);
   filestr.close();
   return(system.convert.tobase64string(by));
  }

  /// <summary>
  /// smtp回应代码哈希表
  /// </summary>
  private void smtpcodeadd()
  {
   //[rfc 821 4.2.1.]
   /*
     4.2.2.  numeric order list of reply codes

   211 system status, or system help reply
   214 help message
      [information on how to use the receiver or the meaning of a
      particular non-standard command; this reply is useful only
      to the human user]
   220 <domain> service ready
   221 <domain> service closing transmission channel
   250 requested mail action okay, completed
   251 user not local; will forward to <forward-path>
         

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

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

go top