Email(C#)

None.gif using  System; 
None.gif
using  System.Text; 
None.gif
using  System.IO; 
None.gif
using  System.Net; 
None.gif
using  System.Net.Sockets; 
None.gif
using  System.Collections; 
None.gif
None.gif
namespace  MailList.Data
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif 
InBlock.gif 
public class SmtpEMail 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif  
内部变量#region 内部变量
InBlock.gif
InBlock.gif  
private string enter="\r\n"
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 设定语言代码,默认设定为GB2312,如不需要可设置为"" 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  public string Charset="GB2312"
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 发件人地址 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  public string From=""
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 发件人姓名 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  public string FromName=""
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 回复邮件地址 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  //public string ReplyTo=""; 
ExpandedSubBlockStart.gifContractedSubBlock.gif
  /**//// <summary> 
InBlock.gif  
/// 收件人姓名 
ExpandedSubBlockEnd.gif  
/// </summary>    

InBlock.gif  public string RecipientName=""
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 收件人列表 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private Hashtable Recipient=new Hashtable(); 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 邮件服务器域名 
ExpandedSubBlockEnd.gif  
/// </summary>    

InBlock.gif  private string mailserver=""
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 邮件服务器端口号 
ExpandedSubBlockEnd.gif  
/// </summary>    

InBlock.gif  private int mailserverport=25
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// SMTP认证时使用的用户名 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private string username=""
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// SMTP认证时使用的密码 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private string password=""
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 是否需要SMTP验证 
ExpandedSubBlockEnd.gif  
/// </summary>       

InBlock.gif  private bool ESmtp=false
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 是否Html邮件 
ExpandedSubBlockEnd.gif  
/// </summary>       

InBlock.gif  public bool Html=false
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 邮件附件列表 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private System.Collections.ArrayList Attachments; 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 邮件发送优先级,可设置为"High","Normal","Low"或"1","3","5" 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private string priority="Normal"
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 邮件主题 
ExpandedSubBlockEnd.gif  
/// </summary>       

InBlock.gif  private string Subject=""
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 邮件正文 
ExpandedSubBlockEnd.gif  
/// </summary>       

InBlock.gif  private string Body=""
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 收件人数量 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private int RecipientNum=0
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 最多收件人数量 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private int recipientmaxnum=1
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 密件收件人数量 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  //private int RecipientBCCNum=0; 
ExpandedSubBlockStart.gifContractedSubBlock.gif
  /**//// <summary> 
InBlock.gif  
/// 错误消息反馈 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private string errmsg; 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// TcpClient对象,用于连接服务器 
ExpandedSubBlockEnd.gif  
/// </summary>    

InBlock.gif  private TcpClient tc; 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// NetworkStream对象 
ExpandedSubBlockEnd.gif  
/// </summary>    

InBlock.gif  private NetworkStream ns; 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// SMTP错误代码哈希表 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private Hashtable ErrCodeHT = new Hashtable(); 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// SMTP正确代码哈希表 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private Hashtable RightCodeHT = new Hashtable();
ExpandedSubBlockEnd.gif  
#endregion

InBlock.gif  
InBlock.gif  
//构造函数
InBlock.gif
  public SmtpEMail() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   Attachments 
= new System.Collections.ArrayList(); 
ExpandedSubBlockEnd.gif  }
       
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif  
//属性
InBlock.gif  
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ContractedSubBlock.gifExpandedSubBlockStart.gif
  公共属性#region 公共属性
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 邮件服务器域名和验证信息 
InBlock.gif  
/// 形如:"user:pass@www.server.com:25",也可省略次要信息。如"user:pass@www.server.com"或"www.server.com" 
ExpandedSubBlockEnd.gif  
/// </summary>    

InBlock.gif  public string MailDomain 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
string maidomain=value.Trim(); 
InBlock.gif    
int tempint; 
InBlock.gif
InBlock.gif    
if(maidomain!=""
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif     tempint
=maidomain.IndexOf("@"); 
InBlock.gif     
if(tempint!=-1
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif
InBlock.gif      
string str=maidomain.Substring(0,tempint); 
InBlock.gif      MailServerUserName
=str.Substring(0,str.IndexOf(":")); 
InBlock.gif      MailServerPassWord
=str.Substring(str.IndexOf(":")+1,str.Length-str.IndexOf(":")-1); 
InBlock.gif      maidomain
=maidomain.Substring(tempint+1,maidomain.Length-tempint-1); 
ExpandedSubBlockEnd.gif     }
 
InBlock.gif
InBlock.gif     tempint
=maidomain.IndexOf(":"); 
InBlock.gif     
if(tempint!=-1
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif
InBlock.gif      mailserver
=maidomain.Substring(0,tempint); 
InBlock.gif      mailserverport
=System.Convert.ToInt32(maidomain.Substring(tempint+1,maidomain.Length-tempint-1)); 
ExpandedSubBlockEnd.gif     }
 
InBlock.gif     
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif
InBlock.gif      mailserver
=maidomain; 
InBlock.gif
ExpandedSubBlockEnd.gif     }
 
InBlock.gif
InBlock.gif                
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 邮件服务器端口号 
ExpandedSubBlockEnd.gif  
/// </summary>    

InBlock.gif  public int MailDomainPort 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    mailserverport
=value; 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// SMTP认证时使用的用户名 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  public string MailServerUserName 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
if(value.Trim()!=""
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif     username
=value.Trim(); 
InBlock.gif     ESmtp
=true
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif     username
=""
InBlock.gif     ESmtp
=false
ExpandedSubBlockEnd.gif    }
 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// SMTP认证时使用的密码 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  public string MailServerPassWord 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    password
=value; 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
    
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 邮件发送优先级,可设置为"High","Normal","Low"或"1","3","5" 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  public string Priority 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
switch(value.ToLower()) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif     
case "high"
InBlock.gif      priority
="High"
InBlock.gif      
break
InBlock.gif
InBlock.gif     
case "1"
InBlock.gif      priority
="High"
InBlock.gif      
break
InBlock.gif
InBlock.gif     
case "normal"
InBlock.gif      priority
="Normal"
InBlock.gif      
break
InBlock.gif
InBlock.gif     
case "3"
InBlock.gif      priority
="Normal"
InBlock.gif      
break
InBlock.gif
InBlock.gif     
case "low"
InBlock.gif      priority
="Low"
InBlock.gif      
break
InBlock.gif
InBlock.gif     
case "5"
InBlock.gif      priority
="Low"
InBlock.gif      
break
InBlock.gif
InBlock.gif     
default
InBlock.gif      priority
="Normal"
InBlock.gif      
break
ExpandedSubBlockEnd.gif    }
 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 错误消息反馈 
ExpandedSubBlockEnd.gif  
/// </summary>       

InBlock.gif  public string ErrorMessage 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
return errmsg; 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 服务器交互记录 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private string logs=""
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 服务器交互记录,如发现本组件不能使用的SMTP服务器,请将出错时的Logs发给我(lion-a@sohu.com),我将尽快查明原因。 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  public string Logs 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
return logs; 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif  
InBlock.gif  
// 邮件标题
InBlock.gif
  public string MailSubject
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
set
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Subject
=value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
//邮件内容
InBlock.gif
  public string MailBody
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
set
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Body
=value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  
#endregion

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// SMTP回应代码哈希表 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private void SMTPCodeAdd() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   ErrCodeHT.Add(
"500","邮箱地址错误"); 
InBlock.gif   ErrCodeHT.Add(
"501","参数格式错误"); 
InBlock.gif   ErrCodeHT.Add(
"502","命令不可实现"); 
InBlock.gif   ErrCodeHT.Add(
"503","服务器需要SMTP验证"); 
InBlock.gif   ErrCodeHT.Add(
"504","命令参数不可实现"); 
InBlock.gif   ErrCodeHT.Add(
"421","服务未就绪,关闭传输信道"); 
InBlock.gif   ErrCodeHT.Add(
"450","要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)"); 
InBlock.gif   ErrCodeHT.Add(
"550","要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)"); 
InBlock.gif   ErrCodeHT.Add(
"451","放弃要求的操作;处理过程中出错"); 
InBlock.gif   ErrCodeHT.Add(
"551","用户非本地,请尝试<forward-path>"); 
InBlock.gif   ErrCodeHT.Add(
"452","系统存储不足,要求的操作未执行"); 
InBlock.gif   ErrCodeHT.Add(
"552","过量的存储分配,要求的操作未执行"); 
InBlock.gif   ErrCodeHT.Add(
"553","邮箱名不可用,要求的操作未执行(例如邮箱格式错误)"); 
InBlock.gif   ErrCodeHT.Add(
"432","需要一个密码转换"); 
InBlock.gif   ErrCodeHT.Add(
"534","认证机制过于简单"); 
InBlock.gif   ErrCodeHT.Add(
"538","当前请求的认证机制需要加密"); 
InBlock.gif   ErrCodeHT.Add(
"454","临时认证失败"); 
InBlock.gif   ErrCodeHT.Add(
"530","需要认证"); 
InBlock.gif
InBlock.gif   RightCodeHT.Add(
"220","服务就绪"); 
InBlock.gif   RightCodeHT.Add(
"250","要求的邮件操作完成"); 
InBlock.gif   RightCodeHT.Add(
"251","用户非本地,将转发向<forward-path>"); 
InBlock.gif   RightCodeHT.Add(
"354","开始邮件输入,以<enter>.<enter>结束"); 
InBlock.gif   RightCodeHT.Add(
"221","服务关闭传输信道"); 
InBlock.gif   RightCodeHT.Add(
"334","服务器响应验证Base64字符串"); 
InBlock.gif   RightCodeHT.Add(
"235","验证成功"); 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 将字符串编码为Base64字符串 
InBlock.gif  
/// </summary> 
ExpandedSubBlockEnd.gif  
/// <param name="estr">要编码的字符串</param> 

InBlock.gif  private string Base64Encode(string str) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
byte[] barray; 
InBlock.gif   barray
=Encoding.Default.GetBytes(str); 
InBlock.gif   
return Convert.ToBase64String(barray); 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 将Base64字符串解码为普通字符串 
InBlock.gif  
/// </summary> 
ExpandedSubBlockEnd.gif  
/// <param name="dstr">要解码的字符串</param> 

InBlock.gif  private string Base64Decode(string str) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
byte[] barray; 
InBlock.gif   barray
=Convert.FromBase64String(str); 
InBlock.gif   
return Encoding.Default.GetString(barray); 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif       
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 得到上传附件的文件流 
InBlock.gif  
/// </summary> 
ExpandedSubBlockEnd.gif  
/// <param name="FilePath">附件的绝对路径</param> 

InBlock.gif  private string GetStream(string FilePath) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
//建立文件流对象 
InBlock.gif
   System.IO.FileStream FileStr=new System.IO.FileStream(FilePath,System.IO.FileMode.Open); 
InBlock.gif   
byte[] by=new byte[System.Convert.ToInt32(FileStr.Length)]; 
InBlock.gif   FileStr.Read(by,
0,by.Length); 
InBlock.gif   FileStr.Close(); 
InBlock.gif   
return(System.Convert.ToBase64String(by)); 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 添加邮件附件 
InBlock.gif  
/// </summary> 
ExpandedSubBlockEnd.gif  
/// <param name="path">附件绝对路径</param> 

InBlock.gif  public void AddAttachment(string path) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   Attachments.Add(path); 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif       
InBlock.gif       
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 添加一个收件人 
InBlock.gif  
/// </summary>    
ExpandedSubBlockEnd.gif  
/// <param name="str">收件人地址</param> 

InBlock.gif  public bool AddRecipient(string str) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   str
=str.Trim(); 
InBlock.gif   
if(str==null||str==""||str.IndexOf("@")==-1
InBlock.gif    
return true
InBlock.gif   
if(RecipientNum<recipientmaxnum) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    Recipient.Add(RecipientNum,str); 
InBlock.gif    RecipientNum
++;             
InBlock.gif    
return true
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    errmsg
+="收件人过多"
InBlock.gif    
return false
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 最多收件人数量 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  public int RecipientMaxNum 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    recipientmaxnum 
= value; 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 添加一组收件人(不超过recipientmaxnum个),参数为字符串数组 
InBlock.gif  
/// </summary> 
ExpandedSubBlockEnd.gif  
/// <param name="str">保存有收件人地址的字符串数组(不超过recipientmaxnum个)</param>    

InBlock.gif  public bool AddRecipient(string[] str) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
for(int i=0;i<str.Length;i++
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
if(!AddRecipient(str[i])) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif     
return false
ExpandedSubBlockEnd.gif    }
 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
return true
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 发送SMTP命令 
ExpandedSubBlockEnd.gif  
/// </summary>    

InBlock.gif  private bool SendCommand(string str) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
byte[]  WriteBuffer; 
InBlock.gif   
if(str==null||str.Trim()==""
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
return true
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   logs
+=str; 
InBlock.gif   WriteBuffer 
= Encoding.Default.GetBytes(str); 
InBlock.gif   
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    ns.Write(WriteBuffer,
0,WriteBuffer.Length); 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    errmsg
="网络连接错误"
InBlock.gif    
return false
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
return true
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 接收SMTP服务器回应 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  private string RecvResponse() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
int StreamSize; 
InBlock.gif   
string ReturnValue = ""
InBlock.gif   
byte[]  ReadBuffer = new byte[1024] ; 
InBlock.gif   
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    StreamSize
=ns.Read(ReadBuffer,0,ReadBuffer.Length); 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
catch 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    errmsg
="网络连接错误"
InBlock.gif    
return "false"
ExpandedSubBlockEnd.gif   }
 
InBlock.gif
InBlock.gif   
if (StreamSize==0
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
return ReturnValue ; 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    ReturnValue 
= Encoding.Default.GetString(ReadBuffer).Substring(0,StreamSize); 
InBlock.gif    logs
+=ReturnValue; 
InBlock.gif    
return  ReturnValue; 
ExpandedSubBlockEnd.gif   }
 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 与服务器交互,发送一条命令并接收回应。 
InBlock.gif  
/// </summary> 
InBlock.gif  
/// <param name="Command">一个要发送的命令</param> 
ExpandedSubBlockEnd.gif  
/// <param name="errstr">如果错误,要反馈的信息</param> 

InBlock.gif  private bool Dialog(string str,string errstr) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
if(str==null||str.Trim()==""
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
return true
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
if(SendCommand(str)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
string RR=RecvResponse(); 
InBlock.gif    
if(RR=="false"
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif     
return false
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
string RRCode=RR.Substring(0,3); 
InBlock.gif    
if(RightCodeHT[RRCode]!=null
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif     
return true
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif     
if(ErrCodeHT[RRCode]!=null
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif
InBlock.gif      errmsg
+=(RRCode+ErrCodeHT[RRCode].ToString()); 
InBlock.gif      errmsg
+=enter; 
ExpandedSubBlockEnd.gif     }
 
InBlock.gif     
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif
InBlock.gif      errmsg
+=RR; 
ExpandedSubBlockEnd.gif     }
 
InBlock.gif     errmsg
+=errstr; 
InBlock.gif     
return false
ExpandedSubBlockEnd.gif    }
 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
return false
ExpandedSubBlockEnd.gif   }
 
InBlock.gif
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 与服务器交互,发送一组命令并接收回应。 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif
InBlock.gif  
private bool Dialog(string[] str,string errstr) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
for(int i=0;i<str.Length;i++
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
if(!Dialog(str[i],"")) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif     errmsg
+=enter; 
InBlock.gif     errmsg
+=errstr; 
InBlock.gif     
return false
ExpandedSubBlockEnd.gif    }
 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif
InBlock.gif   
return true
ExpandedSubBlockEnd.gif  }
 
InBlock.gif  
private bool SendEmail() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
//连接网络 
InBlock.gif
   try 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    tc
=new TcpClient(mailserver,mailserverport); 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
catch(Exception e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    errmsg
=e.ToString(); 
InBlock.gif    
return false
ExpandedSubBlockEnd.gif   }
 
InBlock.gif
InBlock.gif   ns 
= tc.GetStream(); 
InBlock.gif   SMTPCodeAdd(); 
InBlock.gif
InBlock.gif   
//验证网络连接是否正确 
InBlock.gif
   if(RightCodeHT[RecvResponse().Substring(0,3)]==null
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    errmsg
="网络连接失败"
InBlock.gif    
return false
ExpandedSubBlockEnd.gif   }
 
InBlock.gif
InBlock.gif
InBlock.gif   
string[] SendBuffer; 
InBlock.gif   
string SendBufferstr; 
InBlock.gif
InBlock.gif   
//进行SMTP验证 
InBlock.gif
   if(ESmtp) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    SendBuffer
=new String[4]; 
InBlock.gif    SendBuffer[
0]="EHLO " + mailserver + enter; 
InBlock.gif    SendBuffer[
1]="AUTH LOGIN" + enter; 
InBlock.gif    SendBuffer[
2]=Base64Encode(username) + enter; 
InBlock.gif    SendBuffer[
3]=Base64Encode(password) + enter; 
InBlock.gif    
if(!Dialog(SendBuffer,"SMTP服务器验证失败,请核对用户名和密码。")) 
InBlock.gif     
return false
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    SendBufferstr
="HELO " + mailserver + enter; 
InBlock.gif    
if(!Dialog(SendBufferstr,"")) 
InBlock.gif     
return false
ExpandedSubBlockEnd.gif   }
 
