ASP.NET 实现邮件发送和接受的功能(Sockets)

简介:

前台代码:html

ExpandedBlockStart.gif <% @ Page language="c#" Codebehind="SocketMail.aspx.cs" AutoEventWireup="false" Inherits="UseFramework2.MailSys.Mail"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > Mail </ title >
None.gif        
< meta  content ="Microsoft Visual Studio 7.0"  name ="GENERATOR" >
None.gif        
< meta  content ="C#"  name ="CODE_LANGUAGE" >
None.gif        
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
None.gif        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Mail"  method ="post"  runat ="server" >
None.gif            
< FONT  face ="宋体" >
None.gif                
< asp:TextBox  id ="tbServer"  style ="Z-INDEX: 101; LEFT: 143px; POSITION: absolute; TOP: 104px"  runat ="server" ></ asp:TextBox >
None.gif                
< asp:TextBox  id ="tbSend"  style ="Z-INDEX: 120; LEFT: 413px; POSITION: absolute; TOP: 223px"  runat ="server" ></ asp:TextBox >
None.gif                
< asp:Label  id ="Label10"  style ="Z-INDEX: 119; LEFT: 334px; POSITION: absolute; TOP: 231px"  runat ="server" > 发件人 </ asp:Label >
None.gif                
< asp:Button  id ="btnSend"  style ="Z-INDEX: 118; LEFT: 441px; POSITION: absolute; TOP: 325px"  runat ="server"  Width ="76"  Text ="发送"  Height ="25" ></ asp:Button >
None.gif                
< asp:Label  id ="Label9"  style ="Z-INDEX: 117; LEFT: 332px; POSITION: absolute; TOP: 179px"  runat ="server" > 收件人 </ asp:Label >
None.gif                
< asp:Label  id ="Label8"  style ="Z-INDEX: 116; LEFT: 329px; POSITION: absolute; TOP: 278px"  runat ="server" > 邮件主题 </ asp:Label >
None.gif                
< asp:Label  id ="Label7"  style ="Z-INDEX: 115; LEFT: 320px; POSITION: absolute; TOP: 102px"  runat ="server" > SMTP服务器 </ asp:Label >
None.gif                
< asp:TextBox  id ="tbSubject"  style ="Z-INDEX: 114; LEFT: 415px; POSITION: absolute; TOP: 270px"  runat ="server" ></ asp:TextBox >
None.gif                
< asp:TextBox  id ="tbReceive"  style ="Z-INDEX: 113; LEFT: 416px; POSITION: absolute; TOP: 171px"  runat ="server" ></ asp:TextBox >
None.gif                
< asp:TextBox  id ="tbSmtp"  style ="Z-INDEX: 112; LEFT: 419px; POSITION: absolute; TOP: 100px"  runat ="server" ></ asp:TextBox >
None.gif                
< asp:Label  id ="Label5"  style ="Z-INDEX: 109; LEFT: 54px; POSITION: absolute; TOP: 245px"  runat ="server" > 密码 </ asp:Label >
None.gif                
< asp:Label  id ="Label4"  style ="Z-INDEX: 108; LEFT: 54px; POSITION: absolute; TOP: 181px"  runat ="server" > 用户名称 </ asp:Label >
None.gif                
< asp:label  id ="Label1"  style ="Z-INDEX: 106; LEFT: 414px; POSITION: absolute; TOP: 18px"  runat ="server"  Width ="175px"  Font-Names ="隶书"  Font-Size ="XX-Large" > 发邮件 </ asp:label >
None.gif                
< asp:label  id ="Label2"  style ="Z-INDEX: 105; LEFT: 85px; POSITION: absolute; TOP: 14px"  runat ="server"  Width ="175px"  Font-Names ="隶书"  Font-Size ="XX-Large" > 收邮件 </ asp:label >
None.gif                
< asp:TextBox  id ="tbUser"  style ="Z-INDEX: 102; LEFT: 143px; POSITION: absolute; TOP: 182px"  runat ="server" ></ asp:TextBox >
None.gif                
< asp:TextBox  id ="tbPass"  style ="Z-INDEX: 103; LEFT: 143px; POSITION: absolute; TOP: 242px"  runat ="server"  TextMode ="Password" ></ asp:TextBox >
None.gif                
< asp:TextBox  id ="tbBody"  style ="Z-INDEX: 104; LEFT: 141px; POSITION: absolute; TOP: 363px"  runat ="server"  Width ="458px"  Height ="87px" ></ asp:TextBox >
None.gif                
< asp:Label  id ="Label3"  style ="Z-INDEX: 107; LEFT: 54px; POSITION: absolute; TOP: 103px"  runat ="server" > POP3服务器 </ asp:Label >
None.gif                
< asp:Button  id ="btnCheck"  style ="Z-INDEX: 110; LEFT: 67px; POSITION: absolute; TOP: 315px"  runat ="server"  Width ="76"  Text ="检查"  Height ="25" ></ asp:Button >
None.gif                
< asp:Label  id ="Label6"  style ="Z-INDEX: 111; LEFT: 60px; POSITION: absolute; TOP: 387px"  runat ="server"  Width ="71px"  Height ="27px" > 邮件正文 </ asp:Label >
None.gif                
< asp:DropDownList  id ="ddlNew"  style ="Z-INDEX: 121; LEFT: 200px; POSITION: absolute; TOP: 319px"  runat ="server"  AutoPostBack ="True" ></ asp:DropDownList ></ FONT ></ form >
None.gif    
</ body >
None.gif
</ HTML >

后台代码:cs
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Net.Sockets;
None.gif
using  System.Net;
None.gif
using  System.IO;
None.gif
using  System.Web.Mail;
None.gif
using  System.Text;
None.gif
None.gif
namespace  UseFramework2.MailSys
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif    
/// <summary>
InBlock.gif    
/// Mail 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Mail : System.Web.UI.Page
ExpandedSubBlockStart.gif    
{
InBlock.gif        
protected System.Web.UI.WebControls.TextBox tbServer;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox tbUser;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox tbPass;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label2;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label1;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label3;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label4;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label5;
InBlock.gif        
protected System.Web.UI.WebControls.Button btnCheck;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label6;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label7;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox tbSmtp;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox tbReceive;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox tbSubject;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label8;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label9;
InBlock.gif        
protected System.Web.UI.WebControls.Button btnSend;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label10;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox tbSend;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox tbBody;
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList ddlNew;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
// 在此处放置用户代码以初始化页面
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ContractedSubBlock.gif        
Web Form Designer generated code
InBlock.gif
InBlock.gif        
private void btnCheck_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            POP3 pop 
= new POP3(tbServer.Text,tbUser.Text,tbPass.Text);
InBlock.gif            
int n = pop.GetNumberOfNewMessages();
InBlock.gif            
if(n==-1)
ExpandedSubBlockStart.gif            
{
InBlock.gif                Response.Write(
"<script language='javascript'>alert('服务器连接错误!')</script>");
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            ddlNew.Items.Clear();
InBlock.gif            
for(int i=1;i<=n;i++)
InBlock.gif                ddlNew.Items.Add(
""+i.ToString()+"封邮件");
InBlock.gif            
if(n>0)
ExpandedSubBlockStart.gif            
{
InBlock.gif                MailMessage msg 
=  pop.GetNewMessages(0);
InBlock.gif                
if(msg!=null)
InBlock.gif                    tbBody.Text 
= msg.Body;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ddlNew_SelectedIndexChanged(object sender, System.EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            POP3 pop 
= new POP3(tbServer.Text,tbUser.Text,tbPass.Text);
InBlock.gif            
int n = ddlNew.SelectedIndex;
InBlock.gif            
if(n>=0)
ExpandedSubBlockStart.gif            
{
InBlock.gif                MailMessage msg 
=  pop.GetNewMessages(n);
InBlock.gif                
if(msg!=null)
InBlock.gif                    tbBody.Text 
= msg.Body;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btnSend_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            SMTP smtp 
= new SMTP();
InBlock.gif            
bool bSuccess = smtp.Send(tbSmtp.Text,25,tbSend.Text,tbReceive.Text,tbSubject.Text,tbBody.Text);
InBlock.gif            
if(bSuccess)
InBlock.gif                Response.Write(
"<script language='javascript'>alert('邮件发送成功!')</script>");
InBlock.gif            
else
InBlock.gif                Response.Write(
"<script language='javascript'>alert('邮件发送失败!')</script>");
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public class POP3
ExpandedSubBlockStart.gif    
{
InBlock.gif        
string POPServer;
InBlock.gif        
string user;
InBlock.gif        
string pwd;
InBlock.gif        NetworkStream ns;
InBlock.gif        StreamReader sr;
InBlock.gif
ExpandedSubBlockStart.gif        
public POP3(){}
InBlock.gif
InBlock.gif        
public POP3(string server, string _user, string _pwd)
ExpandedSubBlockStart.gif        
{
InBlock.gif            POPServer 
= server;
InBlock.gif            user 
= _user;
InBlock.gif            pwd 
= _pwd;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private bool Connect()
ExpandedSubBlockStart.gif        
{
InBlock.gif            TcpClient sender 
= new TcpClient(POPServer,110);
InBlock.gif            
byte[] outbytes;
InBlock.gif            
string input;
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                ns 
= sender.GetStream();
InBlock.gif                sr 
= new StreamReader(ns);
InBlock.gif
InBlock.gif                sr.ReadLine();
InBlock.gif                input 
= "user " + user + "\r\n";
InBlock.gif                outbytes 
= System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
InBlock.gif                ns.Write(outbytes,
0,outbytes.Length) ;
InBlock.gif                sr.ReadLine();
InBlock.gif            
InBlock.gif                input 
= "pass " + pwd + "\r\n";
InBlock.gif                outbytes 
= System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
InBlock.gif                ns.Write(outbytes,
0,outbytes.Length) ;
InBlock.gif                sr.ReadLine();
InBlock.gif                
return true;  
InBlock.gif        
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gif            
{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Disconnect()
ExpandedSubBlockStart.gif        
{
InBlock.gif            
string input = "quit" + "\r\n";
InBlock.gif            Byte[] outbytes 
= System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
InBlock.gif            ns.Write(outbytes,
0,outbytes.Length);
InBlock.gif            ns.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int GetNumberOfNewMessages()
ExpandedSubBlockStart.gif        
{
InBlock.gif            
byte[] outbytes;
InBlock.gif            
string input;
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                Connect();
InBlock.gif
InBlock.gif                input 
= "stat" + "\r\n";
InBlock.gif                outbytes 
= System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
InBlock.gif                ns.Write(outbytes,
0,outbytes.Length);
InBlock.gif                
string resp = sr.ReadLine();
ExpandedSubBlockStart.gif                
string[] tokens = resp.Split(new Char[] {' '});
InBlock.gif
InBlock.gif                Disconnect();
InBlock.gif
InBlock.gif                
return Convert.ToInt32(tokens[1]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gif            
{
InBlock.gif                
return -1;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public ArrayList GetNewMessages(string subj)
ExpandedSubBlockStart.gif        
{
InBlock.gif
InBlock.gif            
int newcount;
InBlock.gif            ArrayList newmsgs 
= new ArrayList();
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                newcount 
= GetNumberOfNewMessages();
InBlock.gif                Connect();
InBlock.gif
InBlock.gif                
for(int n=1; n<newcount+1; n++)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    ArrayList msglines 
= GetRawMessage(n);
InBlock.gif                    
string msgsubj = GetMessageSubject(msglines);
InBlock.gif                    
if(msgsubj.CompareTo(subj) == 0)
ExpandedSubBlockStart.gif                    
{
InBlock.gif                        System.Web.Mail.MailMessage msg 
= new MailMessage();
InBlock.gif                        msg.Subject 
= msgsubj;
InBlock.gif                        msg.From 
= GetMessageFrom(msglines);
InBlock.gif                        msg.Body 
= GetMessageBody(msglines);
InBlock.gif                        newmsgs.Add(msg);
InBlock.gif                        DeleteMessage(n);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                Disconnect();
InBlock.gif                
return newmsgs;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gif            
{
InBlock.gif                
return newmsgs;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public MailMessage GetNewMessages(int nIndex)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
int newcount;
InBlock.gif            System.Web.Mail.MailMessage msg 
= new MailMessage();
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                newcount 
= GetNumberOfNewMessages();
InBlock.gif                Connect();
InBlock.gif                
int n = nIndex+1;
InBlock.gif
InBlock.gif                
if(n<newcount+1)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    ArrayList msglines 
= GetRawMessage(n);
InBlock.gif                    
string msgsubj = GetMessageSubject(msglines);
InBlock.gif                
InBlock.gif                    
InBlock.gif                    msg.Subject 
= msgsubj;
InBlock.gif                    msg.From 
= GetMessageFrom(msglines);
InBlock.gif                    msg.Body 
= GetMessageBody(msglines);
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                Disconnect();
InBlock.gif                
return msg;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gif            
{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private ArrayList GetRawMessage (int messagenumber)
ExpandedSubBlockStart.gif        
{
InBlock.gif            Byte[] outbytes;
InBlock.gif            
string input;
InBlock.gif            
string line = "";
InBlock.gif
InBlock.gif            input 
= "retr " + messagenumber.ToString() + "\r\n";
InBlock.gif            outbytes 
= System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
InBlock.gif            ns.Write(outbytes,
0,outbytes.Length);
InBlock.gif
InBlock.gif            ArrayList msglines 
= new ArrayList();
InBlock.gif            
do
ExpandedSubBlockStart.gif            
{
InBlock.gif                line 
= sr.ReadLine();
InBlock.gif                msglines.Add(line);
ExpandedSubBlockEnd.gif            }
 while (line != ".");
InBlock.gif            msglines.RemoveAt(msglines.Count
-1);
InBlock.gif
InBlock.gif            
return msglines;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string GetMessageSubject(ArrayList msglines)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
string[] tokens;
InBlock.gif            IEnumerator msgenum 
= msglines.GetEnumerator();
InBlock.gif            
while (msgenum.MoveNext() )
ExpandedSubBlockStart.gif            
{
InBlock.gif                
string line = (string)msgenum.Current;
InBlock.gif                
if(line.StartsWith("Subject:") )
ExpandedSubBlockStart.gif                
{
ExpandedSubBlockStart.gif                    tokens 
= line.Split(new Char[] {' '});
InBlock.gif                    
return tokens[1].Trim();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return "None";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string GetMessageFrom (ArrayList msglines)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
string[] tokens;
InBlock.gif            IEnumerator msgenum 
= msglines.GetEnumerator();
InBlock.gif            
while (msgenum.MoveNext() )
ExpandedSubBlockStart.gif            
{
InBlock.gif                
string line = (string)msgenum.Current;
InBlock.gif                
if(line.StartsWith("From:") )
ExpandedSubBlockStart.gif                
{
ExpandedSubBlockStart.gif                    tokens 
= line.Split(new Char[] {'<'});
ExpandedSubBlockStart.gif                    
return tokens[1].Trim(new Char[] {'<','>'});
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return "None";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string GetMessageBody(ArrayList msglines)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
string body = "";
InBlock.gif            
string line = " ";
InBlock.gif            IEnumerator msgenum 
= msglines.GetEnumerator();
InBlock.gif
InBlock.gif            
while(line.CompareTo(""!= 0)
ExpandedSubBlockStart.gif            
{
InBlock.gif                msgenum.MoveNext();
InBlock.gif                line 
= (string)msgenum.Current;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
while (msgenum.MoveNext() )
ExpandedSubBlockStart.gif            
{
InBlock.gif                body 
= body + (string)msgenum.Current + "\r\n";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return body;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void DeleteMessage(int messagenumber)
ExpandedSubBlockStart.gif        
{
InBlock.gif            Byte[] outbytes;
InBlock.gif            
string input;
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                input 
= "dele " + messagenumber.ToString() + "\r\n";
InBlock.gif                outbytes 
= System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
InBlock.gif                ns.Write(outbytes,
0,outbytes.Length);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gif            
{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public class SMTP
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
public SMTP(){}
InBlock.gif
InBlock.gif        
public bool Send(string strSmtpServer,int nPort,string strSend,string strReceive
InBlock.gif            ,
string strSubject,string strContent)
ExpandedSubBlockStart.gif        
{
InBlock.gif              
InBlock.gif            
string smtpserver=strSmtpServer;// smtp服务器的IP地址
InBlock.gif
            TcpClient tcpc = new TcpClient();
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                tcpc.Connect(smtpserver, nPort);
InBlock.gif                StreamReader sr ;
InBlock.gif                
string strCmd;
InBlock.gif                sr 
= new StreamReader(tcpc.GetStream(),Encoding.Default);
InBlock.gif                
//服务器连接成功以后,首先向服务器发送HeLlo命令
InBlock.gif
                strCmd="HELO shaozhd";
InBlock.gif                SenSmtpCmd(tcpc,strCmd);
InBlock.gif                
//然后向服务器发送信件的成员的信箱
InBlock.gif
                strCmd="mail from:"+ strSend;
InBlock.gif                SenSmtpCmd(tcpc,strCmd);
InBlock.gif                
//向服务器发送收件人的信箱
InBlock.gif
                strCmd="rcpt to:" + strReceive;
InBlock.gif                SenSmtpCmd(tcpc,strCmd);
InBlock.gif                
//所有的准备工作都已经作好了,下面开始进行邮件的部分
InBlock.gif
                strCmd="data";
InBlock.gif                SenSmtpCmd(tcpc,strCmd);
InBlock.gif                
//邮件内容
InBlock.gif
                strCmd="Date: 1234567\r\n";
InBlock.gif                strCmd
=strCmd+"From: " + strSend +"\r\n";
InBlock.gif                strCmd
=strCmd+"To: " + strReceive +"\r\n";
InBlock.gif                strCmd
=strCmd+"Subject: " + strSubject +"\r\n\r\n";
InBlock.gif                strCmd
=strCmd + strContent +"\r\n\r\n";
InBlock.gif                SenSmtpCmd(tcpc,strCmd);
InBlock.gif                strCmd
="\r\n.\r\n";
InBlock.gif                SenSmtpCmd(tcpc,strCmd);
InBlock.gif                
//最后 关闭与smtp 服务器的连接
InBlock.gif
          tcpc.Close();
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif             }

InBlock.gif            
catch
ExpandedSubBlockStart.gif            
{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
void SenSmtpCmd(TcpClient tcpc,String strCmd)
ExpandedSubBlockStart.gif        
{
InBlock.gif
InBlock.gif         
byte[] arrCmd;
InBlock.gif          
string strRet;
InBlock.gif          StreamReader sr;
InBlock.gif        Stream s;
InBlock.gif        s
=tcpc.GetStream();
InBlock.gif        strCmd 
= strCmd + "\r\n";
InBlock.gif        arrCmd
= Encoding.Default.GetBytes(strCmd.ToCharArray()); 
InBlock.gif        s
=tcpc.GetStream();
InBlock.gif        s.Write(arrCmd, 
0, strCmd.Length);
InBlock.gif            
//以下用于程序调试,显示服务器回应信息
InBlock.gif
        sr = new StreamReader(tcpc.GetStream(), Encoding.Default);
InBlock.gif        strRet
=sr.ReadLine();
InBlock.gif        
return;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/10/22/259689.html,如需转载请自行联系原作者
相关文章
|
16天前
|
存储 文字识别 C#
.NET开源免费、功能强大的 Windows 截图录屏神器
今天大姚给大家分享一款.NET开源免费(基于GPL3.0开源协议)、功能强大、简洁灵活的 Windows 截图、录屏、Gif动图制作神器:ShareX。
|
关系型数据库 MySQL
【Mysql】服务没有响应控制功能。 请键入 NET HELPMSG 2186 以获得更多的帮助。
解决方法: 1. 下载dll文件 https://www.aliyundrive.com/s/oV6GgghtPkN 2.将文件放置在mysql bin文件夹下 3. 重新启动Mysql,发现启动成功了!🚀
728 0
|
1月前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
115 0
|
2月前
|
C# Windows
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
|
7月前
|
Apache
基于commons-net实现ftp创建文件夹、上传、下载功能.
基于commons-net实现ftp创建文件夹、上传、下载功能.
106 0
|
9月前
|
移动开发 监控 网络协议
基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)
基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)
|
5月前
|
开发框架 前端开发 .NET
用ajax和asp.net实现智能搜索功能
用ajax和asp.net实现智能搜索功能
43 0
|
9月前
|
SQL 安全 前端开发
.NET开源免费功能最全的商城项目
.NET开源免费功能最全的商城项目
|
9月前
|
开发框架 前端开发 JavaScript
WPF+ASP.NET SignalR实现简易在线聊天功能
WPF+ASP.NET SignalR实现简易在线聊天功能
129 0
|
9月前
|
数据库 C#
C#,.net,winform导入Excel功能以及下载Excel文件到本地,并使用SqlBulkCopy把DataTable类型的数据写入到sqlserver数据库中
C#,.net,winform导入Excel功能以及下载Excel文件到本地,并使用SqlBulkCopy把DataTable类型的数据写入到sqlserver数据库中
218 0