新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → 用C#做的邮件收发系统,但只能收数据流,请各位帮我做简体,繁体,数字的解码!!!!!!!!! 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 12118 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 用C#做的邮件收发系统,但只能收数据流,请各位帮我做简体,繁体,数字的解码!!!!!!!!! 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     371146381 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:4
      积分:72
      门派:XML.ORG.CN
      注册:2007/12/19

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给371146381发送一个短消息 把371146381加入好友 查看371146381的个人资料 搜索371146381在『 Dot NET,C#,ASP,VB 』的所有贴子 引用回复这个贴子 回复这个贴子 查看371146381的博客楼主
    发贴心情 用C#做的邮件收发系统,但只能收数据流,请各位帮我做简体,繁体,数字的解码!!!!!!!!!

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Net ;
    using System.IO;
    using System.Net.Sockets ;
    using System.Text ;
    namespace MailPOP3
    {
     /// <summary>
     /// Form1 的摘要说明。
     /// </summary>
     public class Form1 : System.Windows.Forms.Form
     {
      private System.Windows.Forms.GroupBox groupBox1;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.Label label3;
      private System.Windows.Forms.Label label4;
      private System.Windows.Forms.GroupBox groupBox2;
      private System.Windows.Forms.GroupBox groupBox3;
      private System.Windows.Forms.TextBox servername;
      private System.Windows.Forms.TextBox mailport;
      private System.Windows.Forms.TextBox password;
      private System.Windows.Forms.TextBox username;
      private System.Windows.Forms.Button receive;
      private System.Windows.Forms.RichTextBox richTextBox1;
      private System.Windows.Forms.RichTextBox richTextBox2;
      /// <summary>
      /// 必需的设计器变量。
      /// </summary>
      private System.ComponentModel.Container components = null;

      public Form1()
      {
       //
       // Windows 窗体设计器支持所必需的
       //
       InitializeComponent();

       //
       // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
       //
      }

      /// <summary>
      /// 清理所有正在使用的资源。
      /// </summary>
      protected override void Dispose( bool disposing )
      {
       if( disposing )
       {
        if (components != null)
        {
         components.Dispose();
        }
       }
       base.Dispose( disposing );
      }

      #region Windows 窗体设计器生成的代码
      /// <summary>
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改
      /// 此方法的内容。
      /// </summary>
      private void InitializeComponent()
      {
                this.groupBox1 = new System.Windows.Forms.GroupBox();
                this.label1 = new System.Windows.Forms.Label();
                this.servername = new System.Windows.Forms.TextBox();
                this.mailport = new System.Windows.Forms.TextBox();
                this.password = new System.Windows.Forms.TextBox();
                this.username = new System.Windows.Forms.TextBox();
                this.label2 = new System.Windows.Forms.Label();
                this.label3 = new System.Windows.Forms.Label();
                this.label4 = new System.Windows.Forms.Label();
                this.groupBox2 = new System.Windows.Forms.GroupBox();
                this.richTextBox1 = new System.Windows.Forms.RichTextBox();
                this.groupBox3 = new System.Windows.Forms.GroupBox();
                this.richTextBox2 = new System.Windows.Forms.RichTextBox();
                this.receive = new System.Windows.Forms.Button();
                this.groupBox1.SuspendLayout();
                this.groupBox2.SuspendLayout();
                this.groupBox3.SuspendLayout();
                this.SuspendLayout();
                //
                // groupBox1
                //
                this.groupBox1.Controls.Add(this.label1);
                this.groupBox1.Controls.Add(this.servername);
                this.groupBox1.Controls.Add(this.mailport);
                this.groupBox1.Controls.Add(this.password);
                this.groupBox1.Controls.Add(this.username);
                this.groupBox1.Controls.Add(this.label2);
                this.groupBox1.Controls.Add(this.label3);
                this.groupBox1.Controls.Add(this.label4);
                this.groupBox1.Location = new System.Drawing.Point(4, 4);
                this.groupBox1.Name = "groupBox1";
                this.groupBox1.Size = new System.Drawing.Size(490, 82);
                this.groupBox1.TabIndex = 0;
                this.groupBox1.TabStop = false;
                this.groupBox1.Text = "接收邮件服务器设置";
                //
                // label1
                //
                this.label1.Location = new System.Drawing.Point(10, 26);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(64, 16);
                this.label1.TabIndex = 1;
                this.label1.Text = "服务器名:";
                //
                // servername
                //
                this.servername.Location = new System.Drawing.Point(76, 22);
                this.servername.Name = "servername";
                this.servername.Size = new System.Drawing.Size(174, 21);
                this.servername.TabIndex = 0;
                //
                // mailport
                //
                this.mailport.Location = new System.Drawing.Point(76, 52);
                this.mailport.Name = "mailport";
                this.mailport.Size = new System.Drawing.Size(174, 21);
                this.mailport.TabIndex = 0;
                //
                // password
                //
                this.password.Location = new System.Drawing.Point(306, 52);
                this.password.Name = "password";
                this.password.PasswordChar = '*';
                this.password.Size = new System.Drawing.Size(174, 21);
                this.password.TabIndex = 0;
                //
                // username
                //
                this.username.Location = new System.Drawing.Point(306, 22);
                this.username.Name = "username";
                this.username.Size = new System.Drawing.Size(174, 21);
                this.username.TabIndex = 0;
                //
                // label2
                //
                this.label2.Location = new System.Drawing.Point(38, 56);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(36, 14);
                this.label2.TabIndex = 1;
                this.label2.Text = "端口:";
                //
                // label3
                //
                this.label3.Location = new System.Drawing.Point(260, 26);
                this.label3.Name = "label3";
                this.label3.Size = new System.Drawing.Size(48, 14);
                this.label3.TabIndex = 1;
                this.label3.Text = "用户名:";
                //
                // label4
                //
                this.label4.Location = new System.Drawing.Point(272, 56);
                this.label4.Name = "label4";
                this.label4.Size = new System.Drawing.Size(36, 16);
                this.label4.TabIndex = 1;
                this.label4.Text = "密码:";
                //
                // groupBox2
                //
                this.groupBox2.Controls.Add(this.richTextBox1);
                this.groupBox2.Location = new System.Drawing.Point(4, 90);
                this.groupBox2.Name = "groupBox2";
                this.groupBox2.Size = new System.Drawing.Size(562, 100);
                this.groupBox2.TabIndex = 2;
                this.groupBox2.TabStop = false;
                this.groupBox2.Text = "邮件名称";
                //
                // richTextBox1
                //
                this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.richTextBox1.Location = new System.Drawing.Point(3, 17);
                this.richTextBox1.Name = "richTextBox1";
                this.richTextBox1.Size = new System.Drawing.Size(556, 80);
                this.richTextBox1.TabIndex = 1;
                this.richTextBox1.Text = "";
                //
                // groupBox3
                //
                this.groupBox3.Controls.Add(this.richTextBox2);
                this.groupBox3.Location = new System.Drawing.Point(2, 194);
                this.groupBox3.Name = "groupBox3";
                this.groupBox3.Size = new System.Drawing.Size(564, 224);
                this.groupBox3.TabIndex = 3;
                this.groupBox3.TabStop = false;
                this.groupBox3.Text = "邮件内容";
                //
                // richTextBox2
                //
                this.richTextBox2.Dock = System.Windows.Forms.DockStyle.Fill;
                this.richTextBox2.Location = new System.Drawing.Point(3, 17);
                this.richTextBox2.Name = "richTextBox2";
                this.richTextBox2.Size = new System.Drawing.Size(558, 204);
                this.richTextBox2.TabIndex = 0;
                this.richTextBox2.Text = "";
                //
                // receive
                //
                this.receive.Location = new System.Drawing.Point(498, 30);
                this.receive.Name = "receive";
                this.receive.Size = new System.Drawing.Size(64, 23);
                this.receive.TabIndex = 4;
                this.receive.Text = "接收邮件";
                this.receive.Click += new System.EventHandler(this.receive_Click);
                //
                // Form1
                //
                this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                this.ClientSize = new System.Drawing.Size(568, 424);
                this.Controls.Add(this.receive);
                this.Controls.Add(this.groupBox3);
                this.Controls.Add(this.groupBox2);
                this.Controls.Add(this.groupBox1);
                this.MaximizeBox = false;
                this.Name = "Form1";
                this.Text = "邮件接收";
                this.groupBox1.ResumeLayout(false);
                this.groupBox1.PerformLayout();
                this.groupBox2.ResumeLayout(false);
                this.groupBox3.ResumeLayout(false);
                this.ResumeLayout(false);

      }
      #endregion

      /// <summary>
      /// 应用程序的主入口点。
      /// </summary>
      [STAThread]
      static void Main()
      {
       Application.Run(new Form1());
                
            }
        
            private void receive_Click(object sender, System.EventArgs e)
            {
                if (this.servername.Text == "")
                {
                    MessageBox.Show("服务器不能为空,请重新输入!");
                    servername.Focus();
                    return;
                }
                if (this.mailport.Text != "110")
                {
                    MessageBox.Show("请输入默认的端口号子110");
                    mailport.Clear();
                    mailport.Focus();
                    return;
                }
                if (this.username.Text == "")
                {
                    MessageBox.Show("用户名不能为空,请重新输入!");
                    username.Focus();
                    return;
                }
                if (this.password.Text == "")
                {
                    MessageBox.Show("密码不能为空,请重新输入!");
                    password.Focus();
                    return;
                }
                int mailnumber;
       //连接服务器
       TcpClient tcpClient=new TcpClient();
       try
       {
                    //连接POP3服务器
        tcpClient.Connect(servername.Text,Int32.Parse (mailport.Text));
       }
       catch
       {
                    //显示错误信息
        MessageBox.Show ("无法连接到POP3服务器"+servername.Text+"的"+mailport.Text+"端口!");
                    servername.Clear();
                    password.Clear();
                    username.Clear();
                    servername.Focus();
                    return;
       }
       //从POP3邮件服务器取得回复
       NetworkStream netStream=tcpClient.GetStream ();
       if (netStream==null)
       {
        throw new Exception ("无法取得回复");
       }
       string returnMsg=ReadFromNetStream(ref netStream);
       checkForError(returnMsg);
       richTextBox1.AppendText("连接应答:"+returnMsg+"\r\n");
                
       //发送用户名信息
       richTextBox1.AppendText("USER: "+this.username.Text+"\r\n");
       WriteToNetStream(ref netStream,"USER "+this.username.Text);
       returnMsg=ReadFromNetStream(ref netStream);
       checkForError(returnMsg);
       richTextBox1.AppendText("POP3SERVER:"+returnMsg+"\r\n");
       //发送用户密码信息
       richTextBox1.AppendText("PASSWORD: "+this.password.Text+"\r\n");
       WriteToNetStream(ref netStream,"PASS "+this.password .Text);
       returnMsg=ReadFromNetStream(ref netStream);
       checkForError(returnMsg);
       richTextBox1.AppendText("POP3SERVER:"+returnMsg+"\r\n");
       //检查邮件
       mailnumber=EmailCheck(ref netStream);
       richTextBox1.AppendText("邮件数:"+mailnumber+" \r\n");
       //接收邮件
       for (int i=1;i<=mailnumber;i++)
       {
        richTextBox1.AppendText("RETR: "+i.ToString() +" \r\n");
        WriteToNetStream(ref netStream,"RETR "+i.ToString());
        returnMsg=ReadFromNetStream(ref netStream);
        checkForError(returnMsg);
        richTextBox1.AppendText("POP3SERVER:"+returnMsg+"\r\n");
        ReadMail(ref netStream,i);

       }
       //断开与POP3服务器的连接
       richTextBox1.AppendText("QUIT: \r\n");
       WriteToNetStream(ref netStream,"QUIT ");
       returnMsg=ReadFromNetStream(ref netStream);
       checkForError(returnMsg);
       richTextBox1.AppendText("POP3SERVER:"+returnMsg+"\r\n");
      }

        
      //邮件检查
      private int EmailCheck(ref NetworkStream NetStream)
      {
       richTextBox1.AppendText("STAT: \r\n");
       WriteToNetStream(ref NetStream,"STAT");
       string returnMsg=ReadFromNetStream(ref NetStream);
       checkForError(returnMsg);
       richTextBox1.AppendText("POP3SERVER:"+returnMsg+"\r\n");
       string[] TotalStat=returnMsg.Split (new char[] {' '});
       int mailNumber=Int32.Parse (TotalStat[1]);
       return mailNumber;
      }

      //向POP3服务器写入命令
      private void WriteToNetStream(ref NetworkStream NetStream,string Command)
      {
       string stringToSend=Command+"\r\n";
       Byte[] arrayToSend=Encoding.ASCII.GetBytes(stringToSend.ToCharArray());
       NetStream.Write(arrayToSend,0,arrayToSend.Length );
      }
      
      //从POP3读取数据
      private string ReadFromNetStream(ref NetworkStream NetStream)
      {
       StringBuilder strReceived=new StringBuilder();
       StreamReader sr=new StreamReader (NetStream);
       String strLine=sr.ReadLine ();
       while(strLine==null || strLine.Length ==0)
       {
        strLine=sr.ReadLine ();
       }
       strReceived.Append(strLine);
       if (sr.Peek ()!=-1)
       {
        while((strLine=sr.ReadLine ())!=null)
        {
         strReceived.Append(strLine);
        }
       }
       return strReceived.ToString();
      }

      //检查流中是否含有错误
      private void checkForError(String strMessage)
      {
       if (strMessage.IndexOf ("+OK")==-1)
        throw new Exception ("收到来自POP3服务器的错误信息:"+strMessage);
      }

           //读邮件
      private void ReadMail(ref NetworkStream NetStream,int number)
      {
       int k=0;
       bool check=false;
       byte[] bb=new byte [6400];
       while(!check)
       {
        k=NetStream.Read (bb,0,bb.Length );
        string read=System.Text .Encoding .UTF8.GetString (bb,0,k);
        int x=read.IndexOf ("\r\n.\r\n");
        if (x!=-1)
        {
         check=true;
        }
        richTextBox2.AppendText (read);
        richTextBox1.AppendText("DELE "+number.ToString ()+"\r\n");
        WriteToNetStream(ref NetStream,"DELE "+number.ToString ());
        string returnMsg=ReadFromNetStream(ref NetStream);
        checkForError(returnMsg);
        richTextBox1.AppendText("POP3SERVER:"+returnMsg+"\r\n");
       }
      }
     }
    }


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/12/19 15:54:00
     
     371146381 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:4
      积分:72
      门派:XML.ORG.CN
      注册:2007/12/19

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给371146381发送一个短消息 把371146381加入好友 查看371146381的个人资料 搜索371146381在『 Dot NET,C#,ASP,VB 』的所有贴子 引用回复这个贴子 回复这个贴子 查看371146381的博客2
    发贴心情 
    没人搞吗?C#高手呢?
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/12/19 17:03:00
     
     netjian 帅哥哟,离线,有人找我吗?白羊座1986-4-16
      
      
      头衔:智能入门者
      等级:大四(GRE考了1600分!)
      文章:198
      积分:1332
      门派:IEEE.ORG.CN
      注册:2007/5/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给netjian发送一个短消息 把netjian加入好友 查看netjian的个人资料 搜索netjian在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给netjian  引用回复这个贴子 回复这个贴子 查看netjian的博客3
    发贴心情 
    只做过简单的发系统,没有收过。
    不做C#好多年,呵呵。

    ----------------------------------------------
    长江后浪,无坚不摧。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/12/19 20:05:00
     
     371146381 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:4
      积分:72
      门派:XML.ORG.CN
      注册:2007/12/19

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给371146381发送一个短消息 把371146381加入好友 查看371146381的个人资料 搜索371146381在『 Dot NET,C#,ASP,VB 』的所有贴子 引用回复这个贴子 回复这个贴子 查看371146381的博客4
    发贴心情 感谢!!
    还是非常感谢你的支持哦,我现在还在找,解码是最关键的...
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/12/20 8:55:00
     
     371146381 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:4
      积分:72
      门派:XML.ORG.CN
      注册:2007/12/19

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给371146381发送一个短消息 把371146381加入好友 查看371146381的个人资料 搜索371146381在『 Dot NET,C#,ASP,VB 』的所有贴子 引用回复这个贴子 回复这个贴子 查看371146381的博客5
    发贴心情 
    [color=#FF0000][size=4]怎么没人帮我啊,这样吧,我把找到的解码的代码也发下,请高手帮我连接,两个类怎么连起来解码[/size][/color]


    using System;
    using System.Collections;
    using System.Globalization;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO;
    using System.Net.Mail;
    namespace MailPOP3
    {
        class Pop3Decode
        {
            private static Encoding SourceEncoding = null;
            private string m_from;
            private string m_to;
            private string m_cc;
            private string m_subject;
            private string m_contentType;
            private static string m_messageId = null;
            private static string tmpFile = Environment.GetEnvironmentVariable("TEMP") + @"\Pop3ClientTmp.msg";
            private ArrayList arBytes = new ArrayList();
            private bool m_isMultipart = false;

            private string m_multipartBoundary;

            private const int m_fromState = 0;
            private const int m_toState = 1;
            private const int m_subjectState = 2;
            private const int m_contentTypeState = 3;
            private const int m_contentTransferEncodingType = 4;
            private const int m_messageID = 5;
            private const int m_charset = 6;
            private const int m_CcState = 7;

            private const int m_notKnownState = -99;
            private const int m_endOfHeader = -98;
            public string[] m_lines = null;
            public byte[] m_content = null;
            public string m_boundary = "";
            public string m_defCharSet = "";
            public string m_charSet = "";
            public string m_defContentTransferEncoding = "";
            public string m_Description = "";
            public string m_Disposition = "";
            public string m_Location = "";
            public string m_ContentType = "";
            public string m_Name = "";
            public string m_FileName = "";
            public Pop3Decode()
            {
                //
                // TODO: Add constructor logic here
                //
            }

            internal static string ParseSubject(string str)
            {
                return TranslateString(str).Replace("_", " ");
            }
           
            internal static string TranslateString(string str)
            {
                if (str != null && str.Length > 0)
                    return Regex.Replace(str, @"=\?([^\?]*)\?(\S)\?([^\?]*)\?=", new MatchEvaluator(DecodeSubstringToString));
                return "";
            }

            public static System.Text.Encoding GetEncoding(string CharSet)
            {
                if (Regex.IsMatch(CharSet, ".*ascii.*", RegexOptions.IgnoreCase))
                    return System.Text.Encoding.Default;
                try
                {
                    return System.Text.Encoding.GetEncoding(CharSet);
                }
                catch
                {
                    return System.Text.Encoding.Default;
                }
            }
            private static string DecodeQuotedPrintableToString(Match qp)
            {
                string[] arr = qp.Value.TrimStart('=').Split('=');
                byte[] res = new byte[arr.Length];
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i].Trim() != string.Empty)
                    {
                        byte[] buftest = new byte[2];

                        res[i] = (byte)int.Parse(arr[i].Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
                        //res[i] = System.Convert.ToByte(arr[i], 16);
                    }
                }
                return SourceEncoding.GetString(res).Trim();
            }

            public static string DecodeToDefault(string encoding, byte[] Data, string CharSet)
            {
                if (Data != null && Pop3Decode.Decode(encoding, Data, CharSet) != null)
                {
                    return
                        Encoding.Default.GetString(
                            Encoding.Convert(
                                GetEncoding(CharSet),
                                Encoding.Default,
                                Pop3Decode.Decode(encoding, Data, CharSet)
                            )
                        ).Trim();
                }
                return "";
            }
            public static byte[] Decode(string encoding, byte[] Body, string CharSet)
            {
                SourceEncoding = GetEncoding(CharSet);
                if (Body == null)
                    return (new byte[0]);
                if (encoding.ToLower() == "quoted-printable")
                {
                    //RetVal = Decode(Text, GetEncoding(CharSet));
                    try
                    {
                        string str = TranslateString(SourceEncoding.GetString(Body)).Replace("\r", "");
                        SourceEncoding = GetEncoding(CharSet);

                        return SourceEncoding.GetBytes(Regex.Replace(str, "(=[0-9A-F][0-9A-F])+", new MatchEvaluator(DecodeQuotedPrintableToString)));


                    }
                    catch//(Exception exc)
                    {

                        return Decode("base64", Body, CharSet);
                    }
                }
                else if (encoding.ToLower() == "base64")
                {
                    try
                    {
                        return System.Convert.FromBase64String(GetEncoding(CharSet).GetString(Body).Replace("\n", "").Replace("\r", ""));
                    }
                    catch//(Exception exc)
                    {

                        return Decode("8bit", Body, CharSet);
                    }
                }
                else if (encoding.ToLower() == "8bit")
                {
                    return Body;
                }
                else if (encoding.Length <= 0)
                {
                    return Decode("quoted-printable", Body, CharSet);
                }
                return Body;
            }
            static string HexDecoder(string line)
            {

                Regex re = new Regex(
                    "(\\=([0-9A-F][0-9A-F]))",
                    RegexOptions.IgnoreCase
                    );
                return re.Replace(line, new MatchEvaluator(HexDecoderEvaluator));
            }

            static string DecodeSubstringToString(Match m)
            {
                SourceEncoding = GetEncoding(m.Groups[1].Value);
                if (m.Groups[2].Value.ToLower() == "b")
                    return SourceEncoding.GetString(System.Convert.FromBase64String(m.Groups[3].Value));
                return Regex.Replace(m.Groups[3].Value, "(=[0-9A-F][0-9A-F])+", new MatchEvaluator(DecodeQuotedPrintableToString));
            }

            static string HexDecoderEvaluator(Match m)
            {
                return ((char)System.Convert.ToByte(m.Groups[2].Value, 16)).ToString();
            }
            //Pop3Message.cs里面要用到的东西
            private string[] m_lineTypeString =
      {
       "From",
       "To",
       "Subject",
       "Content-Type",
       "Content-Transfer-Encoding",
       "Message-ID",
       "charset",
                "Cc"
      };
            public string MessageId
            {
                get { return m_messageId != null ? m_messageId : ""; }
                set { m_messageId = value; }
            }
      
            private int GetHeaderLineType(string line)
            {
                int lineType = m_notKnownState;

                for (int i = 0; i < m_lineTypeString.Length; i++)
                {
                    string match = m_lineTypeString[i];

                    if (Regex.Match(line, "^" + match + ":" + ".*$").Success)
                    {
                        lineType = i;
                        break;
                    }
                    else
                        if (line.Length == 0)
                        {
                            lineType = m_endOfHeader;
                            break;
                        }
                }

                return lineType;
            }
            private long ParseHeader(string[] lines)
            {
                int numberOfLines = lines.Length;
                long bodyStart = 0;

                for (int i = 0; i < numberOfLines; i++)
                {
                    string currentLine = lines[i].Replace("\n", "");

                    switch (GetHeaderLineType(currentLine))
                    {
                        // CharSet:
                        case m_charset:
                            m_charSet = Pop3Decode.CharSet(GetDecodeLine(i));
                            break;
                        // Message-ID:
                        case m_messageID:
                            MessageId = Pop3Decode.MessageID(GetDecodeLine(i));
                            break;
                        // From:
                        case m_fromState:
                            m_from = Pop3Decode.TranslateString(Pop3Decode.From(GetDecodeLine(i)));
                            break;

                        // Subject:
                        case m_subjectState:
                            m_subject = Pop3Decode.TranslateString(GetDecodeLine(i)).Trim();
                            i++;
                            while (lines[i].Replace("\n", "").Replace("\r", "").StartsWith(" "))
                                m_subject += Pop3Decode.TranslateString(GetDecodeLine(i++).Replace("\n", "").Replace("\r", ""));
                            m_subject = Pop3Decode.Subject(m_subject);
                            i--;
                            break;

                        // To:
                        case m_toState:
                            m_to = Pop3Decode.TranslateString(Pop3Decode.To(GetDecodeLine(i)));
                            break;
                        //Cc:
                        case m_CcState:
                            m_cc = Pop3Decode.TranslateString(Pop3Decode.Cc(GetDecodeLine(i)));
                            break;

                        // Content-Type
                        case m_contentTypeState:

                            m_contentType =
                                Pop3Decode.ContentType(currentLine);

                            if (Pop3Decode.IsCharSet(currentLine))
                                m_charSet = Pop3Decode.CharSet(currentLine);

                            m_isMultipart = Pop3Decode.IsMultipart(m_contentType);

                            if (m_isMultipart)
                            {
                                // if boundary definition is on next
                                // line ...

                                if (m_contentType
                                    .Substring(m_contentType.Length - 1, 1).
                                    Equals(";"))
                                {
                                    ++i;

                                    m_multipartBoundary
                                        = Pop3Decode.
                                        MultipartBoundary(lines[i].
                                        Replace("\n", ""));
                                }
                                else
                                {
                                    // boundary definition is on same
                                    // line as "Content-Type" ...

                                    if (Pop3Decode.IsMultipartBoundary(lines[i].Trim()))
                                        m_multipartBoundary =
                                            Pop3Decode
                                            .MultipartBoundary(lines[i].Trim());
                                    else
                                        m_multipartBoundary =
                                            Pop3Decode
                                            .MultipartBoundary(lines[i + 1].Trim());
                                }
                            }

                            break;

                        case m_endOfHeader:
                            bodyStart = i + 1;
                            break;
                    }

                    if (bodyStart > 0)
                    {
                        break;
                    }
                }

                return (bodyStart);
            }
            //Pop3Parse.cs里面要用到的东西
            public static string From(string line)
            {
                return
                    Pop3Decode.TranslateString(
                        Regex.Replace(
                            Pop3Decode.TranslateString(line),
                            @"^From:[ ]*([^;]+)[ ]*$",
                            "$1"
                        )
                    );
            }

            public static string Subject(string line)
            {
                return Regex.Replace(
                    Pop3Decode.TranslateString(line),
                    @"^Subject:[ ]*([^;]*)[ ]*$",
                    "$1"
                ).Replace("_", " ");
            }

            public static string MessageID(string line)
            {
                return Regex.Replace(
                    Pop3Decode.TranslateString(line),
                    @"^Message-ID:[ ]*([^;]+)[ ]*$",
                    "$1",
                    RegexOptions.IgnoreCase
                );
            }

            public static string To(string line)
            {
                return
                    Pop3Decode.TranslateString(
                        Regex.Replace(
                            Pop3Decode.TranslateString(line),
                            @"^To:[ ]*([^;]+)[ ]*$",
                            "$1"
                        )
                    );
            }

            public static string Cc(string line)
            {
                return
                    Pop3Decode.TranslateString(
                        Regex.Replace(
                            Pop3Decode.TranslateString(line),
                            @"^Cc:[ ]*([^;]+)[ ]*$",
                            "$1"
                        )
                    );
            }

            public static string ContentType(string line)
            {
                if (Regex.IsMatch(line, @".*Content-Type: [""]*([^ ;""]*).*"))
                    return
                        Pop3Decode.TranslateString(
                            Regex.Replace(
                                line.Trim(),
                                @".*Content-Type: [""]*([^ ;""]*).*",
                                "$1"
                            )
                        );
                return "";
            }
            public static string Name(string line)
            {
                if (Regex.IsMatch(line, ".*[^e]name[ ]*=[ ]*[\"]*([^\"\r]*).*"))
                    return Regex.Replace(
                        Pop3Decode.TranslateString(line),
                        ".*[^e]name[ ]*=[ ]*[\"]*([^\"\r]*).*",
                        "$1"
                    );
                return "";
            }

            public static string Filename(string line)
            {
                if (Regex.IsMatch(line, ".*filename[ ]*=[ ]*[\"]*([^\"\r]*).*"))
                    return Regex.Replace(
                        Pop3Decode.TranslateString(line),
                        ".*filename[ ]*=[ ]*[\"]*([^\"\r]*).*",
                        "$1"
                    );
                return "";
            }
            public static string CharSet(string line)
            {
                if (IsCharSet(line))
                {
                    return
                        Regex.Replace(
                            line,
                            @".*charset[ ]*=[ ]*[""]*([^;""]*)[""]*.*",
                            "$1"
                        ).Trim();
                }
                return "";
            }
            public static bool IsCharSet(string line)
            {
                return Regex.IsMatch(line, "[ | ]*charset[ ]*=", RegexOptions.IgnoreCase);
            }
            public static bool IsMultipart(string line)
            {
                return
                    Regex.Match(line, "^multipart/.*").Success;
            }

            public static bool IsMultipartBoundary(string line)
            {
                return
                    Regex.Match(line
                    , ".*boundary[ ]*=[ ]*[\"]*([^\"\n\r]*).*").Success;
            }

            public static string MultipartBoundary(string line)
            {
                if (IsMultipartBoundary(line))
                    return
                        Regex.Replace(line,
                            ".*boundary[ ]*=[ ]*[\"]*([^\"]*).*",
                            "$1"
                        );
                return "";
            }
            public static bool IsContentTransferEncoding(string line)
            {
                return
                    Regex.IsMatch(
                        line,
                        @"^Content-Transfer-Encoding: (.*)$",
                        RegexOptions.IgnoreCase
                    );
            }

            public static string ContentTransferEncoding(string line)
            {
                if (IsContentTransferEncoding(line))
                    return
                        Regex.Replace(
                            line,
                            @"^Content-Transfer-Encoding: (.*)$",
                            "$1",
                            RegexOptions.IgnoreCase
                        );
                return "";
            }


            //Pop3Component里面要用到的东西
            public byte[] Data
            {
                get { return m_binaryData; }
            }
            public byte[] m_binaryData;
           /* public string GetDecodeDataAsString()
            {
              return Pop3Decode.DecodeToDefault(ContentTransferEncoding, Data, CharSet);
            }

            public byte[] GetDecodeDataAsByte()
            {
               return Pop3Decode.Decode(ContentTransferEncoding, Data, CharSet);
            }*/
            //Pop3Msg.cs里面要用到的东西
            public Pop3Decode(byte[] content, string boundary, string CharSet, string defContentTransferEncoding)
      {
       m_content = new byte[content.Length];
       Array.Copy( content, 0, m_content, 0, content.Length);
       m_lines = Encoding.Default.GetString(content).Split(new char[] { '\r'});
       m_boundary = boundary;
       m_defCharSet = CharSet;
       m_charSet = CharSet;
       m_defContentTransferEncoding = defContentTransferEncoding;
      }
            public string getFirstContentTransferEncoding(long i)
            {
                if (m_lines != null && m_lines.Length <= i) return "";
                for (; i < m_lines.Length; i++)
                    if (Pop3Decode.IsContentTransferEncoding(m_lines[i].Trim()))
                        return Pop3Decode.ContentTransferEncoding(m_lines[i].Trim());
                return "";
            }
            public string getFirstCharSet(long i)
            {
                if (m_lines != null && m_lines.Length <= i) return "";
                for (; i < m_lines.Length; i++)
                    if (Pop3Decode.IsCharSet(m_lines[i]))
                        return Pop3Decode.CharSet(m_lines[i]);
                return "";
            }
      public string defContentTransferEncoding
      {
       get
       {
        if(m_defContentTransferEncoding==null || m_defContentTransferEncoding.Length<=0)
         m_defContentTransferEncoding = getFirstContentTransferEncoding(0);
        return m_defContentTransferEncoding;
       }
       set
       {
        m_defContentTransferEncoding = value;
       }
      }
      public string defCharSet
      {
       get
       {
        if(m_defCharSet==null || m_defCharSet.Length<=0)
          m_defCharSet = getFirstCharSet(0);
        if(m_defCharSet==null || m_defCharSet.Length<=0)
         return m_charSet;
        return m_defCharSet;
       }
       set
       {
        m_defCharSet = value;
       }
      }
            public long GetNum(long i)
            {
                if (m_lines != null && m_lines.Length <= i) return i;

                long iRet = 0;
                long j = 0;
                foreach (string s in m_lines)
                {
                    if (j == i)
                        return iRet;
                    iRet += s.ToCharArray().Length + 1;
                    j++;
                }
                return iRet;
            }
            public byte[] GetLineAsByte(long i)
            {
                if (m_lines != null && m_lines.Length <= i) return null;
                byte[] bData = null;
                long StartByte = GetNum(i) + 1;
                long Length = GetNum(i + 1) - StartByte - 1;
                if (Length < 0) Length = 0;
                bData = new byte[Length];
                Array.Copy(m_content, StartByte, bData, 0, Length);
                return bData;
            }

            public string GetDecodeLine(long i)
            {
               return Pop3Decode.DecodeToDefault(defContentTransferEncoding, GetLineAsByte(i), defCharSet);
            }

        }
    }

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/1/3 11:05:00
     
     371146381 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:4
      积分:72
      门派:XML.ORG.CN
      注册:2007/12/19

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给371146381发送一个短消息 把371146381加入好友 查看371146381的个人资料 搜索371146381在『 Dot NET,C#,ASP,VB 』的所有贴子 引用回复这个贴子 回复这个贴子 查看371146381的博客6
    发贴心情 
    哎,可怜啊!!!
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/1/7 10:28:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/3/28 12:49:20

    本主题贴数6,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    257.813ms