Code:
  1. using System;   
  2. using System.Text;   
  3. using System.IO;   
  4. using Email.POP3;   
  5.   
  6. namespace TestPOP3   
  7. ...{   
  8.     class example   
  9.     ...{   
  10.         [STAThread]   
  11.         static void Main(string[] args)   
  12.         ...{   
  13.             //我测试的时候用的是163的邮箱,163的免费POP邮件服务器是pop.163.com。而163官方给出的是                       
  14.             //pop.126.com在这里不能用,原因是这个邮件服务器是有SSL加密的,GMAIL我也测试了也不能用都是这个原因   
  15.             POP3 objPOP3 = new POP3("pop.163.com", 110, "用户名""密码");   
  16.             Console.WriteLine(objPOP3.Connect() ? "Connected" : "Can't connect");   
  17.             try  
  18.             ...{   
  19.                 if (objPOP3.IsAPOPSupported)   
  20.                 ...{   
  21.                     Console.WriteLine(objPOP3.SecureLogin() ? "Secure Logged in" : "Can't login");   
  22.                 }   
  23.                 else  
  24.                 ...{   
  25.                     Console.WriteLine(objPOP3.Login() ? "Logged in" : "Can't login");   
  26.                 }   
  27.                 objPOP3.QueryServer();   
  28.                 Console.WriteLine("Emails count: " + objPOP3.TotalMailCount);   
  29.                 //以下的FOR循环是显示出所有收件箱里面的邮件信息   
  30.                 for (int i = 1; i <= objPOP3.TotalMailCount; i++)   
  31.                 ...{   
  32.                     EmailMessage objEmail = objPOP3.GetMessage(i, false); // use true to get headers only   
  33.                     Console.WriteLine("NEW MESSAGE:------------------");   
  34.                     Console.WriteLine("FROM: " + objEmail.From);   
  35.                     Console.WriteLine("TO: " + objEmail.To);   
  36.                     Console.WriteLine("CC: " + objEmail.Cc);   
  37.                     Console.WriteLine("SUBJECT: " + objEmail.Subject);   
  38.                     Console.WriteLine("DATE: " + objEmail.Date);   
  39.                     Console.WriteLine("CONTENT-TYPE: " + objEmail.ContentType);   
  40.                     Console.WriteLine("CHARSET: " + objEmail.Charset);   
  41.                     Console.WriteLine("MESSAGE-ID: " + objEmail.GetCustomHeader("Message-ID"));    
  42.                     Console.WriteLine("MESSAGE SIZE: " + objEmail.Size);   
  43.                     if (objEmail.IsAnyAttachments)   
  44.                     ...{   
  45.                         for (int a = 0; a < objEmail.Attachments.Count; a++)   
  46.                         ...{   
  47.                             //调用邮件附件的方法   
  48.                             processAttachment((Attachment)objEmail.Attachments[a], 1);   
  49.                         }   
  50.                     }   
  51.                     else  
  52.                     ...{   
  53.                         Console.WriteLine("BODY: " + Encoding.Default.GetString(Convert.FromBase64String(objEmail.Body)));   
  54.                     }   
  55.                     //下面注册掉的代码是删除该邮件   
  56.                     //objPOP3.DeleteMessage(i);   
  57.   
  58.                 }   
  59.                 objPOP3.Close();   
  60.             }   
  61.             catch (System.Exception e)   
  62.             ...{   
  63.                 Console.WriteLine(e.Message);   
  64.                 Console.ReadLine();   
  65.                 objPOP3.Close();   
  66.                 return;   
  67.             }   
  68.   
  69.         }   
  70.   
  71.         static void processAttachment(Attachment att, int nesting)   
  72.         ...{   
  73.             for(int i = 0; i < nesting * 2; i++) Console.Write("-");   
  74.   
  75.             //以下注释掉的代码可以打开,以下都是关于邮件附件的相关信息,因为我只需要得到附件的文件信息^_^   
  76.   
  77.             //Console.WriteLine("ATT: ");   
  78.             //Console.WriteLine("ContentTransferEncoding: " + att.ContentTransferEncoding);   
  79.             //Console.WriteLine("ContentType: " + att.ContentType);   
  80.             //Console.WriteLine("EstimatedSize: " + att.EstimatedSize);   
  81.             //Console.WriteLine("FileName: " + att.FileName);   
  82.             //processBody("HtmlBody", att.HtmlBody);   
  83.             //processBody("TextBody", att.TextBody);   
  84.             //Console.WriteLine("IsAnyAttachments: " + att.IsAnyAttachments);   
  85.             //Console.WriteLine("IsFileAttachment: " + att.IsFileAttachment);   
  86.             if (att.IsAnyAttachments)   
  87.             ...{   
  88.                 for (int a = 0; a < att.Attachments.Count; a++)   
  89.                 ...{   
  90.                     processAttachment((Attachment)att.Attachments[a], nesting * 2);   
  91.                 }   
  92.             }   
  93.             if(att.IsFileAttachment)   
  94.             ...{   
  95.                 //这里说一下在保存邮件附件之前必须"c:/pop3"该文件夹是存在的,否则是保存不了的   
  96.                 att.Save(@"c:/pop3" + att.FileName);   
  97.                 Console.WriteLine("附件保存成功!附件名称为:" + att.FileName);   
  98.             }   
  99.         }   
  100.   
  101.         static void processBody(string bodytype, string body)   
  102.         ...{   
  103.             if (body == null)    
  104.             ...{   
  105.                 Console.WriteLine(bodytype + ": null");   
  106.                 return;   
  107.             }   
  108.             if (body.Length > 1000)   
  109.             ...{   
  110.                 Console.WriteLine(bodytype + ": " + body.Substring(0, 1000) + "...");   
  111.             }   
  112.             else  
  113.             ...{   
  114.                 Console.WriteLine(bodytype + ": " + body);   
  115.             }   
  116.         }   
  117.     }   
  118. }   
  119.   
  120.   
  121. 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sy_litao/archive/2008/01/08/2030974.aspx  
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()