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(01000+ "...");
            }

            else
            ...{
                Console.WriteLine(bodytype 
+ "" + body);
            }

        }
    }
}

 

下載地址:https://download.csdn.net/download/sy_litao/2132327

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章