专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »DotNet » smtp邮件服务器:用c#写的smtp邮件发送类(1) »正文

smtp邮件服务器:用c#写的smtp邮件发送类(1)

来源: 发布时间:星期五, 2009年1月9日 浏览:45次 评论:0
  //**********************Created by Chen**************************using ;
  using .IO;
  using .Text;
  using .Net;
  using .Net.Sockets;
  using .Collections;
  using .Collections.Specialized;
  using KSN.Exceptions;
  using KSN.Validate;
   KSN.Web.Mail
  {
  /// <summary>
  /// 邮件内容
  /// </summary>
  public MailMessage
  {
  private
   sender=null;
  private StringCollection receivers= StringCollection( );
  private
   subject="";
  private
   xMailer="";
  private StringCollection attachments= StringCollection( );
  private MailEncodings mailEncoding=MailEncodings.GB2312;
  private MailTypes mailType=MailTypes.Html;
  private mailBody=null;
  /// <summary> 
  /// 获取或设置发件人 
  /// </summary> 
  public
   Sender 
  {
  get
  {
  
  this.sender;
  }
  
  {
  this.sender=value;
  }
  }
  /// <summary> 
  /// 获取收件人地址集合 
  /// </summary> 
  public StringCollection Receivers 
  {
  get
  {
  
  this.receivers;
  }
  }
  /// <summary> 
  /// 获取或设置邮件主题 
  /// </summary> 
  public
   Subject 
  {
  get
  {
  
  this.subject;
  }
  
  {
  this.subject=value;
  }
  }
  /// <summary> 
  /// 获取或设置邮件传送者 
  /// </summary> 
  public
   XMailer 
  {
  get
  {
  
  this.xMailer;
  }
  
  {
  this.xMailer=value;
  }
  }
  /// <summary> 
  /// 获取附件列表 
  /// </summary> 
  public StringCollection Attachments 
  {
  get
  {
  
  this.attachments;
  }
  }
  /// <summary> 
  /// 获取或设置邮件编码方式 
  /// </summary> 
  public MailEncodings MailEncoding 
  {
  get
  {
  
  this.mailEncoding;
  }
  
  {
  this.mailEncoding=value;
  }
  }
  /// <summary> 
  /// 获取或设置邮件格式 
  /// </summary> 
  public MailTypes MailType 
  {
  get
  {
  
  this.mailType;
  }
  
  {
  this.mailType=value;
  }
  }
  /// <summary> 
  /// 获取或设置邮件正文 
  /// </summary> 
  public MailBody 
  {
  get
  {
  
  this.mailBody;
  }
  
  {
  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 KSN_Smtp
  {
  #region "member fields" 
  /// <summary> 
  /// 连接对象 
  /// </summary> 
  private TcpClient tc;
  /// <summary> 
  /// 网络流 
  /// </summary> 
  private NetworkStream ns;
  /// <summary> 
  /// 代码字典 
  /// </summary> 
  private StringDictionary errorCodes= StringDictionary( );
  /// <summary> 
  /// 操作执行成功后响应代码字典 
  /// </summary> 
  private StringDictionary rightCodes= StringDictionary( );
  /// <summary> 
  /// 执行过程中消息 
  /// </summary> 
  private
   errorMessage="";
  /// <summary> 
  /// 记录操作日志 
  /// </summary> 
  private
   logs="";
  /// <summary> 
  /// 主机登陆验证方式 
  /// </summary> 
  private StringCollection validateTypes= StringCollection( );
  /// <summary> 
  /// 换行常数 
  /// </summary> 
  private const
   CRLF="
  ";
  private
   serverName="smtp";
  private
   logPath=null;
  private
   userid=null;
  private
   password=null;
  private
   mailEncodingName="GB2312";
  private bool sendIsComplete=false;
  private SmtpValidateTypes smtpValidateType=SmtpValidateTypes.Login;
  #endregion #region "propertys" 
  /// <summary> 
  /// 获取最后执行中消息 
  /// </summary> 
  public
   ErrorMessage 
  {
  get
  {
  
  this.errorMessage;
  }
  }
  /// <summary> 
  /// 获取或设置日志输出路径 
  /// </summary> 
  public
   LogPath 
  {
  get
  {
  
  this.logPath;
  }
  
  {
  this.logPath=value;
  }
  }
  /// <summary> 
  /// 获取或设置登陆smtp服务器帐号 
  /// </summary> 
  public
   UserID 
  {
  get
  {
  
  this.userid;
  }
  
  {
  this.userid=value;
  }
  }
  /// <summary> 
  /// 获取或设置登陆smtp服务器密码 
  /// </summary> 
  public
   Password 
  {
  get
  {
  
  this.password;
  }
  
  {
  this.password=value;
  }
  }
  /// <summary> 
  /// 获取或设置要使用登陆Smtp服务器验证方式 
  /// </summary> 
  public SmtpValidateTypes SmtpValidateType 
  {
  get
  {
  
  this.smtpValidateType;
  }
  
  {
  this.smtpValidateType=value;
  }
  }
  #endregion #region "construct functions" 
  /// <summary> 
  /// 构造 
  /// </summary> 
  /// <param name="server">主机名</param> 
  /// <param name="port">端口</param> 
  public KSN_Smtp(
   server,
   port ) 
  {
  tc= 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,
   port ) 
  {
  IPEndPo
   endPo
  = IPEndPo
  ( ip,port );
  tc= TcpClient( endPo
   );
  ns=tc.GetStream( );
  this.serverName=ip.( );
  this.initialFields( );
  }
  #endregion #region "methods" 
  private void initialFields( ) //化连接 
  {
  logs=""+DateTime.Now.ToLongDateString( )+"
  "+DateTime.Now.ToLongTimeString( )+"="+CRLF;
  //*****************************************************************
  //状态码
  //*****************************************************************
  errorCodes.Add( "421","服务未就绪,关闭传输通道" );
  errorCodes.Add( "432","需要个密码转换" );
  errorCodes.Add( "450","要求邮件操作未完成,邮箱不可用( 如:邮箱忙 )" );
  errorCodes.Add( "451","放弃要求操作,要求操作未执行" );
  errorCodes.Add( "452","系统存储不足,要求操作未完成" );
  errorCodes.Add( "454","临时认证失败" );
  errorCodes.Add( "500","邮箱地址" );
  errorCodes.Add( "501","参数格式" );
  errorCodes.Add( "502","命令不可实现" );
  errorCodes.Add( "503","命令次序不正确" );
  errorCodes.Add( "504","命令参数不可实现" );
  errorCodes.Add( "530","需要认证" );
  errorCodes.Add( "534","认证机制过于简单" );
  errorCodes.Add( "538","当前请求认证机制需要加密" );
  errorCodes.Add( "550","当前邮件操作未完成,邮箱不可用( 如:邮箱未找到或邮箱不能用 )" );
  errorCodes.Add( "551","用户非本地,请尝试<forward-path>" );
  errorCodes.Add( "552","过量存储分配,制定操作未完成" );
  errorCodes.Add( "553","邮箱名不可用,如:邮箱地址格式" );
  errorCodes.Add( "554","传送失败" );
  errorCodes.Add( "535","用户身份验证失败" );
  //****************************************************************
  //操作执行成功后状态码
  //****************************************************************
  rightCodes.Add( "220","服务就绪" );
  rightCodes.Add( "221","服务关闭传输通道" );
  rightCodes.Add( "235","验证成功" );
  rightCodes.Add( "250","要求邮件操作完成" );
  rightCodes.Add( "251","非本地用户,将转发向<forward-path>" );
  rightCodes.Add( "334","服务器响应验证Base64串" );
  rightCodes.Add( "354","开始邮件输入,以<CRLF>.<CRLF>结束" );
  //读取系统回应
  StreamReader reader= StreamReader( ns );
  logsreader.ReadLine( )+CRLF;
  }


1

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: