注册本站  论坛  繁體中文

电脑技巧
手机 | MP3 | MP4 | 显卡 | 主板 | 显示器 | 光存储 | 笔记本 | 网络设备 | 移动存储 | 数码相机
键鼠 | CPU | 音箱 | GPS | 电视 | 服务器 | 投影机 | 机箱电源 | 品牌电脑 | 办公打印 |
| 网站首页 | Cisco | Windows | Linux | Java | Dotnet | Oracle | 网页设计 | 平面设计 | 安全 | 软件应用 | 电脑维修 | 办公维修 |
您现在的位置: 电脑技巧 >> Dotnet >> C# >> Dotnet正文

C#的PoP3连接

文章来源:blog.csdn.net 作者:neok 更新时间:2008-6-28 20:39:00 【 】 【加入收藏
 
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5.   
  6. using System.Net.Sockets;    
  7. using System.IO;    
  8. using System.Net;    
  9.   
  10. namespace ConsoleApplication1   
  11. {   
  12.     class Program   
  13.     {   
  14.         static void Main(string[] args)   
  15.         {   
  16.             POP pop3 = new POP("E-Mail.Server", "UserName", "PassWord");   
  17.             Console.WriteLine("New Messages = {0}", pop3.GetNumberOfNewMessages());   
  18.             Console.ReadLine();    
  19.         }   
  20.     }   
  21.   
  22.     class POP   
  23.     {   
  24.         string POPServer;   
  25.         string user;   
  26.         string pwd;   
  27.         public POP() { }   
  28.         public POP(string _popserver, string _user, string _pwd)   
  29.         {   
  30.             POPServer = _popserver;   
  31.             user = _user;   
  32.             pwd = _pwd;   
  33.         }   
  34.         private NetworkStream Connect()   
  35.         {   
  36.             TcpClient sender = new TcpClient(POPServer, 110);   
  37.             Byte[] outbytes;   
  38.             string input;   
  39.             NetworkStream ns = null;   
  40.             try  
  41.             {   
  42.                 ns = sender.GetStream();   
  43.                 StreamReader sr = new StreamReader(ns);   
  44.                 Console.WriteLine("1:" + sr.ReadLine());   
  45.   
  46.                 input = "user " + user + "\r\n";   
  47.                 outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());   
  48.                 ns.Write(outbytes, 0, outbytes.Length);   
  49.                 Console.WriteLine("2:" + sr.ReadLine());   
  50.   
  51.                 input = "pass " + pwd + "\r\n";   
  52.                 outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());   
  53.                 ns.Write(outbytes, 0, outbytes.Length);   
  54.                 Console.WriteLine("3:" + sr.ReadLine());   
  55.   
  56.                 return ns;   
  57.             }   
  58.             catch (InvalidOperationException ioe)   
  59.             {   
  60.                 Console.WriteLine("Could not connect to mail server");   
  61.                 return ns;   
  62.             }   
  63.         }   
  64.         public int GetNumberOfNewMessages()   
  65.         {   
  66.             Byte[] outbytes;   
  67.             string input;   
  68.             try  
  69.             {   
  70.                 NetworkStream ns = Connect();   
  71.                 StreamReader sr = new StreamReader(ns);   
  72.   
  73.                 input = "stat" + "\r\n";   
  74.                 outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());   
  75.                 ns.Write(outbytes, 0, outbytes.Length);   
  76.                 string resp = sr.ReadLine();   
  77.                 Console.WriteLine("4:" + resp);   
  78.                 string[] tokens = resp.Split(new Char[] { ' ' });   
  79.   
  80.                 input = "quit" + "\r\n";   
  81.                 outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());   
  82.                 ns.Write(outbytes, 0, outbytes.Length);   
  83.                 Console.WriteLine("5:" + sr.ReadLine());   
  84.   
  85.                 sr.Close();   
  86.                 ns.Close();   
  87.                 //return tokens[1].ToInt32();   
  88.                 return Convert.ToInt32(tokens[1]);   
  89.             }   
  90.             catch (InvalidOperationException ioe)   
  91.             {   
  92.                 Console.WriteLine("Could not connect to mail server");   
  93.                 return 0;   
  94.             }   
  95.         }   
  96.     }   
  97.   
  98. }  
  • 上一篇Dotnet:

  • 下一篇Dotnet:
  • 最 新 热 门
     web.config配置文件中的 元素
     为网站添加业务层
     用SqlDataSource实现DataList嵌套DataList
     Visual Studio 2003插件的编写
     千条DOS命令收藏
     IIS 常见问题
     IIS需要的最小NTFS权限
     优化 .NET的性能
     设计模式与VB .net代码 外观模式,合成模式
     VB.net中介者模式
    最 新 推 荐
     Windows via C/C++ —进程(一)
     C#邮件发送程序
     扩展 ASP.NET 的客户端验证
     实现DataGridView中行的上下移动
     C#中的委托和事件
     与IDE相关的Attribute属性
     C#中using关键字的使用介绍
     C#FileStream复制大文件
     C#实用技巧:轻松实现对文件的操作
     C#实现所有经典排序算法
    相 关 文 章

    C#通过ADO.NET建表点实现方法
    asp.net+C#实现文件上传实现代码
    C#邮件发送程序
    C#中的委托和事件
    C#中using关键字的使用介绍
    C#FileStream复制大文件
    C#实用技巧:轻松实现对文件的操作
    C#实现所有经典排序算法
    c#生成的word文档
    C#编程技巧:轻松实现对文件的操作

    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告

     

    Copyright 2006-2008 pcjx.com All Rights Reserved
    电脑技巧 版权所有 粤ICP备06059145号 地图
    本网站所有内容未经许可不得转载或做其他使用