InBlock.gif
InBlock.gif   
// 
InBlock.gif
   SendBufferstr="MAIL FROM:<" + From + ">" + enter; 
InBlock.gif   
if(!Dialog(SendBufferstr,"发件人地址错误,或不能为空")) 
InBlock.gif    
return false
InBlock.gif
InBlock.gif   
// 
InBlock.gif
   SendBuffer=new string[recipientmaxnum]; 
InBlock.gif   
for(int i=0;i<Recipient.Count;i++
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif
InBlock.gif    SendBuffer[i]
="RCPT TO:<" + Recipient[i].ToString() +">" + enter; 
InBlock.gif
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
if(!Dialog(SendBuffer,"收件人地址有误")) 
InBlock.gif    
return false
InBlock.gif
InBlock.gif   SendBufferstr
="DATA" + enter; 
InBlock.gif   
if(!Dialog(SendBufferstr,"")) 
InBlock.gif    
return false
InBlock.gif
InBlock.gif   SendBufferstr
="From:" + FromName + "<" + From +">" +enter; 
InBlock.gif
InBlock.gif   SendBufferstr 
+= "To:=?"+Charset.ToUpper()+"?B?"+Base64Encode(RecipientName)+"?="+"<"+Recipient[0]+">"+enter; 
InBlock.gif   SendBufferstr
+="CC:"
InBlock.gif   
for(int i=0;i<Recipient.Count;i++
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    SendBufferstr
+=Recipient[i].ToString() + "<" + Recipient[i].ToString() +">,"
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   SendBufferstr
+=enter; 
InBlock.gif
InBlock.gif
InBlock.gif   
if(Charset==""
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    SendBufferstr
+="Subject:" + Subject + enter; 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    SendBufferstr
+="Subject:" + "=?" + Charset.ToUpper() + "?B?" + Base64Encode(Subject) +"?=" +enter; 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif
InBlock.gif   SendBufferstr
+="X-Priority:" + priority + enter; 
InBlock.gif   SendBufferstr
+="X-MSMail-Priority:" + priority + enter; 
InBlock.gif   SendBufferstr
+="Importance:" + priority + enter; 
InBlock.gif   SendBufferstr
+="X-Mailer: Huolx.Pubclass" + enter; 
InBlock.gif   SendBufferstr
+="MIME-Version: 1.0" + enter; 
InBlock.gif
InBlock.gif   SendBufferstr 
+= "Content-Type: multipart/mixed;"+enter;//内容格式和分隔符 
InBlock.gif
   SendBufferstr += "   boundary=\"----=_NextPart_000_00D6_01C29593.AAB31770\""+enter; 
InBlock.gif   SendBufferstr 
+= "------=_NextPart_000_00D6_01C29593.AAB31770"+enter; 
InBlock.gif
InBlock.gif   
if(Html) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    SendBufferstr
+="Content-Type: text/html;" + enter; 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    SendBufferstr
+="Content-Type: text/plain;" + enter; 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif
InBlock.gif   
if(Charset==""
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    SendBufferstr
+="   charset=\"iso-8859-1\"" + enter; 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    SendBufferstr
+="   charset=\"" + Charset.ToLower() + "\"" + enter; 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
//SendBufferstr += "Content-Transfer-Encoding: base64"+enter; 
InBlock.gif

InBlock.gif   SendBufferstr
+="Content-Transfer-Encoding: base64" + enter + enter; 
InBlock.gif
InBlock.gif   SendBufferstr
+= Base64Encode(Body) + enter; 
InBlock.gif   
if(Attachments.Count!=0
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    
foreach(string filepath in Attachments) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif                
InBlock.gif     SendBufferstr 
+= "------=_NextPart_000_00D6_01C29593.AAB31770"+enter; 
InBlock.gif     SendBufferstr 
+= "Content-Type: application/octet-stream"+enter; 
InBlock.gif     SendBufferstr 
+= "   name=\"=?"+Charset.ToUpper()+"?B?"+Base64Encode(filepath.Substring(filepath.LastIndexOf("\\")+1))+"?=\""+enter; 
InBlock.gif     SendBufferstr 
+= "Content-Transfer-Encoding: base64"+enter; 
InBlock.gif     SendBufferstr 
+= "Content-Disposition: attachment;"+enter; 
InBlock.gif     SendBufferstr 
+= "   filename=\"=?"+Charset.ToUpper()+"?B?"+Base64Encode(filepath.Substring(filepath.LastIndexOf("\\")+1))+"?=\""+enter+enter; 
InBlock.gif     SendBufferstr 
+= GetStream(filepath)+enter+enter; 
ExpandedSubBlockEnd.gif    }
 
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   SendBufferstr 
+= "------=_NextPart_000_00D6_01C29593.AAB31770--"+enter+enter; 
InBlock.gif          
InBlock.gif          
InBlock.gif   SendBufferstr 
+= enter + "." + enter; 
InBlock.gif
InBlock.gif   
if(!Dialog(SendBufferstr,"错误信件信息")) 
InBlock.gif    
return false
InBlock.gif
InBlock.gif
InBlock.gif   SendBufferstr
="QUIT" + enter; 
InBlock.gif   
if(!Dialog(SendBufferstr,"断开连接时错误")) 
InBlock.gif    
return false
InBlock.gif
InBlock.gif
InBlock.gif   ns.Close(); 
InBlock.gif   tc.Close(); 
InBlock.gif   
return true
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 发送邮件方法,所有参数均通过属性设置。 
ExpandedSubBlockEnd.gif  
/// </summary> 

InBlock.gif  public bool Send() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   
if(Recipient.Count==0
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    errmsg
="收件人列表不能为空"
InBlock.gif    
return false
ExpandedSubBlockEnd.gif   }
 
InBlock.gif
InBlock.gif   
if(mailserver.Trim()==""
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
InBlock.gif    errmsg
="必须指定SMTP服务器"
InBlock.gif    
return false
ExpandedSubBlockEnd.gif   }
 
InBlock.gif
InBlock.gif   
return SendEmail(); 
InBlock.gif          
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 发送邮件方法 
InBlock.gif  
/// </summary> 
ExpandedSubBlockEnd.gif  
/// <param name="smtpserver">smtp服务器信息,如"username:password@www.smtpserver.com:25",也可去掉部分次要信息,如" www.smtpserver.com"</param> 

InBlock.gif  public bool Send(string smtpserver) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif          
InBlock.gif   MailDomain
=smtpserver; 
InBlock.gif   
return Send(); 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary> 
InBlock.gif  
/// 发送邮件方法 
InBlock.gif  
/// </summary> 
InBlock.gif  
/// <param name="smtpserver">smtp服务器信息,如"username:password@www.smtpserver.com:25",也可去掉部分次要信息,如" www.smtpserver.com"</param> 
InBlock.gif  
/// <param name="from">发件人mail地址</param> 
InBlock.gif  
/// <param name="fromname">发件人姓名</param> 
InBlock.gif  
/// <param name="to">收件人地址</param> 
InBlock.gif  
/// <param name="toname">收件人姓名</param> 
InBlock.gif  
/// <param name="html">是否HTML邮件</param> 
InBlock.gif  
/// <param name="subject">邮件主题</param> 
ExpandedSubBlockEnd.gif  
/// <param name="body">邮件正文</param> 

InBlock.gif  public bool Send(string smtpserver,string from,string fromname,string to,string toname,bool html,string subject,string body) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif
InBlock.gif   MailDomain
=smtpserver; 
InBlock.gif   From
=from; 
InBlock.gif   FromName
=fromname; 
InBlock.gif   AddRecipient(to); 
InBlock.gif   RecipientName
=toname; 
InBlock.gif   Html
=html; 
InBlock.gif   Subject
=subject; 
InBlock.gif   Body
=body; 
InBlock.gif   
return Send(); 
ExpandedSubBlockEnd.gif  }
 
InBlock.gif       
InBlock.gif       
ExpandedSubBlockEnd.gif }
 
ExpandedBlockEnd.gif}
 
None.gif
None.gif

 

转载于:https://www.cnblogs.com/gamebaby/archive/2006/11/11/557639.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值