网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C/C++
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.C++箴言:用非成员非友元函数取代.
.关于 MD5 的一些知识.
.使用Microsoft Agent的COM接口编.
.二级指针.
.More Effective C++:类型转换.
.就c++中的const限定修饰符做一个.
.一个简单的口令程序!.
.《c语言程序设计》第一章: C语言.
.C程序开发经典实例之1.
.牛顿和拉格朗日插值算法.
.在Window右下角的添加图标.
.C++ 局部类和嵌套类.
.C语言图像处理方法.
.C++中几个比较不常用的关键字.
.利用.NET语言开发自己的脚本语言.
.修练8年C++面向对象程序设计之体.
.C++箴言:避免析构函数调用虚函数.
.C语言初级讲座之二.
.Visual Basic使用技巧.
.对C++递增(增量)运算符重载的思考.

发布源码:高效的Esmtp,带验证,用Socket编写

发表日期:2008-3-8


using System;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Configuration;
using System.Text;
using System.XML;
using System.IO;
using System.Web;
using System.Web.Mail;namespace mail
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 public class mSendMail
 {
  private TcpClient tcpClt;
  private NetworkStream networkStm;
  private Hashtable rightCodeHT = new Hashtable();
  private string smtpServerName;
  private int smtpServerPort;
  private string userName;
  private string passWord;
  private string to;
  private string from;
  private string fromName;
  private string charset;
  private string recipientName;
  private string subject;
  private string body;
  private string priority;
       
  static string Send_Method;
  
  public mSendMail()
  {
  
  }  public mSendMail(string strToName,string strTo,string strBody)
  {
   to = strTo;
   recipientName = strToName;
   body = strBody;
   smtpCodeAdd();
  }
  public  mSendMail(string strToName,string strTo, string strSubject, string strBody)
  {
   to = strTo;
   recipientName = strToName;
   subject = strSubject;
   body = strBody;
   smtpCodeAdd();
  }
  public  mSendMail(string strToName,string strTo,string strFromName,string strFrom, string strSubject, string strBody)
  {
   to = strTo;
   recipientName = strToName;
   from = strFrom;
   fromName = strFromName;
   subject = strSubject;
   body = strBody;
   smtpCodeAdd();
  }
  private bool initialize()
  {
   try
   {
    if(Send_Method=="1")
    {
     smtpServerName = ConfigurationSettings.AppSettings["smtpServerName"];
     smtpServerPort = Convert.ToInt32(ConfigurationSettings.AppSettings["smtpServerPort"]);
     userName = ConfigurationSettings.AppSettings["userName"];
     password = ConfigurationSettings.AppSettings["password"];
     //from = ConfigurationSettings.AppSettings["from"];
     //fromName = ConfigurationSettings.AppSettings["fromName"];
     charset = ConfigurationSettings.AppSettings["charset"];
    }
    else
    {
     smtpServerName ="";//your smtp server
     smtpServerPort =25;
     userName ="";//your name
     password ="";//your pass
     charset ="GB2312";
     //from = ConfigurationSettings.AppSettings["from"];
     //fromName = ConfigurationSettings.AppSettings["fromName"];
    }
    
    
   }
   catch
   {
    return false;
   }
   priority = "Normal";
   //subject = "//";
   //smtpCodeAdd();
   return true;
  }  private string Base64Encode(string str)
  {
   byte[] barray;
   barray=Encoding.Default.GetBytes(str);
   return Convert.ToBase64String(barray);
  }  private void smtpCodeAdd()
  {
   rightCodeHT.Add("220","");
   rightCodeHT.Add("250","");
   rightCodeHT.Add("251","");
   rightCodeHT.Add("354","");
   rightCodeHT.Add("221","");
   rightCodeHT.Add("334","");
   rightCodeHT.Add("235","");
  }  private bool sendCommand(string str)
  {
   byte[] writeBuffer;
   writeBuffer = Encoding.Default.GetBytes(str);
   try
   {
    networkStm.Write(writeBuffer, 0, writeBuffer.Length);
   }
   catch
   {
    return false;
   }
   return true;
  }  private bool isRight()
  {
   int streamSize;
   byte[] readBuffer = new byte[1024];
   string returnValue = "";
   try
   {
    streamSize = networkStm.Read(readBuffer, 0, readBuffer.Length);
   }
   catch
   {
    return false;
   }
   if (streamSize != 0)
    returnValue = Encoding.Default.GetString(readBuffer, 0, streamSize);
   if(rightCodeHT[returnValue.Substring(0,3)] == null)
    return false;
   return true;
  }  public bool sendMail()
  {
   if (!initialize())
    return false;
   try
   {
    tcpClt = new TcpClient(smtpServerName, smtpServerPort);
   }
   catch
   {
    return false;
   }
   networkStm = tcpClt.GetStream();
   if (!isRight())
    return false;
   string[] sendBuffer;
   string enter = "\r\n";   sendBuffer = new String[9];
   sendBuffer[0] = "EHLO " + smtpServerName + enter;
   sendBuffer[1] = "AUTH LOGIN" + enter;
   sendBuffer[2] = Base64Encode(userName) + enter;
   sendBuffer[3] = Base64Encode(password) + enter;
   sendBuffer[4] = "MAIL FROM:<" + from + ">" + enter;
   sendBuffer[5] = "RCPT TO:<" + to +">" + enter;
   sendBuffer[6] = "DATA" + enter;
   sendBuffer[7] = "From:" + fromName + "<" + from +">" + enter;
   sendBuffer[7] += "To:=?" + charset.ToUpper() + "?B?"
    + Base64Encode(recipientName) + "?=" + "<" + to + ">" + enter;
   sendBuffer[7] += "Subject:" + "=?" + charset.ToUpper() + "?B?"
    + Base64Encode(subject) + "?=" + enter;
   sendBuffer[7] += "X-Priority:" + priority + enter;
   sendBuffer[7] += "X-MSMail-Priority:" + priority + enter;
   sendBuffer[7] += "Importance:" + priority + enter;
   sendBuffer[7] += "X-Mailer: Huolx.Pubclass" + enter;
   sendBuffer[7] += "MIME-Version: 1.0" + enter;
   sendBuffer[7] += "Content-Type: multipart/mixed;" + enter;
   sendBuffer[7] += "   boundary=\"----=_NextPart_000_00D6_01C29593.AAB31770\"" + enter;
   sendBuffer[7] += "------=_NextPart_000_00D6_01C29593.AAB31770" + enter;
   sendBuffer[7] += "Content-Type: text/Html;" + enter;
   sendBuffer[7] += "   charset=\"" + charset.ToLower() + "\"" + enter;
   sendBuffer[7] += "Content-Transfer-Encoding: base64" + enter + enter;
   sendBuffer[7] += Base64Encode(body) + enter;
   sendBuffer[7] += "------=_NextPart_000_00D6_01C29593.AAB31770--" + enter + enter;
   sendBuffer[7] += enter + "." + enter;
   sendBuffer[8] = "QUIT" + enter;   int i;   for (i=0;i<sendBuffer.Length;i++)
   {
    if (!sendCommand(sendBuffer[i]))
     return false;
    if (!isRight())
     return false;
   }
   tcpClt.Close();
   networkStm.Close();   return true;
  }
  public int Send_Email(string From, string To,string FromName,string ToName,string Subject,string Body)
  {
      int IsSUCcess = 0;
   string s1=To;
   int ix;
   int iy;
   int iz;
   char split;
   split=',';
   string[] MailAddress;
   
   ix=To.LastIndexOf("@");
   iy=To.LastIndexOf(".");
   iz=To.LastIndexOf(",");   if (ix>0 && iy>0 && iy>ix)
   {
    if (iz>0)
    {  
     MailAddress=s1.Split(split);
     for(int i=0;i<MailAddress.Length;i++)
     {
      ix=MailAddress[i].LastIndexOf("@");
      if (MailAddress[i].Substring(ix+1)=="sina.com")
      {Send_Method="1";}
      else{Send_Method="0";}
          
      mSendMail mySendMail = new mSendMail(ToName,MailAddress[i],FromName,From,Subject,Body);
      try
      {
       if (mySendMail.sendMail()== true)
        IsSuccess = 0;
      }
      catch
      {
           
      }
          
     }
    }
    else
    {
     if (s1.Substring(ix+1)=="sina.com")
     {Send_Method="1";}
     else{Send_Method="0";}
       
     mSendMail mySendMail = new mSendMail(ToName,To,FromName,From,Subject,Body);
     try
     {
      if (mySendMail.sendMail()== true)
       IsSuccess = 0;
     }
     catch
     {}
     
    }
   }
   else{IsSuccess=2;}
   return IsSuccess;
  }
  public int Send_TuiJian(string From, string To,string FromName,string ToName,string Title,string NewsAddr,string Message)
  {
   //读取邮件内容
   string MessagePath;
   if(System.Configuration.ConfigurationSettings.AppSettings["MessagePath"] != null)
    MessagePath = System.Configuration.ConfigurationSettings.AppSettings["MessagePath"].ToString();
   else
    MessagePath = @"D:\abc.htm";
   string strTemplate;
   
   StreamReader  stream = new StreamReader(MessagePath,System.Text.Encoding.GetEncoding("GB2312"));      
   try
   {
    
    stream.BaseStream.Seek(0,SeekOrigin.Begin);
    strTemplate = stream.ReadToEnd();
    strTemplate.Replace("\"","'");
   }
   finally
   {
    stream.Close();
   }   //替换
   string tmpMessage = Message;
   try
   {
    for (int i=0; i<=(Message.Length/35); i++)
    {
     tmpMessage = tmpMessage.Insert((i+1)*35,"<br>");
    }
   }
   catch
   {
   }
   Message = tmpMessage;
   Message = Message + "<br>";
   strTemplate = strTemplate.Insert(strTemplate.LastIndexOf("此致,礼"),Message);
   strTemplate = strTemplate.Replace("aa",ToName);
   strTemplate = strTemplate.Replace("bb",FromName);
   strTemplate = strTemplate.Replace("cc",Title);
   strTemplate = strTemplate.Replace(@"dd",NewsAddr);
   strTemplate = strTemplate.Replace("1980年",DateTime.Now.ToShortDateString());   //发送邮件
   int IsSuccess = 0;
   string Subject = "想请你去看看";
   
   //邮件地址判定
   string s1=To;
   int ix;
   int iy;
   int iz;
   char split;
   split=',';
   string[] MailAddress;
   
   ix=To.LastIndexOf("@");
   iy=To.LastIndexOf(".");
   iz=To.LastIndexOf(",");
   if (ix>0 && iy>0 && iy>ix)
   {
    if (iz>0)
    {  
     MailAddress=s1.Split(split);
     for(int i=0;i<MailAddress.Length;i++)
     {
      ix=MailAddress[i].LastIndexOf("@");
      if (MailAddress[i].Substring(ix+1)=="sina.com")
      {Send_Method="1";}
      else{Send_Method="0";}
          
      mSendMail mySendMail = new mSendMail(ToName,MailAddress[i],FromName,From,Subject,strTemplate);
      try
      {
       if (mySendMail.sendMail()== true)
        IsSuccess = 0;
      }
      catch
      {
           
      }
          
     }
    }
    else
    {
     if (s1.Substring(ix+1)=="sina.com")
     {Send_Method="1";}
     else{Send_Method="0";}
       
     mSendMail mySendMail = new mSendMail(ToName,To,FromName,From,Subject,strTemplate);
     try
     {
      if (mySendMail.sendMail()== true)
       IsSuccess = 0;
     }
     catch
     {}
     
    }
   }
   else{IsSuccess=2;}
   return IsSuccess;  }
 }}

上一篇:Snake.Net中的ORM(三) 人气:500
下一篇:.net中判断该应用程序是否已经启动,防止重复启动 人气:568
浏览全部C/C++的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