当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  用ASP.Net发送MailList(3)

 用ASP.Net发送MailList(3)

点击次数:24 次 发布日期:2008-11-26 14:37:58 作者:源代码网
源代码网推荐      前面我们讲过如何利用ASP.net 来发送Mail,其实这篇文章的实际目的压根就不是为了让大家利用ASP.NET来简单的发送一封Mail,那样是没有任何作用的,因为Asp.Net 已经提供了这样的强大的功能我们文章的标题就是MailList,所以我们的任务就是实现用Asp.Net发送一个MailList,前面的那位同志说了,我们不需要这么麻烦,只要作个循环即可,不错,这个的确是一个办法,而且豆腐以前也一直是这样在做的,但是这样作的缺点就是浪费了大量的流路资源,和 Mellissa又有什么本质的区别呢?其实在SMTP(Simple Mail Transfer Protcol)已经提供了制作SMTP 的方法,当我们Connect 以后,我们可以不断的向服务器发送RCPT 命令,这样就可以利用一个Connect向多个mail Addr 发送Mail 的Data 内容,在这里我们一定要注意到我们的Data 内容仅仅是发送一次的,这样就节省了大量的网路资源。
源代码网推荐  
源代码网推荐    <%@ Assembly Name="System.Net" %>
源代码网推荐  
源代码网推荐    <%@ Import Namespace="System.Net" %>
源代码网推荐  
源代码网推荐    <% @Import Namespace="System.Net.Sockets" %>
源代码网推荐  
源代码网推荐    <%@ Import Namespace="System.IO" %>
源代码网推荐  
源代码网推荐    <SCRIPT language=C# runat="server">
源代码网推荐  
源代码网推荐     protected void SendMailClick(Object Src, EventArgs E){
源代码网推荐  
源代码网推荐     String smtpserver=txtSmtpserver.Text; file://smtp服务器的IP地址
源代码网推荐  
源代码网推荐     TCPClient tcpc = new TCPClient();
源代码网推荐  
源代码网推荐     if (0 == tcpc.Connect(smtpserver, txtPort.Text.ToInt16()))
源代码网推荐  
源代码网推荐      {
源代码网推荐  
源代码网推荐       file://连接smtp 服务器成功
源代码网推荐  
源代码网推荐       Stream s;
源代码网推荐  
源代码网推荐       StreamReader sr ;
源代码网推荐  
源代码网推荐       String strCmd;
源代码网推荐  
源代码网推荐       Byte[] arrCmd;
源代码网推荐  
源代码网推荐       String strRet;
源代码网推荐  
源代码网推荐       String[] arrRet;
源代码网推荐  
源代码网推荐       sr = new StreamReader(tcpc.GetStream(), Encoding.Default);
源代码网推荐  
源代码网推荐       strRet=sr.ReadLine() + "
源代码网推荐  "; file://mail server 的欢迎语
源代码网推荐  
源代码网推荐       file://服务器连接成功以后,首先向服务器发送HeLo命令
源代码网推荐  
源代码网推荐       strCmd="HELO www.asp888.net";
源代码网推荐  
源代码网推荐       strRet=strRet + SenSmtpCmd(tcpc,strCmd) + "<BR>";
源代码网推荐  
源代码网推荐       file://然后向服务器发送信件的成员的信箱
源代码网推荐  
源代码网推荐       strCmd="mail from:"+ sender.Text;
源代码网推荐  
源代码网推荐       strRet=strRet + SenSmtpCmd(tcpc,strCmd) + "<BR>";
源代码网推荐  
源代码网推荐       file://向服务器发送收件人的信箱
源代码网推荐  
源代码网推荐       strCmd="rcpt to:" + receive2.Text;
源代码网推荐  
源代码网推荐       strRet=strRet + SenSmtpCmd(tcpc,strCmd) + "<BR>";
源代码网推荐  
源代码网推荐       file://邮件列表的发送工作,其实就是打开一次Socket连接,发送多个mail的结果
源代码网推荐  
源代码网推荐       strCmd="rcpt to:" + receive1.Text;
源代码网推荐  
源代码网推荐       strRet=strRet + SenSmtpCmd(tcpc,strCmd) + "<BR>";
源代码网推荐  
源代码网推荐       file://所有的准备工作都已经作好了,下面开始进行邮件的部分
源代码网推荐  
源代码网推荐       strCmd="data";
源代码网推荐  
源代码网推荐       strRet=strRet + SenSmtpCmd(tcpc,strCmd) + "<BR>";
源代码网推荐  
源代码网推荐       file://邮件内容
源代码网推荐  
源代码网推荐       strCmd="Date: 1234567 ";
源代码网推荐  
源代码网推荐       strCmd=strCmd+"From:" + sender.Text +" ";
源代码网推荐  
源代码网推荐       strCmd=strCmd+"To:" + receive1.Text + ";" + receive2.Text +" ";
源代码网推荐  
源代码网推荐       file://strCmd=strCmd+"To:" + receive1.Text + " ";
源代码网推荐  
源代码网推荐       strCmd=strCmd+"Subject:" + subject.Text +" ";
源代码网推荐  
源代码网推荐       strCmd=strCmd + content.Text +" ";
源代码网推荐  
源代码网推荐       strRet=strRet + SenSmtpCmd1(tcpc,strCmd) + "<BR>";
源代码网推荐  
源代码网推荐       strCmd=" . ";
源代码网推荐  
源代码网推荐       strRet=strRet + SenSmtpCmd(tcpc,strCmd) + "<BR>";
源代码网推荐  
源代码网推荐       /*strCmd=strCmd+"From:xuexh@haitai.com.cm ";
源代码网推荐  
源代码网推荐       strCmd=strCmd+"To:xuexh@haitai.com.cn ";
源代码网推荐  
源代码网推荐      strCmd=strCmd+"subject:这是一个测试的邮件 ";
源代码网推荐  
源代码网推荐       strCmd=strCmd+" ";
源代码网推荐  
源代码网推荐       strCmd=strCmd+"这些是测试邮件的邮件内容 ";
源代码网推荐  
源代码网推荐       strCmd=strCmd+" . ";
源代码网推荐  
源代码网推荐       *///strRet=strRet + SenSmtpCmd(tcpc,strCmd) + "<BR>";
源代码网推荐  
源代码网推荐       file://最后 关闭与smtp 服务器的连接
源代码网推荐  
源代码网推荐       tcpc.Close();
源代码网推荐  
源代码网推荐       showmsg.Text=strRet;
源代码网推荐  
源代码网推荐      }
源代码网推荐  
源代码网推荐     else
源代码网推荐  
源代码网推荐      {
源代码网推荐  
源代码网推荐       showmsg.Text="对不起,连接服务器失败";
源代码网推荐  
源代码网推荐      }
源代码网推荐  
源代码网推荐     }
源代码网推荐  
源代码网推荐     String SenSmtpCmd(TCPClient tcpc,String strCmd){
源代码网推荐  
源代码网推荐      file://为了程序的简单可读,特意写了这个函数
源代码网推荐  
源代码网推荐      Byte[] arrCmd;
源代码网推荐  
源代码网推荐      String strRet;
源代码网推荐  
源代码网推荐      StreamReader sr;
源代码网推荐  
源代码网推荐      Stream s;
源代码网推荐  
源代码网推荐      s=tcpc.GetStream();
源代码网推荐  
源代码网推荐      strCmd = strCmd + " ";
源代码网推荐  
源代码网推荐      arrCmd= Encoding.Default.GetBytes(strCmd.ToCharArray());
源代码网推荐  
源代码网推荐      s=tcpc.GetStream();
源代码网推荐  
源代码网推荐      s.Write(arrCmd, 0, strCmd.Length);
源代码网推荐  
源代码网推荐      sr = new StreamReader(tcpc.GetStream(), Encoding.Default);
源代码网推荐  
源代码网推荐      strRet=sr.ReadLine();
源代码网推荐  
源代码网推荐      return strRet;
源代码网推荐  
源代码网推荐     }
源代码网推荐  
源代码网推荐     String SenSmtpCmd1(TCPClient tcpc,String strCmd){
源代码网推荐  
源代码网推荐      file://为了程序的简单可读,特意写了这个函数
源代码网推荐  
源代码网推荐      Byte[] arrCmd;
源代码网推荐  
源代码网推荐      String strRet;
源代码网推荐  
源代码网推荐      StreamReader sr;
源代码网推荐  
源代码网推荐      Stream s;
源代码网推荐  
源代码网推荐      s=tcpc.GetStream();
源代码网推荐  
源代码网推荐      strCmd = strCmd;
源代码网推荐  
源代码网推荐      arrCmd= Encoding.Default.GetBytes(strCmd.ToCharArray());
源代码网推荐  
源代码网推荐      s=tcpc.GetStream();
源代码网推荐  
源代码网推荐      s.Write(arrCmd, 0, strCmd.Length);
源代码网推荐  
源代码网推荐      return "";
源代码网推荐  
源代码网推荐      }
源代码网推荐  
源代码网推荐    
源代码网推荐  
源代码网推荐     <FORM id=testForm runat="server">
源代码网推荐  
源代码网推荐    <?XML:namespace prefix = asp /><asp:label id=showmsg runat="server"></asp:label>
源代码网推荐  
源代码网推荐     SMTP服务器:<asp:TextBox id=txtSmtpserver runat="server"></asp:TextBox><asp:TextBox id=txtPort runat="server" Text="25"></asp:TextBox>
源代码网推荐  
源代码网推荐     发信人:<asp:TextBox id=sender runat="server"></asp:TextBox>
源代码网推荐  
源代码网推荐     收信人1:<asp:TextBox id=receive1 runat="server"></asp:TextBox>
源代码网推荐  
源代码网推荐     收信人2:<asp:TextBox id=receive2 runat="server"></asp:TextBox>
源代码网推荐  
源代码网推荐     信件主题:<asp:TextBox id=subject runat="server"></asp:TextBox>
源代码网推荐  
源代码网推荐     信件内容:<asp:TextBox id=content runat="server" TextMode="MultiLine" rows="10/">
源代码网推荐  
源代码网推荐     <asp:Button id=SendMail onclick=SendMailClick runat="server" Text="发送"></asp:Button>
源代码网推荐  
源代码网推荐     </FORM>
源代码网推荐  
源代码网推荐   
源代码网推荐  </asp:TextBox>
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华