[转]C#接收邮件
最近由于工作需要,接触到了邮件服务器.以前我用CF写过,感觉没有什么,我想用C#来实现会更简单,但是万万没想到C#没有提供邮件接收的方法,令我很不解.通过我在网上查找,发现了一个国外公司写好的接收邮件软件,通过我把他的DLL破解之后就不用注册了,要不可是需要花钱买序列号的啊好贵的啊!不过我希望各位如果有能力还是去买正版的为好!我破解他的确是没有办法的办法拉!请各位能够理解^_^ 下面是接收邮件的主程序代码如下: using System; using System.Text; using System.IO; using Email.POP3; namespace TestPOP3 { class example { [STAThread] static void Main(string[] args) { //我测试的时候用的是163的邮箱,163的免费POP邮件服务器是pop.163.com。而163官方给出的是 //pop.126.com在这里不能用,原因是这个邮件服务器是有SSL加密的,GMAIL我也测试了也不能用都是这个原因 POP3 objPOP3 = new POP3("pop.163.com", 110, "用户名", "密码"); Console.WriteLine(objPOP3.Connect() ? "Connected" : "Can't connect"); try { if (objPOP3.IsAPOPSupported) { Console.WriteLine(objPOP3.SecureLogin() ? "Secure Logged in" : "Can't login"); } else { Console.WriteLine(objPOP3.Login() ? "Logged in" : "Can't login"); } objPOP3.QueryServer(); Console.WriteLine("Emails count: " + objPOP3.TotalMailCount); //以下的FOR循环是显示出所有收件箱里面的邮件信息 for (int i = 1; i <= objPOP3.TotalMailCount; i++) { EmailMessage objEmail = objPOP3.GetMessage(i, false); // use true to get headers only Console.WriteLine("NEW MESSAGE:------------------"); Console.WriteLine("FROM: " + objEmail.From); Console.WriteLine("TO: " + objEmail.To); Console.WriteLine("CC: " + objEmail.Cc); Console.WriteLine("SUBJECT: " + objEmail.Subject); Console.WriteLine("DATE: " + objEmail.Date); Console.WriteLine("CONTENT-TYPE: " + objEmail.ContentType); Console.WriteLine("CHARSET: " + objEmail.Charset); Console.WriteLine("MESSAGE-ID: " + objEmail.GetCustomHeader("Message-ID")); Console.WriteLine("MESSAGE SIZE: " + objEmail.Size); if (objEmail.IsAnyAttachments) { for (int a = 0; a < objEmail.Attachments.Count; a++) { //调用邮件附件的方法 processAttachment((Attachment)objEmail.Attachments[a], 1); } } else { Console.WriteLine("BODY: " + Encoding.Default.GetString(Convert.FromBase64String(objEmail.Body))); } //下面注册掉的代码是删除该邮件 //objPOP3.DeleteMessage(i); } objPOP3.Close(); } catch (System.Exception e) { Console.WriteLine(e.Message); Console.ReadLine(); objPOP3.Close(); return; } } static void processAttachment(Attachment att, int nesting) { for(int i = 0; i < nesting * 2; i++) Console.Write("-"); //以下注释掉的代码可以打开,以下都是关于邮件附件的相关信息,因为我只需要得到附件的文件信息^_^ //Console.WriteLine("ATT: "); //Console.WriteLine("ContentTransferEncoding: " + att.ContentTransferEncoding); //Console.WriteLine("ContentType: " + att.ContentType); //Console.WriteLine("EstimatedSize: " + att.EstimatedSize); //Console.WriteLine("FileName: " + att.FileName); //processBody("HtmlBody", att.HtmlBody); //processBody("TextBody", att.TextBody); //Console.WriteLine("IsAnyAttachments: " + att.IsAnyAttachments); //Console.WriteLine("IsFileAttachment: " + att.IsFileAttachment); if (att.IsAnyAttachments) { for (int a = 0; a < att.Attachments.Count; a++) { processAttachment((Attachment)att.Attachments[a], nesting * 2); } } if(att.IsFileAttachment) { //这里说一下在保存邮件附件之前必须"c:/pop3"该文件夹是存在的,否则是保存不了的 att.Save(@"c:/pop3" + att.FileName); Console.WriteLine("附件保存成功!附件名称为:" + att.FileName); } } static void processBody(string bodytype, string body) { if (body == null) { Console.WriteLine(bodytype + ": null"); return; } if (body.Length > 1000) { Console.WriteLine(bodytype + ": " + body.Substring(0, 1000) + "..."); } else { Console.WriteLine(bodytype + ": " + body); } } } }